Main.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /*********************************************************************************************************
  2. * 模块名称:Main.c
  3. * 摘 要:主文件,包含软硬件初始化函数和main函数
  4. * 当前版本:1.0.0
  5. * 作 者:anycrying
  6. * 完成日期:2024年04月12日
  7. * 内 容:
  8. * 注 意:注意勾选Options for Target 'Target1'->Code Generation->Use MicroLIB,否则printf无法使用
  9. **********************************************************************************************************
  10. * 取代版本:
  11. * 作 者:
  12. * 完成日期:
  13. * 修改内容:
  14. * 修改文件:
  15. *********************************************************************************************************/
  16. /*********************************************************************************************************
  17. * 包含头文件
  18. *********************************************************************************************************/
  19. #include "Main.h"
  20. //#include "gd32e230x_conf.h"
  21. #include "string.h"
  22. #include "NVIC.h"
  23. #include "SysTick.h"
  24. #include "Rcu.h"
  25. #include "Timer.h"
  26. #include "Led.h"
  27. #include "Key.h"
  28. #include "Relay.h"
  29. #include "Uart1.h"
  30. #include "mb.h"
  31. #include "IM1253E.h"
  32. #include "Wwdgt.h"
  33. #include "At24cxx.h"
  34. #include "I2c.h"
  35. #include "ReadEeprom.h"
  36. #include "Sn74hc573.h"
  37. #include "Sn74hc595.h"
  38. #include <stdbool.h>
  39. #include "ADC.h"
  40. /*********************************************************************************************************
  41. * 宏定义
  42. *********************************************************************************************************/
  43. /*********************************************************************************************************
  44. * 枚举结构体
  45. *********************************************************************************************************/
  46. DCPDU_RELAY_Para_Reg DCPDU_RL_time;
  47. DC_SUM_Para_struct_Reg DCPDU_Input;
  48. DC_Output_struct_Reg DCPDU_Output[RELAY_ChN_default];
  49. /*********************************************************************************************************
  50. * 内部变量定义
  51. *********************************************************************************************************/
  52. static uint8_t channel = 0;
  53. unsigned long int Delay_Ms_1 = 0;
  54. static uint8_t Delay_End_flag = 0;
  55. bool Load_conn_end_flag = false;
  56. bool Init_IM1253E_flag = false;
  57. bool Start_Read_Flag; //开始读取数据标志
  58. bool RL_Allon_flag = false;
  59. bool RL_Alloff_flag = false;
  60. bool Read_IM1253E_flag = false;
  61. bool Fault_stFlag_V = true;
  62. bool Wr_eeprom_begin_flag = false;
  63. bool IM1253E_OK_flag = false;
  64. static uint8_t DCPDU_SlaveAddress = 0x00;
  65. uint8_t cnt = 0;
  66. uint16_t Wr_erom_wait_cnt = 0;
  67. uint8_t Wr_eeprom_cnt = 0;
  68. uint8_t Active_state_bit = 0xff;
  69. uint8_t RL_begin_delay_flag[RELAY_ChN_default] = {0x00};
  70. uint8_t RL_ON_flag[RELAY_ChN_default] = {0x00};
  71. uint8_t RL_OFF_flag[RELAY_ChN_default] = {0x00};
  72. uint8_t RL_end_delay_flag[RELAY_ChN_default] = {0x00};
  73. uint8_t RL_last_state = 0x00;
  74. uint8_t RL_Rx_state = 0x00;
  75. uint8_t DCPDU_active_chn = RELAY_ChN_default;
  76. uint8_t Fault_state_V = 0xff;
  77. uint8_t Fault_state_I = 0;
  78. uint8_t Fault_state_Reg = 0;
  79. uint8_t Fault_state_Last = 0;
  80. float TotalWh[RELAY_ChN_default] ={0.0};
  81. uint16_t Devid_IDChalNum = 0;//设备类型和通道数
  82. uint16_t adc_data_zero[ADC_CH_N];
  83. uint32_t DCPDU_Iout_Zero[RELAY_ChN_default] = {0};
  84. uint32_t DCPDU_Modbus_Reg[DCPDU_Modbus_Reg_len] = {0};
  85. uint32_t DCPDU_Iin_Zero = 0;
  86. uint32_t DCPDU_Win_Zero = 0;
  87. extern bool Rx_Finish_flag;
  88. extern bool adc_data_ok_flag;
  89. extern uint8_t ch_cnt;
  90. extern uint8_t Eeprom_Rbuf[RELAY_para_sum + 1];
  91. extern uint8_t Rx_Data_buff_IM1253E[UART1_BUF_SIZE];
  92. extern uint32_t RL_delay_Limit[RELAY_ChN_default];
  93. extern uint32_t adc_Avg_value[ADC_CH_N];//存放各通道平均值
  94. extern uint16_t adc_original_value[ADC_CH_N];
  95. extern uint32_t Modbus_Wr_buff[Mb_Wcmd_Width][Mb_Wcmd_len];
  96. extern uint32_t RL_delay_cnt[RELAY_ChN_default];
  97. /*********************************************************************************************************
  98. * 内部函数声明
  99. *********************************************************************************************************/
  100. static void InitSoftware(void); //初始化软件相关的模块
  101. static void InitHardware(void); //初始化硬件相关的模块
  102. static void CHK_JLINK(void);
  103. static void MODbus_CMD(void);
  104. /*********************************************************************************************************
  105. * 内部函数实现
  106. *********************************************************************************************************/
  107. /*********************************************************************************************************
  108. * 函数名称:InitSoftware
  109. * 函数功能:所有的软件相关的模块初始化函数都放在此函数中
  110. * 输入参数:void
  111. * 输出参数:void
  112. * 返 回 值:void
  113. * 创建日期:2024年04月12日
  114. * 注 意:
  115. *********************************************************************************************************/
  116. static void InitSoftware(void)
  117. {
  118. eMBInit(MB_RTU, DCPDU_SlaveAddress, 0, Uart0_Baud, MB_PAR_NONE); // 初始化modbus为RTU方式,地址RelaySlaveAddress, 波特率Uart0_Baud,无校验
  119. eMBEnable(); // 使能modbus协议栈
  120. }
  121. /*********************************************************************************************************
  122. * 函数名称:InitHardware
  123. * 函数功能:所有的硬件相关的模块初始化函数都放在此函数中
  124. * 输入参数:void
  125. * 输出参数:void
  126. * 返 回 值:void
  127. * 创建日期:2024年04月12日
  128. * 注 意:
  129. *********************************************************************************************************/
  130. static void InitHardware(void)
  131. {
  132. SystemInit(); //系统初始化,函数里面默认是配置为内部晶振
  133. ViewRcuClock(); //在线调试查看系统时钟频率
  134. InitNVIC(); //初始化NVIC模块
  135. InitSysTick(); //初始化SysTick模块
  136. InitLED(); //初始化LED模块
  137. InitKey(); //初始化Key模块
  138. InitRelay(); //初始化Relay模块
  139. Init_74hc573(); //初始化74hc573锁存器
  140. Init_74hc595(); //初始化74hc595
  141. InitTimer(); //初始化Timer模块
  142. InitUART1(IM1253E_bd); //初始化UART1模块 电能计量芯片默认的波特率,偶校验
  143. InitAT24Cxx(); //初始化24C64模块
  144. ReadEeprom();
  145. Init_ADC();
  146. Delay_Ms_1 = 0;
  147. }
  148. /*********************************************************************************************************
  149. * 函数名称:CHK_JLINK
  150. * 函数功能:检测JLINK有没有插上
  151. * 输入参数:void
  152. * 输出参数:void
  153. * 返 回 值:void
  154. * 创建日期:2024年08月9日 liyuezong重构
  155. * 注 意:
  156. *********************************************************************************************************/
  157. static void CHK_JLINK(void)
  158. {
  159. uint8_t i = 0;
  160. uint8_t temp0 = 0;
  161. uint8_t temp1 = 0;
  162. uint8_t t_RL_st_old = 0;
  163. uint8_t t_RL_st_new = 0;
  164. if(gpio_input_bit_get(CHECK_JLINK_IO,CHECK_JLINK_IN) == 0)//如果有jlink接入,则不会动作继电器,直接跳出进入主函数
  165. {
  166. //1 pan duan shi fou you qing qiu
  167. temp1 = Eeprom_Rbuf[ERom_RL_ST_Addr] ^ RL_Rx_state;
  168. if(temp1 > 0)
  169. {
  170. if(RL_Allon_flag || RL_Alloff_flag)
  171. {
  172. for(i = 0;i < DCPDU_RL_time.RL_chn;i++)//所有状态清零
  173. {
  174. RL_begin_delay_flag[i] = 0;
  175. RL_end_delay_flag[i] = 0;
  176. RL_delay_Limit[i] = 0;
  177. RL_ON_flag[i] = 0;
  178. RL_OFF_flag[i] = 0;
  179. }
  180. }
  181. for(i = 0;i < DCPDU_RL_time.RL_chn;i++)
  182. {
  183. t_RL_st_old = Eeprom_Rbuf[ERom_RL_ST_Addr] >> i;
  184. t_RL_st_old = t_RL_st_old & 0x01;
  185. t_RL_st_new = RL_Rx_state >> i;
  186. t_RL_st_new = t_RL_st_new & 0x01;
  187. if(t_RL_st_old > t_RL_st_new)//off
  188. {
  189. RL_OFF_flag[i] = 1;
  190. if(RL_Alloff_flag)
  191. {
  192. RL_begin_delay_flag[i] = 1;
  193. RL_delay_Limit[i] = DCPDU_RL_time.RL_offtime[i] * 1000;//秒转成毫秒;
  194. }
  195. else
  196. {
  197. RL_end_delay_flag[i] = 1;
  198. }
  199. }
  200. else if(t_RL_st_old < t_RL_st_new)//on
  201. {
  202. RL_ON_flag[i] = 1;
  203. if(RL_Allon_flag)
  204. {
  205. RL_delay_Limit[i] = DCPDU_RL_time.RL_ontime[i] * 1000;//秒转成毫秒
  206. RL_begin_delay_flag[i] = 1;
  207. }
  208. else
  209. {
  210. RL_end_delay_flag[i] = 1;
  211. }
  212. }
  213. }
  214. RL_Alloff_flag = false;
  215. RL_Allon_flag = false;
  216. }
  217. else
  218. {
  219. if(RL_Allon_flag || RL_Alloff_flag)
  220. {
  221. RL_Allon_flag = false;
  222. RL_Alloff_flag = false;
  223. for(i = 0;i < DCPDU_RL_time.RL_chn;i++)//所有状态清零
  224. {
  225. RL_begin_delay_flag[i] = 0;
  226. RL_end_delay_flag[i] = 0;
  227. RL_delay_Limit[i] = 0;
  228. RL_ON_flag[i] = 0;
  229. RL_OFF_flag[i] = 0;
  230. }
  231. }
  232. }
  233. for(i = 0;i < DCPDU_RL_time.RL_chn;i++)
  234. {
  235. if(RL_end_delay_flag[i] > 0)
  236. {
  237. RL_delay_Limit[i] = 0;
  238. RL_begin_delay_flag[i] = 0;
  239. RL_end_delay_flag[i] = 0;
  240. temp0 = 0x01 << i;
  241. if(RL_ON_flag[i] > 0)
  242. {
  243. Relayx_Onoff_state(i,RELAY_ON);
  244. RL_ON_flag[i] = 0;
  245. DCPDU_Modbus_Reg[Mb_Dbuff_RL_ST_Addr] |= temp0;
  246. }
  247. else if(RL_OFF_flag[i] > 0)
  248. {
  249. Relayx_Onoff_state(i,RELAY_OFF);
  250. RL_OFF_flag[i] = 0;
  251. temp0 = ~temp0;
  252. DCPDU_Modbus_Reg[Mb_Dbuff_RL_ST_Addr] &= temp0;
  253. }
  254. Eeprom_Rbuf[ERom_RL_ST_Addr] = DCPDU_Modbus_Reg[Mb_Dbuff_RL_ST_Addr] & Active_state_bit;
  255. AT24CxxWriteOneByte(ERom_RL_ST_Addr,Eeprom_Rbuf[ERom_RL_ST_Addr]);
  256. Write_74hc573();
  257. }
  258. }
  259. Load_conn_end_flag = true;
  260. }
  261. RL_Rx_state = Eeprom_Rbuf[ERom_RL_ST_Addr];
  262. }
  263. /*********************************************************************************************************
  264. * 函数名称:main
  265. * 函数功能:主函数
  266. * 输入参数:void
  267. * 输出参数:void
  268. * 返 回 值:int
  269. * 创建日期:2024年04月12日
  270. * 注 意:
  271. *********************************************************************************************************/
  272. int main(void)
  273. {
  274. uint8_t i = 0;
  275. uint8_t j = 0;
  276. uint32_t temp0 = 0;
  277. uint32_t temp_v = 0;
  278. uint32_t temp_i = 0;
  279. uint32_t temp_p = 0;
  280. uint8_t temp1 = 0;
  281. uint8_t temp2 = 0;
  282. uint32_t temp_da[8] = {0};
  283. float temp_w = 0.0;
  284. cnt = 0;
  285. Wr_eeprom_cnt = 0;
  286. Wr_erom_wait_cnt = 0;
  287. Active_state_bit = 0xff;
  288. for(i = 0;i < RELAY_ChN_default;i++)
  289. {
  290. RL_begin_delay_flag[i] = 0;
  291. RL_end_delay_flag[i] = 0;
  292. TotalWh[i] = 0.0;
  293. }
  294. InitHardware();//初始化硬件相关函数
  295. DCPDU_SlaveAddress = ReadKeyValue();//读取从机的地址
  296. InitSoftware();//初始化软件相关函数
  297. Devid_IDChalNum = DCPDU_Device_ID;
  298. Devid_IDChalNum = Devid_IDChalNum << 8;
  299. Devid_IDChalNum += DCPDU_active_chn;
  300. DCPDU_Modbus_Reg[Mb_Dbuff_DevAddr] = Devid_IDChalNum;//设备型号和通道数
  301. DCPDU_RL_time.RL_chn = DCPDU_active_chn;
  302. Active_state_bit = 0xff >> (8 - DCPDU_active_chn);
  303. for(i = 0;i < RELAY_ChN_default;i++)
  304. {
  305. DCPDU_RL_time.RL_ontime[i] = DCPDU_Modbus_Reg[Mb_Dbuff_RL_Ton1_Addr + i];
  306. DCPDU_RL_time.RL_offtime[i] = DCPDU_Modbus_Reg[Mb_Dbuff_RL_Toff1_Addr + i];
  307. }
  308. gpio_bit_reset(LOCK_74hc573_IO, LOCK_74hc573_OE);
  309. Init_Dev_parameter();
  310. RL_Rx_state = Eeprom_Rbuf[ERom_RL_ST_Addr]& Active_state_bit;
  311. RL_Allon_flag = true;
  312. Eeprom_Rbuf[ERom_RL_ST_Addr]= 0 ;
  313. DCPDU_Modbus_Reg[Mb_Dbuff_RL_ST_Addr]=0;
  314. RL_last_state = 0;
  315. Fault_state_V = Fault_state_V & Active_state_bit;
  316. Fault_display(Fault_state_V);
  317. Fault_state_Last = Fault_state_V;
  318. for(i = 0;i < DCPDU_active_chn;i++)
  319. {
  320. temp0 = 4*i;
  321. TotalWh[i] = DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 3];//耗电量初始化
  322. }
  323. while(1)
  324. {
  325. CHK_JLINK();
  326. eMBPoll();
  327. if(Read_IM1253E_flag)//定时读1253
  328. {
  329. Read_IM1253E_flag = false;
  330. if(IM1253E_OK_flag)
  331. {
  332. Read_IM1253E_REG(Device_addr_IM1253E,V_REG_ADDR,8);
  333. }
  334. }
  335. if(Rx_Finish_flag)//从1253读到数,进行处理
  336. {
  337. Rx_Finish_flag = false;
  338. for(j = 0;j < 8;j++)
  339. {
  340. temp_da[j] = 0;
  341. for(i = 0;i < 4;i++)
  342. {
  343. temp1 = 8 * i;
  344. temp1 = 24 - temp1;
  345. temp2 = 4 * j;
  346. temp2 += 3;
  347. temp2 += i;
  348. temp0 = Rx_Data_buff_IM1253E[temp2] << temp1;
  349. temp_da[j] += temp0;
  350. }
  351. }
  352. DCPDU_Modbus_Reg[Mb_Dbuff_VInput_Addr] = temp_da[0] / 100;//输入电压
  353. DCPDU_Modbus_Reg[Mb_Dbuff_VInput_Addr] *= 10;//去掉第3位小数
  354. for(i = 0;i < RELAY_ChN_default;i++)
  355. {
  356. temp0 = 4 * i;
  357. DCPDU_Modbus_Reg[Mb_Dbuff_P1Output_Addr + temp0] = DCPDU_Modbus_Reg[Mb_Dbuff_VInput_Addr];//输出电压
  358. temp2 = 0x01 << i;
  359. if(DCPDU_Modbus_Reg[Mb_Dbuff_VInput_Addr] > DCPDU_Modbus_Reg[Mb_Dbuff_VLit1_max_Addr + i])
  360. {
  361. Fault_state_V |= temp2;
  362. }
  363. else
  364. {
  365. if((Fault_state_V & temp2) > 0)
  366. {
  367. temp1 = ~temp2;
  368. Fault_state_V &= temp1;
  369. }
  370. }
  371. }
  372. DCPDU_Modbus_Reg[Mb_Dbuff_IInput_Addr] = temp_da[1] / 100;
  373. DCPDU_Modbus_Reg[Mb_Dbuff_IInput_Addr] *= 10;//去掉第3位小数
  374. if(DCPDU_Modbus_Reg[Mb_Dbuff_IInput_Addr] > DCPDU_Iin_Zero)
  375. {
  376. DCPDU_Modbus_Reg[Mb_Dbuff_IInput_Addr] -= DCPDU_Iin_Zero;
  377. }
  378. else
  379. {
  380. DCPDU_Modbus_Reg[Mb_Dbuff_IInput_Addr] = 0;
  381. }
  382. DCPDU_Modbus_Reg[Mb_Dbuff_WInput_Addr] = temp_da[2] / 100;
  383. DCPDU_Modbus_Reg[Mb_Dbuff_WInput_Addr] *= 10;//去掉第3位小数
  384. if(DCPDU_Modbus_Reg[Mb_Dbuff_WInput_Addr] > DCPDU_Win_Zero)
  385. {
  386. DCPDU_Modbus_Reg[Mb_Dbuff_WInput_Addr] -= DCPDU_Win_Zero;
  387. }
  388. else
  389. {
  390. DCPDU_Modbus_Reg[Mb_Dbuff_WInput_Addr] = 0;
  391. }
  392. DCPDU_Modbus_Reg[Mb_Dbuff_KWhInput_Addr] = temp_da[3] / 100;
  393. DCPDU_Modbus_Reg[Mb_Dbuff_KWhInput_Addr] *= 10;//去掉第3位小数
  394. }
  395. if(Get1SecFlag()) //判断1s标志状态
  396. {
  397. LEDFlicker2();
  398. Clr1SecFlag(); //清除1s标志
  399. read_adc_val();//定时读取ADC的数据
  400. //输出耗电量计算。累计。
  401. for(i = 0;i < DCPDU_active_chn;i++)
  402. {
  403. temp0 = 4*i;
  404. temp_w = (float)DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 2];
  405. temp_w = temp_w / 1000.0;//转换成W,原功率是扩大了1000倍的
  406. temp_w = temp_w / 3600.0;//转换成每秒的Wh,
  407. //temp_w = temp_w / 1000.0;//转换成每秒的kWh
  408. //temp_w = temp_w * 1000.0;//扩大1000倍上传
  409. TotalWh[i] = temp_w + TotalWh[i];//累计电量,初值为0
  410. DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 3] = (uint32_t)TotalWh[i];
  411. if(DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 3] > 0xfffffffe)
  412. {
  413. TotalWh[i] = 0.0;
  414. DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 3]=(uint32_t)TotalWh[i];
  415. }
  416. //将耗电量实时写入Eeprom
  417. Eeprom_Rbuf[ERom_TotalWh1_Addr + temp0] = DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 3] >> 24;
  418. Eeprom_Rbuf[ERom_TotalWh1_Addr + temp0 + 1] = DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 3] >> 16;
  419. Eeprom_Rbuf[ERom_TotalWh1_Addr + temp0 + 2] = DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 3] >> 8;
  420. Eeprom_Rbuf[ERom_TotalWh1_Addr + temp0 + 3] = DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 3];
  421. AT24CxxWrite(ERom_TotalWh1_Addr + temp0,(Eeprom_Rbuf + ERom_TotalWh1_Addr + temp0),4);
  422. }
  423. }
  424. if(adc_data_ok_flag)//读到ADC数据,然后进行处理。
  425. {
  426. adc_data_ok_flag = false;
  427. //for(i = 0;i < ADC_CH_N;i++)
  428. for(i = 0;i < DCPDU_active_chn;i++)
  429. {
  430. temp0 = 4*i;
  431. DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 1] = DC_I_calfunction(adc_Avg_value[i]);//输出电流
  432. if(Load_conn_end_flag)//负载加载以后再检测电流
  433. {
  434. temp2 = 0x01 << i;
  435. if(DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 1] > DCPDU_Modbus_Reg[Mb_Dbuff_ILit1_max_Addr + i])
  436. {
  437. Fault_state_I |= temp2;
  438. }
  439. else
  440. {
  441. if((Fault_state_I & temp2) > 0)
  442. {
  443. temp1 = ~temp2;
  444. Fault_state_I &= temp1;
  445. }
  446. }
  447. }
  448. //计算功率//
  449. temp_v =DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr] / 10;
  450. temp_i = DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 1] / 10;
  451. temp_p = temp_v * temp_i;
  452. DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 2] = temp_p / 10;
  453. adc_Avg_value[i] = 0;
  454. }
  455. Fault_state_Reg = Fault_state_V | Fault_state_I;
  456. }
  457. if(Fault_state_Reg > 0)//故障或超阈值处理
  458. {
  459. if(Fault_state_Reg != Fault_state_Last)
  460. {
  461. Fault_display(Fault_state_Reg);
  462. Fault_state_Last = Fault_state_Reg;
  463. }
  464. }
  465. else
  466. {
  467. }
  468. MODbus_CMD();//MODBUS命令池处理。
  469. }
  470. }
  471. /*********************************************************************************************************
  472. * 函数名称:MODbus_CMD
  473. * 函数功能:modbus命令池处理
  474. * 输入参数:void
  475. * 输出参数:void
  476. * 返 回 值:void
  477. * 创建日期:2024年04月06日
  478. * 注 意:
  479. *********************************************************************************************************/
  480. void MODbus_CMD(void)
  481. {
  482. if(Modbus_Wr_buff[Wr_eeprom_cnt][0] > 0)
  483. {
  484. static uint8_t temp_data[4];
  485. static uint32_t temp_addr;
  486. if(Modbus_Wr_buff[Wr_eeprom_cnt][1] < Mb_Dbuff_VLit1_min_Addr)//设置电压最大阈值
  487. {
  488. temp_addr = Modbus_Wr_buff[Wr_eeprom_cnt][1] - Mb_Dbuff_VLit1_max_Addr;
  489. temp_addr = temp_addr << 2;
  490. temp_addr += ERom_VLit1_max_Addr;
  491. Eeprom_Rbuf[temp_addr] = Modbus_Wr_buff[Wr_eeprom_cnt][2] >> 24;
  492. Eeprom_Rbuf[temp_addr + 1] = Modbus_Wr_buff[Wr_eeprom_cnt][2] >> 16;
  493. Eeprom_Rbuf[temp_addr + 2] = Modbus_Wr_buff[Wr_eeprom_cnt][2] >> 8;
  494. Eeprom_Rbuf[temp_addr + 3] = Modbus_Wr_buff[Wr_eeprom_cnt][2];
  495. DCPDU_Modbus_Reg[Modbus_Wr_buff[Wr_eeprom_cnt][1]] = Modbus_Wr_buff[Wr_eeprom_cnt][2];
  496. AT24CxxWrite(temp_addr,(Eeprom_Rbuf + temp_addr),4);
  497. }
  498. else if(Modbus_Wr_buff[Wr_eeprom_cnt][1] < Mb_Dbuff_ILit1_max_Addr)//设置电压最小阈值
  499. {
  500. temp_addr = Modbus_Wr_buff[Wr_eeprom_cnt][1] - Mb_Dbuff_VLit1_min_Addr;
  501. temp_addr = temp_addr << 2;
  502. temp_addr += ERom_VLit1_min_Addr;
  503. Eeprom_Rbuf[temp_addr] = Modbus_Wr_buff[Wr_eeprom_cnt][2] >> 24;
  504. Eeprom_Rbuf[temp_addr + 1] = Modbus_Wr_buff[Wr_eeprom_cnt][2] >> 16;
  505. Eeprom_Rbuf[temp_addr + 2] = Modbus_Wr_buff[Wr_eeprom_cnt][2] >> 8;
  506. Eeprom_Rbuf[temp_addr + 3] = Modbus_Wr_buff[Wr_eeprom_cnt][2];
  507. DCPDU_Modbus_Reg[Modbus_Wr_buff[Wr_eeprom_cnt][1]] = Modbus_Wr_buff[Wr_eeprom_cnt][2];
  508. AT24CxxWrite(temp_addr,(Eeprom_Rbuf + temp_addr),4);
  509. }
  510. else if(Modbus_Wr_buff[Wr_eeprom_cnt][1] < Mb_Dbuff_ILit1_min_Addr)//设置电流最大阈值
  511. {
  512. temp_addr = Modbus_Wr_buff[Wr_eeprom_cnt][1] - Mb_Dbuff_ILit1_max_Addr;
  513. temp_addr = temp_addr << 2;
  514. temp_addr += ERom_ILit1_max_Addr;
  515. Eeprom_Rbuf[temp_addr] = Modbus_Wr_buff[Wr_eeprom_cnt][2] >> 24;
  516. Eeprom_Rbuf[temp_addr + 1] = Modbus_Wr_buff[Wr_eeprom_cnt][2] >> 16;
  517. Eeprom_Rbuf[temp_addr + 2] = Modbus_Wr_buff[Wr_eeprom_cnt][2] >> 8;
  518. Eeprom_Rbuf[temp_addr + 3] = Modbus_Wr_buff[Wr_eeprom_cnt][2];
  519. DCPDU_Modbus_Reg[Modbus_Wr_buff[Wr_eeprom_cnt][1]] = Modbus_Wr_buff[Wr_eeprom_cnt][2];
  520. AT24CxxWrite(temp_addr,(Eeprom_Rbuf + temp_addr),4);
  521. }
  522. else if(Modbus_Wr_buff[Wr_eeprom_cnt][1] < Mb_Dbuff_RL_ST_Addr)//设置电流最小阈值
  523. {
  524. temp_addr = Modbus_Wr_buff[Wr_eeprom_cnt][1] - Mb_Dbuff_ILit1_min_Addr;
  525. temp_addr = temp_addr << 2;
  526. temp_addr += ERom_ILit1_min_Addr;
  527. Eeprom_Rbuf[temp_addr] = Modbus_Wr_buff[Wr_eeprom_cnt][2] >> 24;
  528. Eeprom_Rbuf[temp_addr + 1] = Modbus_Wr_buff[Wr_eeprom_cnt][2] >> 16;
  529. Eeprom_Rbuf[temp_addr + 2] = Modbus_Wr_buff[Wr_eeprom_cnt][2] >> 8;
  530. Eeprom_Rbuf[temp_addr + 3] = Modbus_Wr_buff[Wr_eeprom_cnt][2];
  531. DCPDU_Modbus_Reg[Modbus_Wr_buff[Wr_eeprom_cnt][1]] = Modbus_Wr_buff[Wr_eeprom_cnt][2];
  532. AT24CxxWrite(temp_addr,(Eeprom_Rbuf + temp_addr),4);
  533. }
  534. else if(Modbus_Wr_buff[Wr_eeprom_cnt][1] == Mb_Dbuff_RL_ST_Addr)//设置继电器状态
  535. {
  536. RL_Rx_state = Modbus_Wr_buff[Wr_eeprom_cnt][2] & Active_state_bit;
  537. //Eeprom_Rbuf[ERom_RL_ST_Addr] = Modbus_Wr_buff[Wr_eeprom_cnt][2] & Active_state_bit;
  538. //AT24CxxWriteOneByte(ERom_RL_ST_Addr,Eeprom_Rbuf[ERom_RL_ST_Addr]);
  539. }
  540. else if(Modbus_Wr_buff[Wr_eeprom_cnt][1] == Mb_Dbuff_RL_Allon_Addr)//设置继电器全开
  541. {
  542. RL_Rx_state = 0xff & Active_state_bit;
  543. RL_Allon_flag = true;
  544. //Eeprom_Rbuf[ERom_RL_ST_Addr] = 0xff & Active_state_bit;
  545. //AT24CxxWriteOneByte(ERom_RL_ST_Addr,Eeprom_Rbuf[ERom_RL_ST_Addr]);
  546. }
  547. else if(Modbus_Wr_buff[Wr_eeprom_cnt][1] == Mb_Dbuff_RL_Alloff_Addr)//设置继电器全关
  548. {
  549. RL_Rx_state = 0x00;
  550. RL_Alloff_flag = true;
  551. //Eeprom_Rbuf[ERom_RL_ST_Addr] = 0x00;
  552. //AT24CxxWriteOneByte(ERom_RL_ST_Addr,Eeprom_Rbuf[ERom_RL_ST_Addr]);
  553. }
  554. else if(Modbus_Wr_buff[Wr_eeprom_cnt][1] == Mb_Dbuff_RLft_ST_Addr)//设置故障状态
  555. {
  556. temp_addr = Modbus_Wr_buff[Wr_eeprom_cnt][1] - Mb_Dbuff_RLft_ST_Addr;
  557. temp_addr += ERom_RLft_ST_Addr;
  558. Eeprom_Rbuf[temp_addr] = Modbus_Wr_buff[Wr_eeprom_cnt][2] & Active_state_bit;
  559. }
  560. else if(Modbus_Wr_buff[Wr_eeprom_cnt][1] < Mb_Dbuff_RL_Toff1_Addr)//设置开启延时
  561. {
  562. temp_addr = Modbus_Wr_buff[Wr_eeprom_cnt][1] - Mb_Dbuff_RL_Ton1_Addr;
  563. temp_addr += ERom_RL_Ton1_Addr;
  564. Eeprom_Rbuf[temp_addr] = Modbus_Wr_buff[Wr_eeprom_cnt][2];
  565. DCPDU_Modbus_Reg[Modbus_Wr_buff[Wr_eeprom_cnt][1]] = Eeprom_Rbuf[temp_addr];
  566. DCPDU_RL_time.RL_ontime[Modbus_Wr_buff[Wr_eeprom_cnt][1] - Mb_Dbuff_RL_Ton1_Addr] = Eeprom_Rbuf[temp_addr];
  567. AT24CxxWriteOneByte(temp_addr,Eeprom_Rbuf[temp_addr]);
  568. }
  569. else if(Modbus_Wr_buff[Wr_eeprom_cnt][1] < Mb_Dbuff_RL_Allon_Addr)//设置继电器关断延时
  570. {
  571. temp_addr = Modbus_Wr_buff[Wr_eeprom_cnt][1] - Mb_Dbuff_RL_Toff1_Addr;
  572. temp_addr += ERom_RL_Toff1_Addr;
  573. Eeprom_Rbuf[temp_addr] = Modbus_Wr_buff[Wr_eeprom_cnt][2];
  574. DCPDU_Modbus_Reg[Modbus_Wr_buff[Wr_eeprom_cnt][1]] = Eeprom_Rbuf[temp_addr];
  575. DCPDU_RL_time.RL_offtime[Modbus_Wr_buff[Wr_eeprom_cnt][1] - Mb_Dbuff_RL_Toff1_Addr] = Eeprom_Rbuf[temp_addr];
  576. AT24CxxWriteOneByte(temp_addr,Eeprom_Rbuf[temp_addr]);
  577. }
  578. Modbus_Wr_buff[Wr_eeprom_cnt][0] = 0;
  579. Wr_eeprom_begin_flag = true;
  580. }
  581. Wr_eeprom_cnt++;
  582. if(Wr_eeprom_cnt >= Mb_Wcmd_Width)
  583. {
  584. Wr_eeprom_cnt = 0;
  585. }
  586. }
  587. /*********************************************************************************************************
  588. * 函数名称:Init_Dev_parameter
  589. * 函数功能:初始化模块内部参数
  590. * 输入参数:void
  591. * 输出参数:void
  592. * 返 回 值:void
  593. * 创建日期:2024年04月06日
  594. * 注 意:
  595. *********************************************************************************************************/
  596. void Init_Dev_parameter(void)
  597. {
  598. uint8_t i = 0;
  599. uint8_t j = 0;
  600. uint8_t temp1 = 0;
  601. uint8_t temp2 = 0;
  602. uint32_t temp0 = 0;
  603. uint32_t temp_da[8] = {0};
  604. while(!dma_flag_get(DMA_CH0,DMA_FLAG_FTF));
  605. dma_flag_clear(DMA_CH0,DMA_FLAG_FTF);
  606. for(i = 0;i < 8;i++)
  607. {
  608. adc_data_zero[i] = adc_original_value[i];
  609. }
  610. Init_IM1253E_flag = true;
  611. while(Init_IM1253E_flag)
  612. {
  613. if(Read_IM1253E_flag)
  614. {
  615. Read_IM1253E_flag = false;
  616. Read_IM1253E_REG(Device_addr_IM1253E,V_REG_ADDR,8);//Device_addr_IM1253E
  617. }
  618. if(Rx_Finish_flag)
  619. {
  620. Rx_Finish_flag = false;
  621. for(j = 0;j < 8;j++)
  622. {
  623. temp_da[j] = 0;
  624. for(i = 0;i < 4;i++)
  625. {
  626. temp1 = 8 * i;
  627. temp1 = 24 - temp1;
  628. temp2 = 4 * j;
  629. temp2 += 3;
  630. temp2 += i;
  631. temp0 = Rx_Data_buff_IM1253E[temp2] << temp1;
  632. temp_da[j] += temp0;
  633. }
  634. }
  635. DCPDU_Iin_Zero = temp_da[1] / 10;
  636. DCPDU_Win_Zero = temp_da[2] / 10;
  637. DCPDU_Modbus_Reg[Mb_Dbuff_VInput_Addr] = temp_da[0] / 10;//输入电压
  638. for(i = 0;i < RELAY_ChN_default;i++)
  639. {
  640. temp0 = 4 * i;
  641. DCPDU_Modbus_Reg[Mb_Dbuff_P1Output_Addr + temp0] = DCPDU_Modbus_Reg[Mb_Dbuff_VInput_Addr];//输出电压
  642. temp2 = 0x01 << i;
  643. if((DCPDU_Modbus_Reg[Mb_Dbuff_VInput_Addr] > DCPDU_Modbus_Reg[Mb_Dbuff_VLit1_max_Addr + i]) || (DCPDU_Modbus_Reg[Mb_Dbuff_VInput_Addr] < DCPDU_Modbus_Reg[Mb_Dbuff_VLit1_min_Addr + i]))
  644. {
  645. Fault_state_V |= temp2;
  646. }
  647. else
  648. {
  649. if((Fault_state_V & temp2) > 0)
  650. {
  651. temp1 = ~temp2;
  652. Fault_state_V &= temp1;
  653. }
  654. }
  655. }
  656. Init_IM1253E_flag = false;
  657. IM1253E_OK_flag = true;
  658. }
  659. }
  660. }
  661. /*freemodbus作者使用了assert,故增加如下代码,同时
  662. *options for target 对话框,target页面,勾选Use MicroLib
  663. */
  664. #ifdef USE_FULL_ASSERT
  665. /**
  666. * @brief Reports the name of the source file and the source line number
  667. * where the assert_param error has occurred.
  668. * @param file: pointer to the source file name
  669. * @param line: assert_param error line source number
  670. * @retval None
  671. */
  672. void assert_failed(uint8_t* file, uint32_t line)
  673. {
  674. while (1)
  675. {
  676. }
  677. }
  678. #else
  679. void __aeabi_assert(const char * x1, const char * x2, int x3)
  680. {
  681. (void)x3;
  682. }
  683. #endif