From 570c20413415cc218cbb800dfb1e4b41e5775a96 Mon Sep 17 00:00:00 2001 From: kyz Date: Thu, 5 Dec 2024 16:45:33 +0800 Subject: [PATCH] =?utf8?q?=E7=94=B5=E6=B5=81=E7=94=B5=E5=8E=8B=E5=80=8D?= =?utf8?q?=E7=8E=87=E7=94=A8=E4=BA=8E=E4=B8=8A=E6=8A=A5=E6=95=B0=E6=8D=AE?= =?utf8?q?=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit B码对时增加解析代码 --- STM32/code/app/App_irig_b.c | 171 ++++++++++++++++++++++------ STM32/code/app/main_task.c | 5 +- STM32/code/app/root.c | 6 +- STM32/code/flat/flats.c | 4 +- STM32/code/mcu_com/mcu_com_energy.c | 64 +++++------ 5 files changed, 178 insertions(+), 72 deletions(-) diff --git a/STM32/code/app/App_irig_b.c b/STM32/code/app/App_irig_b.c index 9ec1a0da..3250ee23 100644 --- a/STM32/code/app/App_irig_b.c +++ b/STM32/code/app/App_irig_b.c @@ -1,6 +1,5 @@ /* - App_Lora.c - LoraÓ¦Óà + BÂë¶Ôʱ */ #include "def.h" @@ -15,70 +14,170 @@ #include "partial_discharge.h" #include "protocol_modbus_rtu.h" -int32_t signal_cnt = 0; -uint32_t rx_bit_h; -uint32_t rx_bit_l; -static uint8_t rx_data[1024]; -static uint8_t rx_data_copy[1024]; -static uint8_t rx_cnt = 0; -static uint8_t rx_cnt_copy = 0; +#define TIME_BUF_SIZE 2048 //0x800 +#define TIME_BUF_SIZE_MASK ( TIME_BUF_SIZE - 1) //0x7FF static uint8_t rx_start = 0; -void IRIG_B_Scan( void ) +static uint8_t irig_b_rx_buf[TIME_BUF_SIZE]; +static uint32_t rd_n = 0; //Ñ­»·buffer ¶ÁÖ¸Õë +static uint32_t wr_n = 0; //Ñ­»·buffer дָÕë + +static void push_item( uint8_t d ) +{ + if( wr_n - rd_n < TIME_BUF_SIZE -1 ){ + irig_b_rx_buf[wr_n&TIME_BUF_SIZE_MASK] = d; + wr_n++; + }else{ + //buffer full + } +} + +static int pop_item( void ) +{ + int ret = -1; + + if( rd_n < wr_n ){ + ret = irig_b_rx_buf[rd_n&TIME_BUF_SIZE_MASK]; + rd_n++; + } + return ret; +} + +void IRIG_B_Time_Scan( void ) { -#if 0 +#if 1 //1ºÁÃëÖжÏÖУ¬¶¨Ê±Ö´ÐУ¬¼ì²âÊäÈëÐÅºÅµçÆ½£¬¸ßµÍ½øÐмÆÊý£¬ºÍʱ¼ä //µÍµçƽµ½¸ßµçƽ¿ªÊ¼Ò»¸öÂëÔª£¬¸ßµ½µÍ£¬ÔÙµ½¸ß£¬ Íê³ÉÒ»¸öÂëÔª£¬¿ªÊ¼ÏÂÒ»¸öÂëÔª static uint32_t last_level=-1; - uint32_t curr_level=-1; - static uint32_t idle_time = 0; //µçƽ²»±ä¼ÆÊ±´ïµ½20ºÁÃëºó£¬Í£Ö¹²É¼¯Êý¾Ý - uint32_t rx_stop = 0; + static uint32_t stop_time = 0; //µçƽ²»±ä¼ÆÊ±´ïµ½20ºÁÃëºó£¬Í£Ö¹²É¼¯Êý¾Ý + + uint32_t curr_level; //USART2_RXD / GPD6 curr_level = HAL_GPIO_ReadPin( GPIOD, 1<<6 )==GPIO_PIN_SET ? 1: 0; - HAL_GPIO_WritePin( GPIOD, PIN(4), GPIO_PIN_RESET ); if( rx_start == 0 ){ if( last_level == 0 && curr_level == 1 ){ - //ÉÏÉýÑØ¿ªÊ¼Êý¾Ý²ÉÑù - rx_start = 1; - if( rx_cnt > 1000 ){ - rx_cnt_copy = rx_cnt; - memcpy( rx_data_copy, rx_data, rx_cnt ); + //µÚÒ»¸öÉÏÉýÑØ¿ªÊ¼Êý¾Ý²ÉÑù + if( wr_n == rd_n ){ + rd_n = 0; + wr_n = 0; } - rx_cnt = 0; + rx_start = 1; + }else{ + last_level = curr_level; + return; } - }else{ - rx_data[rx_cnt++] = curr_level; } - + + //Êý¾Ýÿ1ºÁÃë¶ÁÈ¡ÐÅºÅµçÆ½±£´æÔÚbufferÖУ¬Óû§taskÖÐÈ¥½âÎö + push_item( curr_level ); + if( last_level == curr_level ){ - idle_time++; - if( idle_time > 20 ){ + if( stop_time++ > 20 ){ //20ºÁÃëûÓб仯£¬Í£Ö¹²ÉÑù - idle_time = 0; - rx_stop = 1; + stop_time = 0; + rx_start = 0; } - }else{ - idle_time = 0; + stop_time = 0; last_level = curr_level; } + #endif } -void IRIG_B_Init( void ) +//¶Áȡһ¸öbit£¬ ·µ»Ø ´íÎó£º-1 £¬ 0£ºÊý¾Ý0 2/8 1£ºÊý¾Ý1 5/5£¬ 2£ºP 8/2 +int32_t IRIG_B_Time_get_bit( void ) { - + int h_time = 0; + int l_time = 0; + int ret = -1; + + //¼ÆËã¸ßµçƽʱ¼ä + while( rd_n < wr_n && irig_b_rx_buf[rd_n&TIME_BUF_SIZE_MASK] == 1 ){ + rd_n++; + h_time++; + } + //¼ÆËãµÍµçƽʱ¼ä + while( rd_n < wr_n && irig_b_rx_buf[rd_n&TIME_BUF_SIZE_MASK] == 0 ){ + rd_n++; + l_time++; + } + + if( h_time>0 && h_time + l_time >=9 && h_time + l_time <= 11 ){ + if( h_time <3 ) + ret = 0; // 2ms + 8ms - 0 + else if( h_time <6 ) + ret = 1; //5ms + 5ms - 1 + else if( h_time <9 ) + ret = 2; //8ms + 2ms - P + } + + return ret; } +//¶Áȡһ¸öByte£¬ Õý³££º·µ»Ø BCDÂ룬 ´íÎ󣺷µ»Ø-1 +int32_t IRIG_B_Time_get_byte( void ) +{ + uint8_t rx_byte = 0; + + int32_t b; + + //P + if( (b = IRIG_B_Time_get_bit()) != 2) + return -1; + + //Bit0 + if( (b = IRIG_B_Time_get_bit()) < 0 || b == 2) + return -1; + rx_byte |= b?1:0; + + //Bit1 + if( (b = IRIG_B_Time_get_bit()) < 0 || b == 2) + return -1; + rx_byte |= b?2:0; + + //Bit2 + if( (b = IRIG_B_Time_get_bit()) < 0 || b == 2) + return -1; + rx_byte |= b?4:0; + + //Bit3 + if( (b = IRIG_B_Time_get_bit()) < 0 || b == 2) + return -1; + rx_byte |= b?8:0; -void IRIG_B_Task( void ) + //0 + if( (b = IRIG_B_Time_get_bit()) < 0 || b != 0) + return -1; + rx_byte |= b?1:0; + +} + + +void IRIG_B_Time_Init( void ) { - if( rx_cnt_copy ){ - printf( "IRIG-B rx data %d\n", rx_cnt_copy ); - rx_cnt_copy = 0; + //HAL_GPIO_WritePin( GPIOD, PIN(4), GPIO_PIN_RESET ); +} + +void IRIG_B_Time_Task( void ) +{ +#if 0 + uint8_t data[10]; + if( wr_n - rd_n >= 1000 ){ + //½âÎöÊý¾Ý + + if( get_bit() == TIME_P ){ //P0 + for( int i=0; i<10; i++ ){ + get_bit() == TEIM_P + data[i] = get_byte(); + } } + }else if( rx_start == 0 ){ + rd_n = wr_n; + } +#endif } diff --git a/STM32/code/app/main_task.c b/STM32/code/app/main_task.c index 90d37ef9..e2feffd0 100644 --- a/STM32/code/app/main_task.c +++ b/STM32/code/app/main_task.c @@ -113,6 +113,7 @@ extern int32_t Task_Test( void ); extern int32_t Run_RN2026_Task( void ); extern void Debug_temctrl( void ); //for debug +extern void IRIG_B_Time_Task( void ); void Task_main() { @@ -182,7 +183,9 @@ void Task_main() Task_ModbusRtu(); Task_Iwdg_feed(); // ι¹· - + + IRIG_B_Time_Task(); //BÂë¶Ôʱ + // Mqtt_Proactive_Escalation(); //all tasks running time measurment in ms. diff --git a/STM32/code/app/root.c b/STM32/code/app/root.c index 8eb4cf53..0f650d0c 100644 --- a/STM32/code/app/root.c +++ b/STM32/code/app/root.c @@ -30,6 +30,7 @@ extern void MX_LWIP_Init(void); extern int32_t Init_Test( void ); +extern void IRIG_B_Time_Init( void ); /* Ó¦Óóõʼ»¯ */ @@ -112,5 +113,8 @@ void App_Init() Modbus_Init(); //MAX31865_Config(CONFIG_ADDR,THREE_WIRE); -} + //BÂë¶Ôʱ + IRIG_B_Time_Init(); + +} diff --git a/STM32/code/flat/flats.c b/STM32/code/flat/flats.c index 9f9103c4..07b3918a 100644 --- a/STM32/code/flat/flats.c +++ b/STM32/code/flat/flats.c @@ -11,7 +11,7 @@ #include "usart.h" #include "yx.h" -void IRIG_B_Scan( void ); +extern void IRIG_B_Time_Scan( void ); // ϵͳÈí¼þ¸´Î» void System_Soft_RST(void) @@ -32,7 +32,7 @@ void Task_1ms_Ontimeout() Refresh_YX_Status(); //IRIG_BÂë¶Ôʱ,ÐźÅÏßµçÆ½¼ì²â - IRIG_B_Scan(); + IRIG_B_Time_Scan(); } /* diff --git a/STM32/code/mcu_com/mcu_com_energy.c b/STM32/code/mcu_com/mcu_com_energy.c index 28daadca..76028a67 100644 --- a/STM32/code/mcu_com/mcu_com_energy.c +++ b/STM32/code/mcu_com/mcu_com_energy.c @@ -38,22 +38,22 @@ void Mcu_Energy_Get_eg_info( void ) // ÕâÀïµçѹºÍµçÁ÷±¶Âʲ»ÖªµÀÒª²»ÒªËãÉÏ£¿ Read_Energy_dbase(E_ACTIVE_POS, PH_T, i, &ddVal); - lVal = ddVal * DOT1 / 6400; + lVal = ddVal * DOT1 / 6400 * chipflashpara.PriLinePara.CT_I_D1/10 *chipflashpara.PriLinePara.CT_U_D1/10; g_energy.eg.PosEP[i] = swap_u32(lVal); Read_Energy_dbase(E_ACTIVE_NEG, PH_T, i, &ddVal); - lVal = ddVal * DOT1 / 6400; + lVal = ddVal * DOT1 / 6400 * chipflashpara.PriLinePara.CT_I_D1/10 *chipflashpara.PriLinePara.CT_U_D1/10; g_energy.eg.NegEP[i] = swap_u32(lVal); Read_Energy_dbase(E_REACTIVE_I, PH_T, i, &ddVal); - lVal = ddVal * DOT1 / 6400; + lVal = ddVal * DOT1 / 6400 * chipflashpara.PriLinePara.CT_I_D1/10 *chipflashpara.PriLinePara.CT_U_D1/10; g_energy.eg.PosEQ[i] = swap_u32(lVal); Read_Energy_dbase(E_REACTIVE_IV, PH_T, i, &ddVal); - lVal = ddVal * DOT1 / 6400; + lVal = ddVal * DOT1 / 6400 * chipflashpara.PriLinePara.CT_I_D1/10 *chipflashpara.PriLinePara.CT_U_D1/10; g_energy.eg.NegEQ[i] = swap_u32(lVal); } } @@ -169,28 +169,28 @@ void Mcu_Energy_Get_vol_info(void) { //Ïàµçѹ A/B/C/ƽ¾ù 4· - g_energy.vol.PV_A_D3 = swap_u32( rn_measure_data.U[0] ); - g_energy.vol.PV_B_D3 = swap_u32( rn_measure_data.U[1] ); - g_energy.vol.PV_C_D3 = swap_u32( rn_measure_data.U[2] ); - g_energy.vol.PV_Average_D3 = swap_u32( (rn_measure_data.U[0]+rn_measure_data.U[1]+rn_measure_data.U[2])/3 ); + g_energy.vol.PV_A_D3 = swap_u32( rn_measure_data.U[0] * chipflashpara.PriLinePara.CT_U_D1/10 ); + g_energy.vol.PV_B_D3 = swap_u32( rn_measure_data.U[1] * chipflashpara.PriLinePara.CT_U_D1/10 ); + g_energy.vol.PV_C_D3 = swap_u32( rn_measure_data.U[2] * chipflashpara.PriLinePara.CT_U_D1/10 ); + g_energy.vol.PV_Average_D3 = swap_u32( (rn_measure_data.U[0]+rn_measure_data.U[1]+rn_measure_data.U[2])/3 * chipflashpara.PriLinePara.CT_U_D1/10 ); //Ïßµçѹ A/B/C/ƽ¾ù 4· - g_energy.vol.LV_AB_D3 = ( Calc_Line_Vol_D3(0) ); - g_energy.vol.LV_BC_D3 = ( Calc_Line_Vol_D3(1) ); - g_energy.vol.LV_CA_D3 = ( Calc_Line_Vol_D3(2) ); + g_energy.vol.LV_AB_D3 = ( Calc_Line_Vol_D3(0) * chipflashpara.PriLinePara.CT_U_D1/10 ); + g_energy.vol.LV_BC_D3 = ( Calc_Line_Vol_D3(1) * chipflashpara.PriLinePara.CT_U_D1/10 ); + g_energy.vol.LV_CA_D3 = ( Calc_Line_Vol_D3(2) * chipflashpara.PriLinePara.CT_U_D1/10 ); g_energy.vol.LV_Average_D3 = swap_u32( ( g_energy.vol.LV_AB_D3 + g_energy.vol.LV_BC_D3 + g_energy.vol.LV_CA_D3 )/3 ); g_energy.vol.LV_AB_D3 = swap_u32( g_energy.vol.LV_AB_D3 ); g_energy.vol.LV_BC_D3 = swap_u32( g_energy.vol.LV_BC_D3 ); g_energy.vol.LV_CA_D3 = swap_u32( g_energy.vol.LV_CA_D3 ); //ÏàµçÁ÷ A/B/C/ƽ¾ù 4· - g_energy.vol.PI_A_D4 = swap_u32( rn_measure_data.I[0] ); - g_energy.vol.PI_B_D4 = swap_u32( rn_measure_data.I[1] ); - g_energy.vol.PI_C_D4 = swap_u32( rn_measure_data.I[2] ); - g_energy.vol.PI_Average_D4 = swap_u32( (rn_measure_data.I[0]+rn_measure_data.I[1]+rn_measure_data.I[2])/3 ); + g_energy.vol.PI_A_D4 = swap_u32( rn_measure_data.I[0] * chipflashpara.PriLinePara.CT_I_D1/10 ); + g_energy.vol.PI_B_D4 = swap_u32( rn_measure_data.I[1] * chipflashpara.PriLinePara.CT_I_D1/10 ); + g_energy.vol.PI_C_D4 = swap_u32( rn_measure_data.I[2] * chipflashpara.PriLinePara.CT_I_D1/10 ); + g_energy.vol.PI_Average_D4 = swap_u32( (rn_measure_data.I[0]+rn_measure_data.I[1]+rn_measure_data.I[2])/3 * chipflashpara.PriLinePara.CT_I_D1/10 ); //ÁãÐòµçÁ÷ - g_energy.vol.PI_N_D4 = swap_u32( rn_measure_data.In ); + g_energy.vol.PI_N_D4 = swap_u32( rn_measure_data.In * chipflashpara.PriLinePara.CT_I_D1/10 ); //Ïà½Ç g_energy.vol.Angle_D1[0] = swap_u16( rn_measure_data.Angle[0] ); //PhUb, ÒÔAÏàµçѹΪ»ù×¼£¬BÏà½Ç¶È @@ -203,31 +203,31 @@ void Mcu_Energy_Get_vol_info(void) g_energy.vol.Angle_D1[7] = swap_u16( rn_measure_data.Angle[7] ); //Angle C, CÏàµçÁ÷ÓëAÏàµçѹ¼ä½Ç¶È //µçѹƫ²îÖµÉϱ¨£¬ ΪÂú×ãÉÏλ»úÆæ¹ÖÐèÇó¶øÌí¼Ó 2024/12/1£¬ - g_energy.vol.PV_A_DIFF_D3 = swap_u32( g_energy.vol.PV_A_D3 - chipflashpara.PriLinePara.Un_D1*100 ); - g_energy.vol.PV_B_DIFF_D3 = swap_u32( g_energy.vol.PV_B_D3 - chipflashpara.PriLinePara.Un_D1*100 ); - g_energy.vol.PV_C_DIFF_D3 = swap_u32( g_energy.vol.PV_C_D3 - chipflashpara.PriLinePara.Un_D1*100 ); + g_energy.vol.PV_A_DIFF_D3 = swap_u32( (rn_measure_data.U[0] - chipflashpara.PriLinePara.Un_D1*100)* chipflashpara.PriLinePara.CT_U_D1/10 ); + g_energy.vol.PV_B_DIFF_D3 = swap_u32( (rn_measure_data.U[1] - chipflashpara.PriLinePara.Un_D1*100)* chipflashpara.PriLinePara.CT_U_D1/10 ); + g_energy.vol.PV_C_DIFF_D3 = swap_u32( (rn_measure_data.U[2] - chipflashpara.PriLinePara.Un_D1*100)* chipflashpara.PriLinePara.CT_U_D1/10 ); } //·â×°¹¦ÂÊÊý¾Ý°ü void Mcu_Energy_Get_power_info(void) { //×Ü/A/B/C Óй¦¹¦ÂÊ int32 СÊýµãºó3λ - g_energy.power.Active_T_D3 = swap_u32(rn_measure_data.Pw[0] ); - g_energy.power.Active_A_D3 = swap_u32(rn_measure_data.Pw[1] ); - g_energy.power.Active_B_D3 = swap_u32(rn_measure_data.Pw[2] ); - g_energy.power.Active_C_D3 = swap_u32(rn_measure_data.Pw[3] ); + g_energy.power.Active_T_D3 = swap_u32(rn_measure_data.Pw[0] * chipflashpara.PriLinePara.CT_I_D1/10 *chipflashpara.PriLinePara.CT_U_D1/10 ); + g_energy.power.Active_A_D3 = swap_u32(rn_measure_data.Pw[1] * chipflashpara.PriLinePara.CT_I_D1/10 *chipflashpara.PriLinePara.CT_U_D1/10 ); + g_energy.power.Active_B_D3 = swap_u32(rn_measure_data.Pw[2] * chipflashpara.PriLinePara.CT_I_D1/10 *chipflashpara.PriLinePara.CT_U_D1/10 ); + g_energy.power.Active_C_D3 = swap_u32(rn_measure_data.Pw[3] * chipflashpara.PriLinePara.CT_I_D1/10 *chipflashpara.PriLinePara.CT_U_D1/10 ); ////×Ü/A/B/C ÎÞ¹¦¹¦ÂÊ int32 СÊýµãºó3λ - g_energy.power.Reactive_T_D3 = swap_u32(rn_measure_data.Pw[4] ); - g_energy.power.Reactive_A_D3 = swap_u32(rn_measure_data.Pw[5] ); - g_energy.power.Reactive_B_D3 = swap_u32(rn_measure_data.Pw[6] ); - g_energy.power.Reactive_C_D3 = swap_u32(rn_measure_data.Pw[7] ); + g_energy.power.Reactive_T_D3 = swap_u32(rn_measure_data.Pw[4] * chipflashpara.PriLinePara.CT_I_D1/10 *chipflashpara.PriLinePara.CT_U_D1/10 ); + g_energy.power.Reactive_A_D3 = swap_u32(rn_measure_data.Pw[5] * chipflashpara.PriLinePara.CT_I_D1/10 *chipflashpara.PriLinePara.CT_U_D1/10 ); + g_energy.power.Reactive_B_D3 = swap_u32(rn_measure_data.Pw[6] * chipflashpara.PriLinePara.CT_I_D1/10 *chipflashpara.PriLinePara.CT_U_D1/10 ); + g_energy.power.Reactive_C_D3 = swap_u32(rn_measure_data.Pw[7] * chipflashpara.PriLinePara.CT_I_D1/10 *chipflashpara.PriLinePara.CT_U_D1/10 ); //×Ü/A/B/C ÊÓÔÚ¹¦ÂÊ int32 СÊýµãºó3λ - g_energy.power.Apparent_T_D3 = swap_u32(rn_measure_data.Pw[8] ); - g_energy.power.Apparent_A_D3 = swap_u32(rn_measure_data.Pw[9] ); - g_energy.power.Apparent_B_D3 = swap_u32(rn_measure_data.Pw[10] ); - g_energy.power.Apparent_C_D3 = swap_u32(rn_measure_data.Pw[11] ); + g_energy.power.Apparent_T_D3 = swap_u32(rn_measure_data.Pw[8] * chipflashpara.PriLinePara.CT_I_D1/10 *chipflashpara.PriLinePara.CT_U_D1/10 ); + g_energy.power.Apparent_A_D3 = swap_u32(rn_measure_data.Pw[9] * chipflashpara.PriLinePara.CT_I_D1/10 *chipflashpara.PriLinePara.CT_U_D1/10 ); + g_energy.power.Apparent_B_D3 = swap_u32(rn_measure_data.Pw[10] * chipflashpara.PriLinePara.CT_I_D1/10 *chipflashpara.PriLinePara.CT_U_D1/10 ); + g_energy.power.Apparent_C_D3 = swap_u32(rn_measure_data.Pw[11] * chipflashpara.PriLinePara.CT_I_D1/10 *chipflashpara.PriLinePara.CT_U_D1/10 ); //×Ü/A/B/C ¹¦ÂÊÒòËØ int32 СÊýµãºó3λ g_energy.power.Factor_T_D3 = swap_u32( rn_measure_data.Pf[0] ); @@ -239,7 +239,7 @@ void Mcu_Energy_Get_power_info(void) g_energy.power.freq_D3 = swap_u32( rn_measure_data.Frequency*10 ); //ƵÂÊÆ«²î //TODO£ºÔݰ´¹Ì¶¨50HzË㣬 ºóÃæÐ޸ijɰ´É豸ƵÂʲÎÊý¼ÆËã - g_energy.power.freq_diff_D3 = swap_u32( (rn_measure_data.Frequency -50*1000) ); + g_energy.power.freq_diff_D3 = swap_u32( (rn_measure_data.Frequency -chipflashpara.PriLinePara.Freq_D1*100) ); //ÐèÁ¿ g_energy.power.Active_Demand_D3 = swap_u32( RemoteValue.Active_Demand_now ); -- 2.34.1