| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719 |
- /*********************************************************************************************************
- * 模块名称:Main.c
- * 摘 要:主文件,包含软硬件初始化函数和main函数
- * 当前版本:1.0.0
- * 作 者:anycrying
- * 完成日期:2024年04月12日
- * 内 容:
- * 注 意:注意勾选Options for Target 'Target1'->Code Generation->Use MicroLIB,否则printf无法使用
- **********************************************************************************************************
- * 取代版本:
- * 作 者:
- * 完成日期:
- * 修改内容:
- * 修改文件:
- *********************************************************************************************************/
- /*********************************************************************************************************
- * 包含头文件
- *********************************************************************************************************/
- #include "Main.h"
- //#include "gd32e230x_conf.h"
- #include "string.h"
- #include "NVIC.h"
- #include "SysTick.h"
- #include "Rcu.h"
- #include "Timer.h"
- #include "Led.h"
- #include "Key.h"
- #include "Relay.h"
- #include "Uart1.h"
- #include "mb.h"
- #include "IM1253E.h"
- #include "Wwdgt.h"
- #include "At24cxx.h"
- #include "I2c.h"
- #include "ReadEeprom.h"
- #include "Sn74hc573.h"
- #include "Sn74hc595.h"
- #include <stdbool.h>
- #include "ADC.h"
- /*********************************************************************************************************
- * 宏定义
- *********************************************************************************************************/
- /*********************************************************************************************************
- * 枚举结构体
- *********************************************************************************************************/
- DCPDU_RELAY_Para_Reg DCPDU_RL_time;
- DC_SUM_Para_struct_Reg DCPDU_Input;
- DC_Output_struct_Reg DCPDU_Output[RELAY_ChN_default];
- /*********************************************************************************************************
- * 内部变量定义
- *********************************************************************************************************/
- static uint8_t channel = 0;
- unsigned long int Delay_Ms_1 = 0;
- static uint8_t Delay_End_flag = 0;
- bool Load_conn_end_flag = false;
- bool Init_IM1253E_flag = false;
- bool Start_Read_Flag; //开始读取数据标志
- bool RL_Allon_flag = false;
- bool RL_Alloff_flag = false;
- bool Read_IM1253E_flag = false;
- bool Fault_stFlag_V = true;
- bool Wr_eeprom_begin_flag = false;
- bool IM1253E_OK_flag = false;
- static uint8_t DCPDU_SlaveAddress = 0x00;
- uint8_t cnt = 0;
- uint16_t Wr_erom_wait_cnt = 0;
- uint8_t Wr_eeprom_cnt = 0;
- uint8_t Active_state_bit = 0xff;
- uint8_t RL_begin_delay_flag[RELAY_ChN_default] = {0x00};
- uint8_t RL_ON_flag[RELAY_ChN_default] = {0x00};
- uint8_t RL_OFF_flag[RELAY_ChN_default] = {0x00};
- uint8_t RL_end_delay_flag[RELAY_ChN_default] = {0x00};
- uint8_t RL_last_state = 0x00;
- uint8_t RL_Rx_state = 0x00;
- uint8_t DCPDU_active_chn = RELAY_ChN_default;
- uint8_t Fault_state_V = 0xff;
- uint8_t Fault_state_I = 0;
- uint8_t Fault_state_Reg = 0;
- uint8_t Fault_state_Last = 0;
- float TotalWh[RELAY_ChN_default] ={0.0};
- uint16_t Devid_IDChalNum = 0;//设备类型和通道数
- uint16_t adc_data_zero[ADC_CH_N];
- uint32_t DCPDU_Iout_Zero[RELAY_ChN_default] = {0};
- uint32_t DCPDU_Modbus_Reg[DCPDU_Modbus_Reg_len] = {0};
- uint32_t DCPDU_Iin_Zero = 0;
- uint32_t DCPDU_Win_Zero = 0;
- extern bool Rx_Finish_flag;
- extern bool adc_data_ok_flag;
- extern uint8_t ch_cnt;
- extern uint8_t Eeprom_Rbuf[RELAY_para_sum + 1];
- extern uint8_t Rx_Data_buff_IM1253E[UART1_BUF_SIZE];
- extern uint32_t RL_delay_Limit[RELAY_ChN_default];
- extern uint32_t adc_Avg_value[ADC_CH_N];//存放各通道平均值
- extern uint16_t adc_original_value[ADC_CH_N];
- extern uint32_t Modbus_Wr_buff[Mb_Wcmd_Width][Mb_Wcmd_len];
- extern uint32_t RL_delay_cnt[RELAY_ChN_default];
- /*********************************************************************************************************
- * 内部函数声明
- *********************************************************************************************************/
- static void InitSoftware(void); //初始化软件相关的模块
- static void InitHardware(void); //初始化硬件相关的模块
- static void CHK_JLINK(void);
- static void MODbus_CMD(void);
- /*********************************************************************************************************
- * 内部函数实现
- *********************************************************************************************************/
- /*********************************************************************************************************
- * 函数名称:InitSoftware
- * 函数功能:所有的软件相关的模块初始化函数都放在此函数中
- * 输入参数:void
- * 输出参数:void
- * 返 回 值:void
- * 创建日期:2024年04月12日
- * 注 意:
- *********************************************************************************************************/
- static void InitSoftware(void)
- {
- eMBInit(MB_RTU, DCPDU_SlaveAddress, 0, Uart0_Baud, MB_PAR_NONE); // 初始化modbus为RTU方式,地址RelaySlaveAddress, 波特率Uart0_Baud,无校验
- eMBEnable(); // 使能modbus协议栈
-
- }
- /*********************************************************************************************************
- * 函数名称:InitHardware
- * 函数功能:所有的硬件相关的模块初始化函数都放在此函数中
- * 输入参数:void
- * 输出参数:void
- * 返 回 值:void
- * 创建日期:2024年04月12日
- * 注 意:
- *********************************************************************************************************/
- static void InitHardware(void)
- {
- SystemInit(); //系统初始化,函数里面默认是配置为内部晶振
- ViewRcuClock(); //在线调试查看系统时钟频率
- InitNVIC(); //初始化NVIC模块
- InitSysTick(); //初始化SysTick模块
- InitLED(); //初始化LED模块
- InitKey(); //初始化Key模块
- InitRelay(); //初始化Relay模块
- Init_74hc573(); //初始化74hc573锁存器
- Init_74hc595(); //初始化74hc595
- InitTimer(); //初始化Timer模块
- InitUART1(IM1253E_bd); //初始化UART1模块 电能计量芯片默认的波特率,偶校验
- InitAT24Cxx(); //初始化24C64模块
- ReadEeprom();
- Init_ADC();
- Delay_Ms_1 = 0;
- }
- /*********************************************************************************************************
- * 函数名称:CHK_JLINK
- * 函数功能:检测JLINK有没有插上
- * 输入参数:void
- * 输出参数:void
- * 返 回 值:void
- * 创建日期:2024年08月9日 liyuezong重构
- * 注 意:
- *********************************************************************************************************/
- static void CHK_JLINK(void)
- {
- uint8_t i = 0;
- uint8_t temp0 = 0;
- uint8_t temp1 = 0;
- uint8_t t_RL_st_old = 0;
- uint8_t t_RL_st_new = 0;
- if(gpio_input_bit_get(CHECK_JLINK_IO,CHECK_JLINK_IN) == 0)//如果有jlink接入,则不会动作继电器,直接跳出进入主函数
- {
- //1 pan duan shi fou you qing qiu
- temp1 = Eeprom_Rbuf[ERom_RL_ST_Addr] ^ RL_Rx_state;
- if(temp1 > 0)
- {
- if(RL_Allon_flag || RL_Alloff_flag)
- {
- for(i = 0;i < DCPDU_RL_time.RL_chn;i++)//所有状态清零
- {
- RL_begin_delay_flag[i] = 0;
- RL_end_delay_flag[i] = 0;
- RL_delay_Limit[i] = 0;
- RL_ON_flag[i] = 0;
- RL_OFF_flag[i] = 0;
- }
- }
-
- for(i = 0;i < DCPDU_RL_time.RL_chn;i++)
- {
- t_RL_st_old = Eeprom_Rbuf[ERom_RL_ST_Addr] >> i;
- t_RL_st_old = t_RL_st_old & 0x01;
- t_RL_st_new = RL_Rx_state >> i;
- t_RL_st_new = t_RL_st_new & 0x01;
-
- if(t_RL_st_old > t_RL_st_new)//off
- {
- RL_OFF_flag[i] = 1;
- if(RL_Alloff_flag)
- {
- RL_begin_delay_flag[i] = 1;
- RL_delay_Limit[i] = DCPDU_RL_time.RL_offtime[i] * 1000;//秒转成毫秒;
- }
- else
- {
- RL_end_delay_flag[i] = 1;
- }
- }
- else if(t_RL_st_old < t_RL_st_new)//on
- {
- RL_ON_flag[i] = 1;
- if(RL_Allon_flag)
- {
- RL_delay_Limit[i] = DCPDU_RL_time.RL_ontime[i] * 1000;//秒转成毫秒
- RL_begin_delay_flag[i] = 1;
- }
- else
- {
- RL_end_delay_flag[i] = 1;
- }
- }
- }
- RL_Alloff_flag = false;
- RL_Allon_flag = false;
- }
- else
- {
- if(RL_Allon_flag || RL_Alloff_flag)
- {
- RL_Allon_flag = false;
- RL_Alloff_flag = false;
- for(i = 0;i < DCPDU_RL_time.RL_chn;i++)//所有状态清零
- {
- RL_begin_delay_flag[i] = 0;
- RL_end_delay_flag[i] = 0;
- RL_delay_Limit[i] = 0;
- RL_ON_flag[i] = 0;
- RL_OFF_flag[i] = 0;
- }
- }
- }
-
- for(i = 0;i < DCPDU_RL_time.RL_chn;i++)
- {
- if(RL_end_delay_flag[i] > 0)
- {
- RL_delay_Limit[i] = 0;
- RL_begin_delay_flag[i] = 0;
- RL_end_delay_flag[i] = 0;
- temp0 = 0x01 << i;
- if(RL_ON_flag[i] > 0)
- {
- Relayx_Onoff_state(i,RELAY_ON);
- RL_ON_flag[i] = 0;
- DCPDU_Modbus_Reg[Mb_Dbuff_RL_ST_Addr] |= temp0;
- }
- else if(RL_OFF_flag[i] > 0)
- {
- Relayx_Onoff_state(i,RELAY_OFF);
- RL_OFF_flag[i] = 0;
- temp0 = ~temp0;
- DCPDU_Modbus_Reg[Mb_Dbuff_RL_ST_Addr] &= temp0;
- }
- Eeprom_Rbuf[ERom_RL_ST_Addr] = DCPDU_Modbus_Reg[Mb_Dbuff_RL_ST_Addr] & Active_state_bit;
- AT24CxxWriteOneByte(ERom_RL_ST_Addr,Eeprom_Rbuf[ERom_RL_ST_Addr]);
- Write_74hc573();
- }
- }
- Load_conn_end_flag = true;
- }
- RL_Rx_state = Eeprom_Rbuf[ERom_RL_ST_Addr];
- }
- /*********************************************************************************************************
- * 函数名称:main
- * 函数功能:主函数
- * 输入参数:void
- * 输出参数:void
- * 返 回 值:int
- * 创建日期:2024年04月12日
- * 注 意:
- *********************************************************************************************************/
- int main(void)
- {
- uint8_t i = 0;
- uint8_t j = 0;
- uint32_t temp0 = 0;
- uint32_t temp_v = 0;
- uint32_t temp_i = 0;
- uint32_t temp_p = 0;
- uint8_t temp1 = 0;
- uint8_t temp2 = 0;
- uint32_t temp_da[8] = {0};
- float temp_w = 0.0;
- cnt = 0;
- Wr_eeprom_cnt = 0;
- Wr_erom_wait_cnt = 0;
- Active_state_bit = 0xff;
- for(i = 0;i < RELAY_ChN_default;i++)
- {
- RL_begin_delay_flag[i] = 0;
- RL_end_delay_flag[i] = 0;
- TotalWh[i] = 0.0;
- }
- InitHardware();//初始化硬件相关函数
- DCPDU_SlaveAddress = ReadKeyValue();//读取从机的地址
- InitSoftware();//初始化软件相关函数
-
- Devid_IDChalNum = DCPDU_Device_ID;
- Devid_IDChalNum = Devid_IDChalNum << 8;
- Devid_IDChalNum += DCPDU_active_chn;
- DCPDU_Modbus_Reg[Mb_Dbuff_DevAddr] = Devid_IDChalNum;//设备型号和通道数
-
- DCPDU_RL_time.RL_chn = DCPDU_active_chn;
- Active_state_bit = 0xff >> (8 - DCPDU_active_chn);
-
- for(i = 0;i < RELAY_ChN_default;i++)
- {
- DCPDU_RL_time.RL_ontime[i] = DCPDU_Modbus_Reg[Mb_Dbuff_RL_Ton1_Addr + i];
- DCPDU_RL_time.RL_offtime[i] = DCPDU_Modbus_Reg[Mb_Dbuff_RL_Toff1_Addr + i];
- }
- gpio_bit_reset(LOCK_74hc573_IO, LOCK_74hc573_OE);
-
- Init_Dev_parameter();
-
- RL_Rx_state = Eeprom_Rbuf[ERom_RL_ST_Addr]& Active_state_bit;
- RL_Allon_flag = true;
-
- Eeprom_Rbuf[ERom_RL_ST_Addr]= 0 ;
- DCPDU_Modbus_Reg[Mb_Dbuff_RL_ST_Addr]=0;
- RL_last_state = 0;
- Fault_state_V = Fault_state_V & Active_state_bit;
- Fault_display(Fault_state_V);
- Fault_state_Last = Fault_state_V;
- for(i = 0;i < DCPDU_active_chn;i++)
- {
- temp0 = 4*i;
- TotalWh[i] = DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 3];//耗电量初始化
- }
- while(1)
- {
- CHK_JLINK();
- eMBPoll();
- if(Read_IM1253E_flag)//定时读1253
- {
- Read_IM1253E_flag = false;
- if(IM1253E_OK_flag)
- {
- Read_IM1253E_REG(Device_addr_IM1253E,V_REG_ADDR,8);
- }
- }
- if(Rx_Finish_flag)//从1253读到数,进行处理
- {
- Rx_Finish_flag = false;
- for(j = 0;j < 8;j++)
- {
- temp_da[j] = 0;
- for(i = 0;i < 4;i++)
- {
- temp1 = 8 * i;
- temp1 = 24 - temp1;
- temp2 = 4 * j;
- temp2 += 3;
- temp2 += i;
- temp0 = Rx_Data_buff_IM1253E[temp2] << temp1;
- temp_da[j] += temp0;
- }
- }
- DCPDU_Modbus_Reg[Mb_Dbuff_VInput_Addr] = temp_da[0] / 100;//输入电压
- DCPDU_Modbus_Reg[Mb_Dbuff_VInput_Addr] *= 10;//去掉第3位小数
- for(i = 0;i < RELAY_ChN_default;i++)
- {
- temp0 = 4 * i;
- DCPDU_Modbus_Reg[Mb_Dbuff_P1Output_Addr + temp0] = DCPDU_Modbus_Reg[Mb_Dbuff_VInput_Addr];//输出电压
- temp2 = 0x01 << i;
- if(DCPDU_Modbus_Reg[Mb_Dbuff_VInput_Addr] > DCPDU_Modbus_Reg[Mb_Dbuff_VLit1_max_Addr + i])
- {
- Fault_state_V |= temp2;
- }
- else
- {
- if((Fault_state_V & temp2) > 0)
- {
- temp1 = ~temp2;
- Fault_state_V &= temp1;
- }
- }
- }
-
- DCPDU_Modbus_Reg[Mb_Dbuff_IInput_Addr] = temp_da[1] / 100;
- DCPDU_Modbus_Reg[Mb_Dbuff_IInput_Addr] *= 10;//去掉第3位小数
- if(DCPDU_Modbus_Reg[Mb_Dbuff_IInput_Addr] > DCPDU_Iin_Zero)
- {
- DCPDU_Modbus_Reg[Mb_Dbuff_IInput_Addr] -= DCPDU_Iin_Zero;
- }
- else
- {
- DCPDU_Modbus_Reg[Mb_Dbuff_IInput_Addr] = 0;
- }
-
- DCPDU_Modbus_Reg[Mb_Dbuff_WInput_Addr] = temp_da[2] / 100;
- DCPDU_Modbus_Reg[Mb_Dbuff_WInput_Addr] *= 10;//去掉第3位小数
- if(DCPDU_Modbus_Reg[Mb_Dbuff_WInput_Addr] > DCPDU_Win_Zero)
- {
- DCPDU_Modbus_Reg[Mb_Dbuff_WInput_Addr] -= DCPDU_Win_Zero;
- }
- else
- {
- DCPDU_Modbus_Reg[Mb_Dbuff_WInput_Addr] = 0;
- }
-
- DCPDU_Modbus_Reg[Mb_Dbuff_KWhInput_Addr] = temp_da[3] / 100;
- DCPDU_Modbus_Reg[Mb_Dbuff_KWhInput_Addr] *= 10;//去掉第3位小数
- }
- if(Get1SecFlag()) //判断1s标志状态
- {
- LEDFlicker2();
- Clr1SecFlag(); //清除1s标志
- read_adc_val();//定时读取ADC的数据
- //输出耗电量计算。累计。
- for(i = 0;i < DCPDU_active_chn;i++)
- {
- temp0 = 4*i;
- temp_w = (float)DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 2];
- temp_w = temp_w / 1000.0;//转换成W,原功率是扩大了1000倍的
- temp_w = temp_w / 3600.0;//转换成每秒的Wh,
- //temp_w = temp_w / 1000.0;//转换成每秒的kWh
- //temp_w = temp_w * 1000.0;//扩大1000倍上传
- TotalWh[i] = temp_w + TotalWh[i];//累计电量,初值为0
- DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 3] = (uint32_t)TotalWh[i];
- if(DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 3] > 0xfffffffe)
- {
- TotalWh[i] = 0.0;
- DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 3]=(uint32_t)TotalWh[i];
- }
-
- //将耗电量实时写入Eeprom
- Eeprom_Rbuf[ERom_TotalWh1_Addr + temp0] = DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 3] >> 24;
- Eeprom_Rbuf[ERom_TotalWh1_Addr + temp0 + 1] = DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 3] >> 16;
- Eeprom_Rbuf[ERom_TotalWh1_Addr + temp0 + 2] = DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 3] >> 8;
- Eeprom_Rbuf[ERom_TotalWh1_Addr + temp0 + 3] = DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 3];
- AT24CxxWrite(ERom_TotalWh1_Addr + temp0,(Eeprom_Rbuf + ERom_TotalWh1_Addr + temp0),4);
- }
- }
- if(adc_data_ok_flag)//读到ADC数据,然后进行处理。
- {
- adc_data_ok_flag = false;
- //for(i = 0;i < ADC_CH_N;i++)
- for(i = 0;i < DCPDU_active_chn;i++)
- {
- temp0 = 4*i;
- DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 1] = DC_I_calfunction(adc_Avg_value[i]);//输出电流
-
- if(Load_conn_end_flag)//负载加载以后再检测电流
- {
- temp2 = 0x01 << i;
- if(DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 1] > DCPDU_Modbus_Reg[Mb_Dbuff_ILit1_max_Addr + i])
- {
- Fault_state_I |= temp2;
- }
- else
- {
- if((Fault_state_I & temp2) > 0)
- {
- temp1 = ~temp2;
- Fault_state_I &= temp1;
- }
- }
- }
- //计算功率//
-
- temp_v =DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr] / 10;
- temp_i = DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 1] / 10;
- temp_p = temp_v * temp_i;
- DCPDU_Modbus_Reg[temp0 + Mb_Dbuff_P1Output_Addr + 2] = temp_p / 10;
- adc_Avg_value[i] = 0;
- }
- Fault_state_Reg = Fault_state_V | Fault_state_I;
- }
-
- if(Fault_state_Reg > 0)//故障或超阈值处理
- {
- if(Fault_state_Reg != Fault_state_Last)
- {
- Fault_display(Fault_state_Reg);
- Fault_state_Last = Fault_state_Reg;
- }
- }
- else
- {
- }
- MODbus_CMD();//MODBUS命令池处理。
- }
- }
- /*********************************************************************************************************
- * 函数名称:MODbus_CMD
- * 函数功能:modbus命令池处理
- * 输入参数:void
- * 输出参数:void
- * 返 回 值:void
- * 创建日期:2024年04月06日
- * 注 意:
- *********************************************************************************************************/
- void MODbus_CMD(void)
- {
- if(Modbus_Wr_buff[Wr_eeprom_cnt][0] > 0)
- {
- static uint8_t temp_data[4];
- static uint32_t temp_addr;
- if(Modbus_Wr_buff[Wr_eeprom_cnt][1] < Mb_Dbuff_VLit1_min_Addr)//设置电压最大阈值
- {
- temp_addr = Modbus_Wr_buff[Wr_eeprom_cnt][1] - Mb_Dbuff_VLit1_max_Addr;
- temp_addr = temp_addr << 2;
- temp_addr += ERom_VLit1_max_Addr;
- Eeprom_Rbuf[temp_addr] = Modbus_Wr_buff[Wr_eeprom_cnt][2] >> 24;
- Eeprom_Rbuf[temp_addr + 1] = Modbus_Wr_buff[Wr_eeprom_cnt][2] >> 16;
- Eeprom_Rbuf[temp_addr + 2] = Modbus_Wr_buff[Wr_eeprom_cnt][2] >> 8;
- Eeprom_Rbuf[temp_addr + 3] = Modbus_Wr_buff[Wr_eeprom_cnt][2];
- DCPDU_Modbus_Reg[Modbus_Wr_buff[Wr_eeprom_cnt][1]] = Modbus_Wr_buff[Wr_eeprom_cnt][2];
- AT24CxxWrite(temp_addr,(Eeprom_Rbuf + temp_addr),4);
- }
- else if(Modbus_Wr_buff[Wr_eeprom_cnt][1] < Mb_Dbuff_ILit1_max_Addr)//设置电压最小阈值
- {
- temp_addr = Modbus_Wr_buff[Wr_eeprom_cnt][1] - Mb_Dbuff_VLit1_min_Addr;
- temp_addr = temp_addr << 2;
- temp_addr += ERom_VLit1_min_Addr;
- Eeprom_Rbuf[temp_addr] = Modbus_Wr_buff[Wr_eeprom_cnt][2] >> 24;
- Eeprom_Rbuf[temp_addr + 1] = Modbus_Wr_buff[Wr_eeprom_cnt][2] >> 16;
- Eeprom_Rbuf[temp_addr + 2] = Modbus_Wr_buff[Wr_eeprom_cnt][2] >> 8;
- Eeprom_Rbuf[temp_addr + 3] = Modbus_Wr_buff[Wr_eeprom_cnt][2];
- DCPDU_Modbus_Reg[Modbus_Wr_buff[Wr_eeprom_cnt][1]] = Modbus_Wr_buff[Wr_eeprom_cnt][2];
- AT24CxxWrite(temp_addr,(Eeprom_Rbuf + temp_addr),4);
- }
- else if(Modbus_Wr_buff[Wr_eeprom_cnt][1] < Mb_Dbuff_ILit1_min_Addr)//设置电流最大阈值
- {
- temp_addr = Modbus_Wr_buff[Wr_eeprom_cnt][1] - Mb_Dbuff_ILit1_max_Addr;
- temp_addr = temp_addr << 2;
- temp_addr += ERom_ILit1_max_Addr;
- Eeprom_Rbuf[temp_addr] = Modbus_Wr_buff[Wr_eeprom_cnt][2] >> 24;
- Eeprom_Rbuf[temp_addr + 1] = Modbus_Wr_buff[Wr_eeprom_cnt][2] >> 16;
- Eeprom_Rbuf[temp_addr + 2] = Modbus_Wr_buff[Wr_eeprom_cnt][2] >> 8;
- Eeprom_Rbuf[temp_addr + 3] = Modbus_Wr_buff[Wr_eeprom_cnt][2];
- DCPDU_Modbus_Reg[Modbus_Wr_buff[Wr_eeprom_cnt][1]] = Modbus_Wr_buff[Wr_eeprom_cnt][2];
- AT24CxxWrite(temp_addr,(Eeprom_Rbuf + temp_addr),4);
- }
- else if(Modbus_Wr_buff[Wr_eeprom_cnt][1] < Mb_Dbuff_RL_ST_Addr)//设置电流最小阈值
- {
- temp_addr = Modbus_Wr_buff[Wr_eeprom_cnt][1] - Mb_Dbuff_ILit1_min_Addr;
- temp_addr = temp_addr << 2;
- temp_addr += ERom_ILit1_min_Addr;
- Eeprom_Rbuf[temp_addr] = Modbus_Wr_buff[Wr_eeprom_cnt][2] >> 24;
- Eeprom_Rbuf[temp_addr + 1] = Modbus_Wr_buff[Wr_eeprom_cnt][2] >> 16;
- Eeprom_Rbuf[temp_addr + 2] = Modbus_Wr_buff[Wr_eeprom_cnt][2] >> 8;
- Eeprom_Rbuf[temp_addr + 3] = Modbus_Wr_buff[Wr_eeprom_cnt][2];
- DCPDU_Modbus_Reg[Modbus_Wr_buff[Wr_eeprom_cnt][1]] = Modbus_Wr_buff[Wr_eeprom_cnt][2];
- AT24CxxWrite(temp_addr,(Eeprom_Rbuf + temp_addr),4);
- }
- else if(Modbus_Wr_buff[Wr_eeprom_cnt][1] == Mb_Dbuff_RL_ST_Addr)//设置继电器状态
- {
- RL_Rx_state = Modbus_Wr_buff[Wr_eeprom_cnt][2] & Active_state_bit;
- //Eeprom_Rbuf[ERom_RL_ST_Addr] = Modbus_Wr_buff[Wr_eeprom_cnt][2] & Active_state_bit;
- //AT24CxxWriteOneByte(ERom_RL_ST_Addr,Eeprom_Rbuf[ERom_RL_ST_Addr]);
-
- }
- else if(Modbus_Wr_buff[Wr_eeprom_cnt][1] == Mb_Dbuff_RL_Allon_Addr)//设置继电器全开
- {
- RL_Rx_state = 0xff & Active_state_bit;
- RL_Allon_flag = true;
- //Eeprom_Rbuf[ERom_RL_ST_Addr] = 0xff & Active_state_bit;
- //AT24CxxWriteOneByte(ERom_RL_ST_Addr,Eeprom_Rbuf[ERom_RL_ST_Addr]);
- }
- else if(Modbus_Wr_buff[Wr_eeprom_cnt][1] == Mb_Dbuff_RL_Alloff_Addr)//设置继电器全关
- {
- RL_Rx_state = 0x00;
- RL_Alloff_flag = true;
- //Eeprom_Rbuf[ERom_RL_ST_Addr] = 0x00;
- //AT24CxxWriteOneByte(ERom_RL_ST_Addr,Eeprom_Rbuf[ERom_RL_ST_Addr]);
- }
- else if(Modbus_Wr_buff[Wr_eeprom_cnt][1] == Mb_Dbuff_RLft_ST_Addr)//设置故障状态
- {
- temp_addr = Modbus_Wr_buff[Wr_eeprom_cnt][1] - Mb_Dbuff_RLft_ST_Addr;
- temp_addr += ERom_RLft_ST_Addr;
- Eeprom_Rbuf[temp_addr] = Modbus_Wr_buff[Wr_eeprom_cnt][2] & Active_state_bit;
- }
- else if(Modbus_Wr_buff[Wr_eeprom_cnt][1] < Mb_Dbuff_RL_Toff1_Addr)//设置开启延时
- {
- temp_addr = Modbus_Wr_buff[Wr_eeprom_cnt][1] - Mb_Dbuff_RL_Ton1_Addr;
- temp_addr += ERom_RL_Ton1_Addr;
- Eeprom_Rbuf[temp_addr] = Modbus_Wr_buff[Wr_eeprom_cnt][2];
- DCPDU_Modbus_Reg[Modbus_Wr_buff[Wr_eeprom_cnt][1]] = Eeprom_Rbuf[temp_addr];
- DCPDU_RL_time.RL_ontime[Modbus_Wr_buff[Wr_eeprom_cnt][1] - Mb_Dbuff_RL_Ton1_Addr] = Eeprom_Rbuf[temp_addr];
- AT24CxxWriteOneByte(temp_addr,Eeprom_Rbuf[temp_addr]);
- }
- else if(Modbus_Wr_buff[Wr_eeprom_cnt][1] < Mb_Dbuff_RL_Allon_Addr)//设置继电器关断延时
- {
- temp_addr = Modbus_Wr_buff[Wr_eeprom_cnt][1] - Mb_Dbuff_RL_Toff1_Addr;
- temp_addr += ERom_RL_Toff1_Addr;
- Eeprom_Rbuf[temp_addr] = Modbus_Wr_buff[Wr_eeprom_cnt][2];
- DCPDU_Modbus_Reg[Modbus_Wr_buff[Wr_eeprom_cnt][1]] = Eeprom_Rbuf[temp_addr];
- DCPDU_RL_time.RL_offtime[Modbus_Wr_buff[Wr_eeprom_cnt][1] - Mb_Dbuff_RL_Toff1_Addr] = Eeprom_Rbuf[temp_addr];
- AT24CxxWriteOneByte(temp_addr,Eeprom_Rbuf[temp_addr]);
- }
- Modbus_Wr_buff[Wr_eeprom_cnt][0] = 0;
- Wr_eeprom_begin_flag = true;
- }
- Wr_eeprom_cnt++;
- if(Wr_eeprom_cnt >= Mb_Wcmd_Width)
- {
- Wr_eeprom_cnt = 0;
- }
- }
- /*********************************************************************************************************
- * 函数名称:Init_Dev_parameter
- * 函数功能:初始化模块内部参数
- * 输入参数:void
- * 输出参数:void
- * 返 回 值:void
- * 创建日期:2024年04月06日
- * 注 意:
- *********************************************************************************************************/
- void Init_Dev_parameter(void)
- {
- uint8_t i = 0;
- uint8_t j = 0;
- uint8_t temp1 = 0;
- uint8_t temp2 = 0;
- uint32_t temp0 = 0;
- uint32_t temp_da[8] = {0};
- while(!dma_flag_get(DMA_CH0,DMA_FLAG_FTF));
- dma_flag_clear(DMA_CH0,DMA_FLAG_FTF);
- for(i = 0;i < 8;i++)
- {
- adc_data_zero[i] = adc_original_value[i];
- }
- Init_IM1253E_flag = true;
- while(Init_IM1253E_flag)
- {
- if(Read_IM1253E_flag)
- {
- Read_IM1253E_flag = false;
- Read_IM1253E_REG(Device_addr_IM1253E,V_REG_ADDR,8);//Device_addr_IM1253E
- }
- if(Rx_Finish_flag)
- {
- Rx_Finish_flag = false;
- for(j = 0;j < 8;j++)
- {
- temp_da[j] = 0;
- for(i = 0;i < 4;i++)
- {
- temp1 = 8 * i;
- temp1 = 24 - temp1;
- temp2 = 4 * j;
- temp2 += 3;
- temp2 += i;
- temp0 = Rx_Data_buff_IM1253E[temp2] << temp1;
- temp_da[j] += temp0;
- }
- }
- DCPDU_Iin_Zero = temp_da[1] / 10;
- DCPDU_Win_Zero = temp_da[2] / 10;
-
- DCPDU_Modbus_Reg[Mb_Dbuff_VInput_Addr] = temp_da[0] / 10;//输入电压
- for(i = 0;i < RELAY_ChN_default;i++)
- {
- temp0 = 4 * i;
- DCPDU_Modbus_Reg[Mb_Dbuff_P1Output_Addr + temp0] = DCPDU_Modbus_Reg[Mb_Dbuff_VInput_Addr];//输出电压
- temp2 = 0x01 << i;
- 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]))
- {
- Fault_state_V |= temp2;
- }
- else
- {
- if((Fault_state_V & temp2) > 0)
- {
- temp1 = ~temp2;
- Fault_state_V &= temp1;
- }
- }
- }
- Init_IM1253E_flag = false;
- IM1253E_OK_flag = true;
- }
- }
- }
- /*freemodbus作者使用了assert,故增加如下代码,同时
- *options for target 对话框,target页面,勾选Use MicroLib
- */
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- while (1)
- {
- }
- }
- #else
- void __aeabi_assert(const char * x1, const char * x2, int x3)
- {
- (void)x3;
- }
- #endif
|