Main - 副本 (3).c 24 KB

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