Add Zeroing electrical energy
This commit is contained in:
42
main.c
42
main.c
@@ -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];
|
||||
@@ -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;
|
||||
@@ -123,7 +123,29 @@ 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)
|
||||
@@ -156,12 +178,12 @@ static uint16_t IM1253B(void)
|
||||
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");
|
||||
@@ -187,13 +209,19 @@ int main(void)
|
||||
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(); // 喂狗
|
||||
|
||||
IM1253B();
|
||||
printf("\n");
|
||||
|
||||
// 电能清零
|
||||
sec++;
|
||||
Electric_energy_(sec);
|
||||
|
||||
sleep_ms(2000);
|
||||
}
|
||||
|
||||
|
||||
4
main.h
4
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
|
||||
|
||||
Reference in New Issue
Block a user