找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 14548|回復(fù): 12
打印 上一主題 下一主題
收起左側(cè)

HLW8032芯片單相電能計(jì)量模塊單片機(jī)程序-UART接口

  [復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
L91-01是基于HLW8032的單相交流電能計(jì)量模塊(以下簡稱模塊),該模塊包含一路交流電壓和一路交流電流的采集,通過串口的形式送到單片機(jī),模塊可以測(cè)量電壓,電流,功率,電量以及功率因數(shù),例程中僅給出了測(cè)量電壓,電流,功率,通過四位數(shù)碼管顯示。
適用:
Ø 單片機(jī)學(xué)習(xí)、DIY等。


附件包含原理圖,51單片機(jī)的驅(qū)動(dòng)例程,和芯片的Datasheet。

單片機(jī)源程序如下:
  1. #include "Config.H"
  2. //數(shù)碼管0-9顯示代碼                    0         1          2           3        4    5    6           7        8    9       
  3. u8 DISPLAY_CODE[10]         = {        0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f };
  4. //數(shù)碼管帶點(diǎn)的0-9顯示代碼           0         1          2           3        4    5    6           7        8    9       
  5. u8 DISPLAY_Point_CODE[10]         = {        0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef };
  6. //數(shù)碼管位數(shù)                                 1         2    3           4
  7. u8 DIG_BIT_CODE[4]          = {        0x68,0x6a,0x6c,0x6e };
  8. //8段顯示亮度等級(jí)                           1         2          3           4        5         6    7           8                       
  9. u8 Light_Level_CODE[8]        = {        0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x01 };

  10. void I2CStart(void)//開始信號(hào)
  11. {
  12.         CLK_H;
  13.         DIO_H;
  14.         Delay_us(5);
  15.         DIO_L;
  16. }

  17. void I2Cask(void) //ACK信號(hào)
  18. {
  19.         u8 timeout = 1;
  20.         CLK_H;
  21.         Delay_us(5);
  22.         CLK_L;
  23.         while((DIO)&&(timeout<=100))
  24.         {
  25.                 timeout++;
  26.         }
  27.         Delay_us(5);
  28.         CLK_L;
  29. }

  30. void I2CStop(void) //停止信號(hào)
  31. {
  32.         CLK_H;
  33.         DIO_L;
  34.         Delay_us(5);
  35.         DIO_H;
  36. }

  37. void I2CWrByte(u8 oneByte) //寫一個(gè)字節(jié)高位在前,低位在后
  38. {
  39.         u8 i;
  40.         CLK_L;
  41.         Delay_us(1);
  42.         for(i=0;i<8;i++)
  43.         {
  44.                 oneByte = oneByte<<1;
  45.                 DIO = CY;
  46.                 CLK_L;
  47.                 Delay_us(5);
  48.                 CLK_H;
  49.                 Delay_us(5);
  50.                 CLK_L;
  51.         }
  52. }

  53. void AiP650_Set(u8 add,u8 dat) //數(shù)碼管顯示
  54. {
  55.         //寫顯存必須從高地址開始寫
  56.         I2CStart();
  57.         I2CWrByte(add); //第一個(gè)顯存地址
  58.         I2Cask();
  59.         I2CWrByte(dat);
  60.         I2Cask();
  61.         I2CStop();
  62. }

  63. void AiP650_DisPlay(u8 DIG_Bit, u8 Display_num)                //顯示一位數(shù)字
  64. {
  65.         AiP650_Set(DIG_BIT_CODE[DIG_Bit-1],DISPLAY_CODE[Display_num]);
  66. }

  67. void AiP650_CLR()                                                                   //清屏
  68. {
  69.         u8 i;
  70.         for(i=0;i<4;i++)
  71.         {
  72.                 AiP650_Set(DIG_BIT_CODE[i],0x00);               
  73.         }
  74. }

  75. void AiP650_DisPlayFourNum(u16 Display_num)                //顯示一個(gè)四位數(shù) (0-9999)
  76. {
  77.         u8 One,Two,Three,Four;
  78.         One   = Display_num/1000;
  79.         Two   = Display_num%1000/100;
  80.         Three = Display_num%100/10;
  81.         Four  = Display_num%10;
  82.         if(One == 0)
  83.         {
  84.                 AiP650_Set(DIG_BIT_CODE[0],0x00);       
  85.                 if(Two == 0)
  86.                 {
  87.                         AiP650_Set(DIG_BIT_CODE[1],0x00);
  88.                         if(Three == 0)
  89.                         {
  90.                                 AiP650_Set(DIG_BIT_CODE[2],0x00);
  91.                         }
  92.                         else
  93.                         {
  94.                                 AiP650_DisPlay(3,Three);
  95.                         }
  96.                         AiP650_DisPlay(4,Four);
  97.                 }
  98.                 else
  99.                 {
  100.                         AiP650_DisPlay(2,Two);
  101.                         AiP650_DisPlay(3,Three);
  102.                         AiP650_DisPlay(4,Four);       
  103.                 }
  104.         }
  105.         else  
  106.         {
  107.                 AiP650_DisPlay(1,One);
  108.                 AiP650_DisPlay(2,Two);
  109.                 AiP650_DisPlay(3,Three);
  110.                 AiP650_DisPlay(4,Four);
  111.         }                       
  112. }
  113. void AiP650_DisPlayOnePointNum(u16 Display_num)                //顯示帶一位小數(shù)
  114. {
  115.         u8 One,Two,Three,Four;
  116.         One   = Display_num/1000;
  117.         Two   = Display_num%1000/100;
  118.         Three = Display_num%100/10;
  119.         Four  = Display_num%10;
  120.         if(One == 0)
  121.         {
  122.                 AiP650_Set(DIG_BIT_CODE[0],0x00);       
  123.                 if(Two == 0)
  124.                 {
  125.                         AiP650_Set(DIG_BIT_CODE[1],0x00);
  126.                         AiP650_Set(DIG_BIT_CODE[2],DISPLAY_Point_CODE[Three]);
  127.                         AiP650_DisPlay(4,Four);
  128.                 }
  129.                 else
  130.                 {
  131.                         AiP650_DisPlay(2,Two);
  132.                         AiP650_Set(DIG_BIT_CODE[2],DISPLAY_Point_CODE[Three]);
  133.                         AiP650_DisPlay(4,Four);       
  134.                 }
  135.         }
  136.         else  
  137.         {
  138.                 AiP650_DisPlay(1,One);
  139.                 AiP650_DisPlay(2,Two);
  140.                 AiP650_Set(DIG_BIT_CODE[2],DISPLAY_Point_CODE[Three]);
  141.                 AiP650_DisPlay(4,Four);
  142.         }                       
  143. }
  144. void AiP650_DisPlayTwoPointNum(u16 Display_num)                //顯示帶兩位小數(shù)
  145. {
  146.         u8 One,Two,Three,Four;
  147.         One   = Display_num/1000;
  148.         Two   = Display_num%1000/100;
  149.         Three = Display_num%100/10;
  150.         Four  = Display_num%10;
  151.         if(One == 0)
  152.         {
  153.                 AiP650_Set(DIG_BIT_CODE[0],0x00);       
  154.                 AiP650_Set(DIG_BIT_CODE[1],DISPLAY_Point_CODE[Two]);
  155.                 AiP650_DisPlay(3,Three);
  156.                 AiP650_DisPlay(4,Four);
  157.         }
  158.         else  
  159.         {
  160.                 AiP650_DisPlay(1,One);
  161.                 AiP650_Set(DIG_BIT_CODE[1],DISPLAY_Point_CODE[Two]);
  162.                 AiP650_DisPlay(3,Three);
  163.                 AiP650_DisPlay(4,Four);
  164.         }                       
  165. }
  166. void Light_Level_Set(u8 Level)                                           //設(shè)置亮度等級(jí) 1-8級(jí)
  167. {
  168.         AiP650_Set(0x48,Light_Level_CODE[Level-1]);
  169. }
  170. ……………………

  171. …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼

所有資料51hei提供下載:
【實(shí)用電子小模塊】單相電能計(jì)量模塊-UART接口-HLW8032芯片.rar (1.91 MB, 下載次數(shù): 537)

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏7 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:600684 發(fā)表于 2019-8-19 14:14 | 只看該作者
第一次做,程序會(huì)持續(xù)優(yōu)化,歡迎大家交流。
回復(fù)

使用道具 舉報(bào)

板凳
ID:202599 發(fā)表于 2019-10-12 09:31 | 只看該作者
謝謝樓主,辛苦了。
回復(fù)

使用道具 舉報(bào)

地板
ID:59423 發(fā)表于 2019-10-12 13:50 | 只看該作者
計(jì)量芯片都是使用SPI接口居多。
回復(fù)

使用道具 舉報(bào)

5#
ID:79874 發(fā)表于 2019-11-4 15:10 來自手機(jī) | 只看該作者
樓主能否提供下PCB設(shè)計(jì)源文件
回復(fù)

使用道具 舉報(bào)

6#
ID:65956 發(fā)表于 2019-11-5 09:53 | 只看該作者
這個(gè)我以前也作過,但效果不是很好,可能跟當(dāng)時(shí)需求部門要求有一定的難度造成的,所以后來沒有繼續(xù),現(xiàn)在下載你的下來參考一下
回復(fù)

使用道具 舉報(bào)

7#
ID:250660 發(fā)表于 2019-12-27 21:43 | 只看該作者
你的 CUR_Flag==0 注釋了是為什么
回復(fù)

使用道具 舉報(bào)

8#
ID:55115 發(fā)表于 2020-7-28 13:25 | 只看該作者
不能知道和HLW8110通用不?下載來看看吧!
回復(fù)

使用道具 舉報(bào)

9#
ID:721297 發(fā)表于 2020-9-25 21:02 | 只看該作者
在幫別人寫個(gè)測(cè)交流電壓電流程序,用的HLW8110,沒想到這個(gè)片子只有SOP8封裝而且只支持串口9600bps通訊,網(wǎng)上想找個(gè)程序好難啊
回復(fù)

使用道具 舉報(bào)

10#
ID:824001 發(fā)表于 2020-9-27 21:17 來自手機(jī) | 只看該作者
這個(gè)模塊要校準(zhǔn)嗎
回復(fù)

使用道具 舉報(bào)

11#
ID:588044 發(fā)表于 2021-10-28 10:17 | 只看該作者
下來學(xué)習(xí)下,謝謝樓主分享!
回復(fù)

使用道具 舉報(bào)

12#
ID:1013669 發(fā)表于 2022-3-28 13:52 | 只看該作者
電量圖不全啊。單片機(jī)電路在哪里?
回復(fù)

使用道具 舉報(bào)

13#
ID:373 發(fā)表于 2022-8-12 11:35 | 只看該作者
學(xué)習(xí)下,希望有用,有用我再來回帖
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表