From d329c022b55202cbb193a1e07668e5f5858f685e Mon Sep 17 00:00:00 2001 From: aixiao Date: Tue, 12 Mar 2024 11:28:55 +0800 Subject: [PATCH] Add Zeroing electrical energy --- main.c | 86 ++++++++++++++++++++++++++++++++++++++-------------------- main.h | 4 +-- 2 files changed, 59 insertions(+), 31 deletions(-) diff --git a/main.c b/main.c index c3acdd1..8949ed7 100644 --- a/main.c +++ b/main.c @@ -34,7 +34,7 @@ unsigned int chkcrc(unsigned char *buf, unsigned char len) void read_data(void) { - static int i = 0; + static int i = 0; union crcdata { unsigned int word16; unsigned char byte[2]; @@ -83,8 +83,8 @@ void Analysis_data(void) if (Rx_Buffer[0] == Read_ID) // 确认 ID 正确 { - crcnow.word16 = chkcrc(Rx_Buffer, reveive_number - 2); // reveive_number 是接收数据总长度 - if ((crcnow.byte[0] == Rx_Buffer[reveive_number - 1]) && (crcnow.byte[1] == Rx_Buffer[reveive_number - 2])) // 确认 CRC 校验正确 + crcnow.word16 = chkcrc(Rx_Buffer, receive_number - 2); // receive_number 是接收数据总长度 + if ((crcnow.byte[0] == Rx_Buffer[receive_number - 1]) && (crcnow.byte[1] == Rx_Buffer[receive_number - 2])) // 确认 CRC 校验正确 { Voltage_data = (((unsigned long)(Rx_Buffer[3])) << 24) | (((unsigned long)(Rx_Buffer[4])) << 16) | (((unsigned long)(Rx_Buffer[5])) << 8) | Rx_Buffer[6]; Current_data = (((unsigned long)(Rx_Buffer[7])) << 24) | (((unsigned long)(Rx_Buffer[8])) << 16) | (((unsigned long)(Rx_Buffer[9])) << 8) | Rx_Buffer[10]; @@ -94,7 +94,7 @@ void Analysis_data(void) CO2_data = (((unsigned long)(Rx_Buffer[23])) << 24) | (((unsigned long)(Rx_Buffer[24])) << 16) | (((unsigned long)(Rx_Buffer[25])) << 8) | Rx_Buffer[26]; Temperature_data = (((unsigned long)(Rx_Buffer[27])) << 24) | (((unsigned long)(Rx_Buffer[28])) << 16) | (((unsigned long)(Rx_Buffer[29])) << 8) | Rx_Buffer[30]; Hz_data = (((unsigned long)(Rx_Buffer[31])) << 24) | (((unsigned long)(Rx_Buffer[32])) << 16) | (((unsigned long)(Rx_Buffer[33])) << 8) | Rx_Buffer[34]; - + } } } @@ -114,7 +114,7 @@ void Print_data(void) float energy_data = (float)Energy_data * 0.0001; printf("电能: %.2f KWH\n", energy_data); - float pf_data = (float)Pf_data *0.001; + float pf_data = (float)Pf_data * 0.001; printf("功率因数: %.2f \n", pf_data); float cO2_data = (float)CO2_data * 0.0001; @@ -122,14 +122,36 @@ void Print_data(void) printf("温度: %.2f ℃\n", (float)Temperature_data * 0.01); printf("频率: %.2f HZ\n", (float)Hz_data * 0.01); - - return ; + + return; +} + +// 电能清零 +void Electric_energy_(int sec) +{ + + uint8_t RETURN_DATA[8] = { 0 }; + uint8_t ELE_ZRRO[13] = { 0x01, 0x10, 0x00, 0x4B, 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0xB6, 0x2C }; + + // 加入从开机算起60秒后 + if (sec >= 3) { + printf("电能清零\n"); + uart_write_blocking(UART0, ELE_ZRRO, 13); + sleep_ms(10); + uart_read_blocking(UART0, RETURN_DATA, 8); + for (int i = 0; i <= 8 - 1; i++) { + printf("0x%X ", RETURN_DATA[i]); + } + printf("\n"); + } + + return; } static uint16_t IM1253B(void) { static int i = 0; - + // 初始化UART uart_init(UART0, BAUD_RATE); gpio_set_function(UART0_TX_PIN, GPIO_FUNC_UART); @@ -137,31 +159,31 @@ static uint16_t IM1253B(void) uart_set_hw_flow(UART0, false, false); uart_set_format(UART0, DATA_BITS, STOP_BITS, PARITY); sleep_ms(10); - + /* - // 官方软件内部按钮发送的数据 - // 电能清零 TX[13]:01 10 00 4B 00 02 04 00 00 00 00 B6 2C - // RX[8]:01 10 00 4B 00 02 31 DE - - // 读取 TX[8]:00 03 00 01 00 04 14 18 - // RX[13]:01 03 08 02 35 01 08 41 30 01 05 44 BD (直流模块) - - // 设置 TX[11]:00 10 00 04 00 01 02 01 05 6B D7 - // RX[8]:01 10 00 04 00 01 40 08 - */ - + // 官方软件内部按钮发送的数据 + // 电能清零 TX[13]:01 10 00 4B 00 02 04 00 00 00 00 B6 2C + // RX[8]:01 10 00 4B 00 02 31 DE + + // 读取 TX[8]:00 03 00 01 00 04 14 18 + // RX[13]:01 03 08 02 35 01 08 41 30 01 05 44 BD (直流模块) + + // 设置 TX[11]:00 10 00 04 00 01 02 01 05 6B D7 + // RX[8]:01 10 00 04 00 01 40 08 + */ + // 发送 read_enable = 1; read_data(); sleep_ms(10); // 发送完成后接收数据并处理 - reveive_number = 37; + receive_number = 37; receive_finished = 1; Analysis_data(); - + // 打印原始十六进制数据 - for (i=0; i <= 37 - 1; i++) { + for (i = 0; i <= 37 - 1; i++) { printf("0x%X ", Rx_Buffer[i]); } printf("\n"); @@ -177,23 +199,29 @@ int main(void) stdio_init_all(); sleep_ms(1000); set_sys_clock_khz(250000, true); - + // 启动看门狗, 程序如果有阻塞或者程序逻辑错误重启 - if (watchdog_caused_reboot()) { // 判断是否从看门狗启动或者正常启动 + if (watchdog_caused_reboot()) { // 判断是否从看门狗启动或者正常启动 printf("Rebooted by Watchdog!\n"); } else { printf("Clean boot\n"); } - watchdog_enable(8300, 1); // 8秒检测是否重新加载看门狗计数器. (不更新计数器则重启硬件, 最高8秒(8秒后不喂狗硬件重启)) + watchdog_enable(8300, 1); // 8秒检测是否重新加载看门狗计数器. (不更新计数器则重启硬件, 最高8秒(8秒后不喂狗硬件重启)) watchdog_start_tick(12); - printf("IM1253B module\n"); + sec = 0; + printf("IM1253B Module\n"); + while (1) { - watchdog_update(); // 喂狗 - + watchdog_update(); // 喂狗 + IM1253B(); printf("\n"); + // 电能清零 + sec++; + Electric_energy_(sec); + sleep_ms(2000); } diff --git a/main.h b/main.h index ce2fde0..ca5338b 100644 --- a/main.h +++ b/main.h @@ -26,8 +26,9 @@ int Read_ID = 0x01; unsigned char Tx_Buffer[8]; unsigned char Rx_Buffer[40]; -unsigned char read_enable, receive_finished, reveive_number; +unsigned char read_enable, receive_finished, receive_number; unsigned long Voltage_data, Current_data, Power_data, Energy_data, Pf_data, CO2_data, Temperature_data, Hz_data; +int sec; unsigned int calccrc(unsigned char crcbuf, unsigned int crc); unsigned int chkcrc(unsigned char *buf, unsigned char len); @@ -36,5 +37,4 @@ void Analysis_data(void); void Print_data(void); static uint16_t IM1253B(void); - #endif