HLW8032测量代码
实测了两组数据 第一组 第二组 测小的电压时,数据会比较准,测大的电压时,误差会比较大,这不是代码的问题,应该是板子或者芯片的原因。
单片机源程序如下:
- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include "led.h"
- #include "key.h"
- #include "dma.h"
- #include "oled.h"
- #include "stdio.h"
- ////////////////////////////////////////////////////////
- //因为电压电流功率那个系数是根据电路所用器件参数算出来的,所以不同板子参数
- //器件参数误差也不同,可以根据实际情况更改参数,校准电压电流值等等
- //注意:串口连接的时候一定要共地,要不然接收到的电压电流值都会是0,一开始没注意到这个问题,以为是程序错了
- //这里用到了F4串口1和串口6,串口1(波特率为9600)用来接收HLW8032传输过来的数据
- //串口二(波特率为115200)用来发送接收到的数据到串口
- int main(void)
- {
- uint32_t VpR = 0; // 电压参数寄存器
- uint32_t VR = 0; // 电压寄存器
- uint32_t CpR = 0; // 电流参数寄存器
- uint32_t CR = 0; // 电流寄存器
- uint32_t PpR = 0; // 电流参数寄存器
- uint32_t PR = 0; // 电流寄存器
-
- float VoltValue = 0.0; //电压值
- double IcurrentValue = 0.0; //电流值
- float PowerValue = 0.0; //功率值
-
- /**********相关外设函数的初始化*****************************/
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
- usart_x_Init(); //初始化串口
- delay_init(168); //初始化延时函数
- LED_Init(); //初始化LED
- KEY_Init(); //按键初始化
- OLED_Init(); //OLED初始化
-
- /******************OLED初始显示**************************/
- OLED_ShowCHinese(12,0,4,1); //电
- OLED_ShowCHinese(24,0,5,1); //压
- OLED_ShowCHinese(36,0,10,1); //:
- OLED_ShowString(108,0,"V",12); // V
-
- OLED_ShowCHinese(12,12,6,1); //电
- OLED_ShowCHinese(24,12,7,1); //流
- OLED_ShowCHinese(36,12,10,1); //:
- OLED_ShowString(108,12,"A",12); // A
-
- OLED_ShowCHinese(12,24,8,1); //功
- OLED_ShowCHinese(24,24,9,1); //率
- OLED_ShowCHinese(36,24,10,1); //:
- OLED_ShowString(108,24,"W",12); // W
- OLED_Refresh_Gram();//更新显示到OLED
-
-
- printf("Hello!\r\n");
-
- while(1)
- {
- LED0=!LED0; //程序运行标志
-
- /************串口1接收数据,串口6打印数据****************************************/
- if((Rx_InfoLen!=0 ) && ((ReceiveInfo[0] == 0xF2) || (ReceiveInfo[0] == 0x55))&&(ReceiveInfo[1] == 0x5A)) //判断数据是否有效,是否存在和被检测到
- {
- printf("\r\n");
- printf("State REG is %X\r\n",ReceiveInfo[0]); //打印状态寄存器的值
- printf("Check REG is %X\r\n",ReceiveInfo[1]); //打印检测寄存器的值
-
- printf("Voltage parameter REG is %X %X %X \r\n",ReceiveInfo[2],ReceiveInfo[3],ReceiveInfo[4]); //电压参数寄存器
- printf("Voltage REG is %X %X %X \r\n",ReceiveInfo[5],ReceiveInfo[6],ReceiveInfo[7]); //电压寄存器
-
- VpR = (ReceiveInfo[2]<<16) + (ReceiveInfo[3]<<8) + ReceiveInfo[4]; //把寄存器的值移位相加起来
- VR = (ReceiveInfo[5]<<16) + (ReceiveInfo[6]<<8) + ReceiveInfo[7];
- VoltValue = (VpR/VR)*1.88 ; //根据公式计算电压
- printf("VoltValue is %f \r\n",VoltValue); //打印电压值到串口助手
-
- printf("Current parameter REG is %X %X %X \r\n",ReceiveInfo[8],ReceiveInfo[9],ReceiveInfo[10]); //电流参数寄存器
- printf("Current REG is %X %X %X \r\n",ReceiveInfo[11],ReceiveInfo[12],ReceiveInfo[13]); //电流寄存器
-
- CpR =(ReceiveInfo[8]<<16) + (ReceiveInfo[9]<<8) + ReceiveInfo[10];
- CR = (ReceiveInfo[11]<<16) + (ReceiveInfo[12]<<8) + ReceiveInfo[13];
- IcurrentValue = ((float)CpR/(float)CR)*0.1; //根据公式计算电流
- printf("IcurrentValue is %lf \r\n",IcurrentValue); //打印电流的值到串口助手
-
- printf("Power parameter REG is %X %X %X \r\n",ReceiveInfo[14],ReceiveInfo[15],ReceiveInfo[16]); //功率参数寄存器
- printf("Power REG is %X %X %X \r\n",ReceiveInfo[17],ReceiveInfo[18],ReceiveInfo[19]); //功率寄存器
- PpR = (ReceiveInfo[14]<<16) + (ReceiveInfo[15]<<8) + ReceiveInfo[16];
- PR = (ReceiveInfo[17]<<16) + (ReceiveInfo[18]<<8) + ReceiveInfo[19];
- PowerValue = (PpR/PR)*1.6*0.1; //根据公式计算功率
- printf("PowerValueis %f \r\n",PowerValue); //打印功率的值到串口助手
- printf("Data Updata REG is %X\r\n",ReceiveInfo[20]); //数据更新寄存器
- printf("PF REG is %X %X\r\n",ReceiveInfo[21],ReceiveInfo[22]); //电能脉冲个数
- printf("CheckSumREG is %X\r\n",ReceiveInfo[23]); //检验和
-
-
- }
- memset(ReceiveInfo,0x00,Rx_InfoLen); //数据处理结束后,将数据存储数组清零
- Rx_InfoLen = 0; //数据长度清零
-
- /************OLED显示电流电压功率****************************************/
- OLED_ShowFloatNum_12(48,0,VoltValue,5,12); //显示电压
- OLED_ShowFloatNum_12(48,12,IcurrentValue,5,12); //显示电流
- OLED_ShowFloatNum_12(48,24,PowerValue,5,12); //显示功率
- OLED_Refresh_Gram(); // 更新到OELD
-
-
- }
-
- }
复制代码
所有资料51hei提供下载:
HLW8032测量代码.7z
(1.65 MB, 下载次数: 169)
|