L91-01是基于HLW8032的單相交流電能計量模塊(以下簡稱模塊),該模塊包含一路交流電壓和一路交流電流的采集,通過串口的形式送到單片機,模塊可以測量電壓,電流,功率,電量以及功率因數,例程中僅給出了測量電壓,電流,功率,通過四位數碼管顯示。 適用: Ø 單片機學習、DIY等。
2.jpg (88.5 KB, 下載次數: 146)
下載附件
2019-8-16 10:48 上傳
1.jpg (76.01 KB, 下載次數: 157)
下載附件
2019-8-16 10:48 上傳
|
附件包含原理圖,51單片機的驅動例程,和芯片的Datasheet。
單片機源程序如下:
- #include "Config.H"
- //數碼管0-9顯示代碼 0 1 2 3 4 5 6 7 8 9
- u8 DISPLAY_CODE[10] = { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f };
- //數碼管帶點的0-9顯示代碼 0 1 2 3 4 5 6 7 8 9
- u8 DISPLAY_Point_CODE[10] = { 0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef };
- //數碼管位數 1 2 3 4
- u8 DIG_BIT_CODE[4] = { 0x68,0x6a,0x6c,0x6e };
- //8段顯示亮度等級 1 2 3 4 5 6 7 8
- u8 Light_Level_CODE[8] = { 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x01 };
- void I2CStart(void)//開始信號
- {
- CLK_H;
- DIO_H;
- Delay_us(5);
- DIO_L;
- }
- void I2Cask(void) //ACK信號
- {
- u8 timeout = 1;
- CLK_H;
- Delay_us(5);
- CLK_L;
- while((DIO)&&(timeout<=100))
- {
- timeout++;
- }
- Delay_us(5);
- CLK_L;
- }
- void I2CStop(void) //停止信號
- {
- CLK_H;
- DIO_L;
- Delay_us(5);
- DIO_H;
- }
- void I2CWrByte(u8 oneByte) //寫一個字節高位在前,低位在后
- {
- u8 i;
- CLK_L;
- Delay_us(1);
- for(i=0;i<8;i++)
- {
- oneByte = oneByte<<1;
- DIO = CY;
- CLK_L;
- Delay_us(5);
- CLK_H;
- Delay_us(5);
- CLK_L;
- }
- }
- void AiP650_Set(u8 add,u8 dat) //數碼管顯示
- {
- //寫顯存必須從高地址開始寫
- I2CStart();
- I2CWrByte(add); //第一個顯存地址
- I2Cask();
- I2CWrByte(dat);
- I2Cask();
- I2CStop();
- }
- void AiP650_DisPlay(u8 DIG_Bit, u8 Display_num) //顯示一位數字
- {
- AiP650_Set(DIG_BIT_CODE[DIG_Bit-1],DISPLAY_CODE[Display_num]);
- }
- void AiP650_CLR() //清屏
- {
- u8 i;
- for(i=0;i<4;i++)
- {
- AiP650_Set(DIG_BIT_CODE[i],0x00);
- }
- }
- void AiP650_DisPlayFourNum(u16 Display_num) //顯示一個四位數 (0-9999)
- {
- u8 One,Two,Three,Four;
- One = Display_num/1000;
- Two = Display_num%1000/100;
- Three = Display_num%100/10;
- Four = Display_num%10;
- if(One == 0)
- {
- AiP650_Set(DIG_BIT_CODE[0],0x00);
- if(Two == 0)
- {
- AiP650_Set(DIG_BIT_CODE[1],0x00);
- if(Three == 0)
- {
- AiP650_Set(DIG_BIT_CODE[2],0x00);
- }
- else
- {
- AiP650_DisPlay(3,Three);
- }
- AiP650_DisPlay(4,Four);
- }
- else
- {
- AiP650_DisPlay(2,Two);
- AiP650_DisPlay(3,Three);
- AiP650_DisPlay(4,Four);
- }
- }
- else
- {
- AiP650_DisPlay(1,One);
- AiP650_DisPlay(2,Two);
- AiP650_DisPlay(3,Three);
- AiP650_DisPlay(4,Four);
- }
- }
- void AiP650_DisPlayOnePointNum(u16 Display_num) //顯示帶一位小數
- {
- u8 One,Two,Three,Four;
- One = Display_num/1000;
- Two = Display_num%1000/100;
- Three = Display_num%100/10;
- Four = Display_num%10;
- if(One == 0)
- {
- AiP650_Set(DIG_BIT_CODE[0],0x00);
- if(Two == 0)
- {
- AiP650_Set(DIG_BIT_CODE[1],0x00);
- AiP650_Set(DIG_BIT_CODE[2],DISPLAY_Point_CODE[Three]);
- AiP650_DisPlay(4,Four);
- }
- else
- {
- AiP650_DisPlay(2,Two);
- AiP650_Set(DIG_BIT_CODE[2],DISPLAY_Point_CODE[Three]);
- AiP650_DisPlay(4,Four);
- }
- }
- else
- {
- AiP650_DisPlay(1,One);
- AiP650_DisPlay(2,Two);
- AiP650_Set(DIG_BIT_CODE[2],DISPLAY_Point_CODE[Three]);
- AiP650_DisPlay(4,Four);
- }
- }
- void AiP650_DisPlayTwoPointNum(u16 Display_num) //顯示帶兩位小數
- {
- u8 One,Two,Three,Four;
- One = Display_num/1000;
- Two = Display_num%1000/100;
- Three = Display_num%100/10;
- Four = Display_num%10;
- if(One == 0)
- {
- AiP650_Set(DIG_BIT_CODE[0],0x00);
- AiP650_Set(DIG_BIT_CODE[1],DISPLAY_Point_CODE[Two]);
- AiP650_DisPlay(3,Three);
- AiP650_DisPlay(4,Four);
- }
- else
- {
- AiP650_DisPlay(1,One);
- AiP650_Set(DIG_BIT_CODE[1],DISPLAY_Point_CODE[Two]);
- AiP650_DisPlay(3,Three);
- AiP650_DisPlay(4,Four);
- }
- }
- void Light_Level_Set(u8 Level) //設置亮度等級 1-8級
- {
- AiP650_Set(0x48,Light_Level_CODE[Level-1]);
- }
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
【實用電子小模塊】單相電能計量模塊-UART接口-HLW8032芯片.rar
(1.91 MB, 下載次數: 537)
2019-8-16 10:43 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|