diff --git a/applications/PLC_link.c b/applications/PLC_link.c index 9395e72..5c9f1a1 100644 --- a/applications/PLC_link.c +++ b/applications/PLC_link.c @@ -27,8 +27,9 @@ static const mb_backend_param_t mb_bkd_prm = { .rtu.lvl = 0 //控制发送电平 }; -extern unsigned int sys_temperature; -extern unsigned int sys_humidity; +extern rt_int32_t sys_temperature; +extern rt_int32_t sys_humidity; +extern unsigned char sys_time[6]; static void mb_plc_read_regs(mb_inst_t *hinst) { @@ -40,8 +41,12 @@ static void mb_plc_read_regs(mb_inst_t *hinst) u16 regs[64]; - regs[0]=sys_temperature; - regs[1]=sys_humidity; + regs[0]=sys_temperature;//系统温度 + regs[1]=sys_humidity;//系统湿度 + for(int8_t i=0;i<=5;i++)//系统时间 + { + regs[i+2] = sys_time[i]; + } int addr = 7000; int nb = 10; diff --git a/applications/data/Variable.c b/applications/data/Variable.c index f919a7a..bdf76e1 100644 --- a/applications/data/Variable.c +++ b/applications/data/Variable.c @@ -2,9 +2,12 @@ #include // ===== 实际定义 ===== +//系统信息变量 +rt_int32_t sys_temperature;//主机环境温度 +rt_int32_t sys_humidity;//主机环境湿度 +unsigned char sys_time[6];//系统时间 +unsigned int sys_run_time;//运行时间 -unsigned int sys_temperature; -unsigned int sys_humidity; // 动态数据缓冲区(初始化为 NULL,后续分配) char *DATA_dat; diff --git a/applications/drv/drv_aht20.c b/applications/drv/drv_aht20.c index ced01e4..5b2fc5c 100644 --- a/applications/drv/drv_aht20.c +++ b/applications/drv/drv_aht20.c @@ -135,8 +135,8 @@ static void aht20_thread_entry(void *parameter) { rt_uint8_t raw_data[6]; - extern unsigned int sys_temperature; - extern unsigned int sys_humidity; + extern rt_int32_t sys_temperature; + extern rt_int32_t sys_humidity; while (1) { diff --git a/applications/drv/drv_ds1307.c b/applications/drv/drv_ds1307.c index b1d7055..f3097e0 100644 --- a/applications/drv/drv_ds1307.c +++ b/applications/drv/drv_ds1307.c @@ -141,6 +141,9 @@ static void ds1307_read_time(void) } /* Software thread to maintain local time estimation */ +time_t now; +struct tm *p_tm; +extern unsigned char sys_time[6]; static void auto_update_current_datatime(void *parameter) { uint32_t count = 0; @@ -154,6 +157,14 @@ static void auto_update_current_datatime(void *parameter) ds1307_read_time(); count = 0; } + time(&now); // 获取当前日历时间(秒,自 1970-01-01 00:00:00 UTC) + p_tm = localtime(&now); // 转换为本地时间结构 + sys_time[5]= p_tm->tm_year - 100; + sys_time[4]= p_tm->tm_mon + 1; + sys_time[3]= p_tm->tm_mday; + sys_time[2]= p_tm->tm_hour; + sys_time[1]= p_tm->tm_min; + sys_time[0]= p_tm->tm_sec;; } } @@ -210,7 +221,7 @@ static int rt_hw_ds1307_rtc_init(void) ds1307_read_time(); - ds1307.thd_soft_update_sec = rt_thread_create("ds1307",auto_update_current_datatime,RT_NULL,256, 20, 5); + ds1307.thd_soft_update_sec = rt_thread_create("ds1307",auto_update_current_datatime,RT_NULL,512, 20, 5); if (ds1307.thd_soft_update_sec) { rt_thread_startup(ds1307.thd_soft_update_sec);