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

QQ登錄

只需一步,快速開始

搜索

請(qǐng)問為什么GY-30的四位數(shù)據(jù)位在1602上顯示黑線?

查看數(shù): 3163 | 評(píng)論數(shù): 5 | 收藏 0
關(guān)燈 | 提示:支持鍵盤翻頁<-左 右->
    組圖打開中,請(qǐng)稍候......
發(fā)布時(shí)間: 2018-6-10 19:30

正文摘要:

<table cellspacing="0" cellpadding="0" style="table-layout: fixed; width: 757px; font-size: 12px; background-color: rgb(255, 255, 255);"><tbody><tr><td class="t_f" id=" ...

回復(fù)

ID:316425 發(fā)表于 2018-6-11 16:12
roc2 發(fā)表于 2018-6-11 16:07
第一可以用串口或其他方式看看bh1750測(cè)得的數(shù)據(jù)是不是對(duì)的,第二液晶顯示一個(gè)特定的數(shù)字看看是不是對(duì)的,如 ...

我的液晶1602顯示別的程序沒問題,請(qǐng)問你說的第一種怎樣查看呢?我感覺我這個(gè)芯片就是數(shù)據(jù)那沒傳到單片機(jī)上呢,該怎樣解決呢?
ID:349555 發(fā)表于 2018-6-11 16:07
第一可以用串口或其他方式看看bh1750測(cè)得的數(shù)據(jù)是不是對(duì)的,第二液晶顯示一個(gè)特定的數(shù)字看看是不是對(duì)的,如果以上沒問題,再看看是不是硬件問題了
ID:316425 發(fā)表于 2018-6-11 15:52
zhanghyg 發(fā)表于 2018-6-11 08:42
你的主程序呢?BH1750讀出來的數(shù)據(jù)要先處理一下才能顯示。

GY-30函數(shù)體

  1. #include  <REG52.H>       
  2. #include  <math.h>    //Keil library  
  3. #include  <stdio.h>   //Keil library       
  4. #include  <INTRINS.H>
  5. #include "GGYY.H"
  6. #define   uchar unsigned char
  7. #define   uint unsigned int       




  8. #define          SlaveAddress   0x46 //定義器件在IIC總線中的從地址,根據(jù)ALT  ADDRESS地址引腳不同修改
  9.                               //ALT  ADDRESS引腳接地時(shí)地址為0x46,接電源時(shí)地址為0xB8
  10. typedef   unsigned char BYTE;
  11. typedef   unsigned short WORD;

  12. extern BYTE    BUF[8];                         //接收數(shù)據(jù)緩存區(qū)             
  13. extern uchar   ge,shi,bai,qian;            //顯示變量
  14. //int     dis_data;                       //變量



  15. //-----------------------------------

  16. //*********************************************************
  17. void conversion(uint temp_data)  //  數(shù)據(jù)轉(zhuǎn)換出 個(gè),十,百,千,萬
  18. {  
  19. uchar   ge,shi,bai,qian;
  20.         qian=temp_data/1000+0x30 ;
  21.     temp_data=temp_data%1000;    //取余運(yùn)算
  22.     bai=temp_data/100+0x30   ;
  23.     temp_data=temp_data%100;     //取余運(yùn)算
  24.     shi=temp_data/10+0x30    ;
  25.     temp_data=temp_data%10;      //取余運(yùn)算
  26.     ge=temp_data+0x30;        
  27. }



  28. /**************************************
  29. 起始信號(hào)
  30. **************************************/
  31. void BH1750_Start()
  32. {
  33.     SDA = 1;                    //拉高數(shù)據(jù)線
  34.     SCL = 1;                    //拉高時(shí)鐘線
  35.     Delay5us();                 //延時(shí)
  36.     SDA = 0;                    //產(chǎn)生下降沿
  37.     Delay5us();                 //延時(shí)
  38.     SCL = 0;                    //拉低時(shí)鐘線
  39. }

  40. /**************************************
  41. 停止信號(hào)
  42. **************************************/
  43. void BH1750_Stop()
  44. {
  45.     SDA = 0;                    //拉低數(shù)據(jù)線
  46.     SCL = 1;                    //拉高時(shí)鐘線
  47.     Delay5us();                 //延時(shí)
  48.     SDA = 1;                    //產(chǎn)生上升沿
  49.     Delay5us();                 //延時(shí)
  50. }

  51. /**************************************
  52. 發(fā)送應(yīng)答信號(hào)
  53. 入口參數(shù):ack (0:ACK 1:NAK)
  54. **************************************/
  55. void BH1750_SendACK(bit ack)
  56. {
  57.     SDA = ack;                  //寫應(yīng)答信號(hào)
  58.     SCL = 1;                    //拉高時(shí)鐘線
  59.     Delay5us();                 //延時(shí)
  60.     SCL = 0;                    //拉低時(shí)鐘線
  61.     Delay5us();                 //延時(shí)
  62. }

  63. /**************************************
  64. 接收應(yīng)答信號(hào)
  65. **************************************/
  66. bit BH1750_RecvACK()
  67. {
  68.     SCL = 1;                    //拉高時(shí)鐘線
  69.     Delay5us();                 //延時(shí)
  70.     CY = SDA;                   //讀應(yīng)答信號(hào)
  71.     SCL = 0;                    //拉低時(shí)鐘線
  72.     Delay5us();                 //延時(shí)

  73.     return CY;
  74. }

  75. /**************************************
  76. 向IIC總線發(fā)送一個(gè)字節(jié)數(shù)據(jù)
  77. **************************************/
  78. void BH1750_SendByte(BYTE dat)
  79. {
  80.     BYTE i;

  81.     for (i=0; i<8; i++)         //8位計(jì)數(shù)器
  82.     {
  83.         dat <<= 1;              //移出數(shù)據(jù)的最高位
  84.         SDA = CY;               //送數(shù)據(jù)口
  85.         SCL = 1;                //拉高時(shí)鐘線
  86.         Delay5us();             //延時(shí)
  87.         SCL = 0;                //拉低時(shí)鐘線
  88.         Delay5us();             //延時(shí)
  89.     }
  90.     BH1750_RecvACK();
  91. }

  92. /**************************************
  93. 從IIC總線接收一個(gè)字節(jié)數(shù)據(jù)
  94. **************************************/
  95. uchar BH1750_RecvByte()
  96. {
  97.     BYTE i;
  98.     BYTE dat = 0;

  99.     SDA = 1;                    //使能內(nèi)部上拉,準(zhǔn)備讀取數(shù)據(jù),
  100.     for (i=0; i<8; i++)         //8位計(jì)數(shù)器
  101.     {
  102.         dat <<= 1;
  103.         SCL = 1;                //拉高時(shí)鐘線
  104.         Delay5us();             //延時(shí)
  105.         dat |= SDA;             //讀數(shù)據(jù)               
  106.         SCL = 0;                //拉低時(shí)鐘線
  107.         Delay5us();             //延時(shí)
  108.     }
  109.     return dat;
  110. }

  111. //*********************************

  112. void Single_Write_BH1750(uchar REG_Address)
  113. {
  114.     BH1750_Start();                  //起始信號(hào)
  115.     BH1750_SendByte(SlaveAddress);   //發(fā)送設(shè)備地址+寫信號(hào)
  116.     BH1750_SendByte(REG_Address);    //內(nèi)部寄存器地址,
  117.   //  BH1750_SendByte(REG_data);       //內(nèi)部寄存器數(shù)據(jù),
  118.     BH1750_Stop();                   //發(fā)送停止信號(hào)
  119. }


  120. //*********************************************************
  121. //
  122. //連續(xù)讀出BH1750內(nèi)部數(shù)據(jù)
  123. //
  124. //*********************************************************
  125. void Multiple_read_BH1750(void)
  126. {   
  127.         BYTE    BUF[8];
  128.         uchar i;       
  129.     BH1750_Start();                          //起始信號(hào)
  130.     BH1750_SendByte(SlaveAddress+1);         //發(fā)送設(shè)備地址+讀信號(hào)
  131.        
  132.          for (i=0; i<3; i++)                      //連續(xù)讀取2個(gè)地址數(shù)據(jù),存儲(chǔ)中BUF
  133.     {
  134.         BUF[i] = BH1750_RecvByte();          //BUF[0]存儲(chǔ)0x32地址中的數(shù)據(jù)
  135.         if (i == 3)
  136.         {

  137.            BH1750_SendACK(1);                //最后一個(gè)數(shù)據(jù)需要回NOACK
  138.         }
  139.         else
  140.         {               
  141.           BH1750_SendACK(0);                //回應(yīng)ACK
  142.        }
  143.    }

  144.     BH1750_Stop();                          //停止信號(hào)
  145.     Delay5ms();
  146. }


  147. //初始化BH1750,根據(jù)需要請(qǐng)參考pdf進(jìn)行修改****
  148. void Init_BH1750()
  149. {
  150.    Single_Write_BH1750(0x01);  

  151. }
復(fù)制代碼
ID:316425 發(fā)表于 2018-6-11 14:33
zhanghyg 發(fā)表于 2018-6-11 08:42
你的主程序呢?BH1750讀出來的數(shù)據(jù)要先處理一下才能顯示。

這是我的主函數(shù),幫我看看吧,非常感謝


  1. #include <reg52.h>
  2. #include <intrins.h>  
  3. #include <math.h>   
  4. #include <stdio.h>
  5. #include "GGYY.H"
  6. #include "lcdd.h"
  7. #define uint unsigned int
  8. #define uchar unsigned char


  9. typedef   unsigned char BYTE;
  10. typedef   unsigned short WORD;
  11.         BYTE    BUF[8];
  12.   uchar code  ge,shi,bai,qian;
  13.         int     dis_data;
  14.         void  Single_Write_BH1750(uchar REG_Address);
  15.         void delay_nms(unsigned int k);
  16.         void Init_BH1750(void);
  17.         void  Multiple_Read_BH1750();
  18.         void conversion(uint temp_data);
  19. void LCD_disp_char(uchar x,uchar y,uchar dat);//??????????????,X(0-15),y(1-2)  
  20. void LCD_disp_str(uchar x,uchar y,uchar *str);
  21. void LCD_init(void);  


  22. //毫秒延時(shí)**************************
  23. void delay_nms(unsigned int k)       
  24. {                                               
  25. unsigned int i,j;                               
  26. for(i=0;i<k;i++)
  27. {                       
  28. for(j=0;j<121;j++)                       
  29. {;}}                                               
  30. }


  31. /**************************************
  32. 延時(shí)5微秒(STC90C52RC@12M)
  33. 不同的工作環(huán)境,需要調(diào)整此函數(shù),注意時(shí)鐘過快時(shí)需要修改
  34. 當(dāng)改用1T的MCU時(shí),請(qǐng)調(diào)整此延時(shí)函數(shù)
  35. **************************************/
  36. void Delay5us()
  37. {
  38.     _nop_();_nop_();_nop_();_nop_();
  39.     _nop_();_nop_();_nop_();_nop_();
  40.         _nop_();_nop_();_nop_();_nop_();
  41.         _nop_();_nop_();_nop_();_nop_();
  42. }

  43. /**************************************
  44. 延時(shí)5毫秒(STC90C52RC@12M)
  45. 不同的工作環(huán)境,需要調(diào)整此函數(shù)
  46. 當(dāng)改用1T的MCU時(shí),請(qǐng)調(diào)整此延時(shí)函數(shù)
  47. **************************************/
  48. void Delay5ms()
  49. {
  50.     WORD n = 560;

  51.     while (n--);
  52. }
  53.        
  54. void main()
  55. {
  56.   float temp;
  57.    delay_nms(100);            //延時(shí)100ms       
  58.    LCD_init();          //初始化LCD
  59.    Init_BH1750();       //初始化BH1750

  60.        
  61.          
  62.         Delay5ms();
  63.   while(1)              //循環(huán)
  64.   {

  65.     Single_Write_BH1750(0x01);   // power on
  66.     Single_Write_BH1750(0x10);   // H- resolution mode

  67.      delay_nms(180);              //延時(shí)180ms

  68.     Multiple_Read_BH1750();       //連續(xù)讀出數(shù)據(jù),存儲(chǔ)在BUF中

  69.     dis_data=BUF[0];
  70.     dis_data=(dis_data<<8)+BUF[1];//合成數(shù)據(jù),即光照數(shù)據(jù)
  71.    
  72.     temp=(float)dis_data/1.2;

  73.     conversion(temp);         //計(jì)算數(shù)據(jù)和顯示
  74.        
  75.                
  76.                 LCD_disp_str(9,0,"L:");
  77.                
  78.     LCD_disp_char(11,0,qian); //顯示數(shù)據(jù)
  79.     LCD_disp_char(12,0,bai);
  80.     LCD_disp_char(13,0,shi);
  81.           LCD_disp_char(14,0,ge);

  82.           LCD_disp_str(15,0,"l"); ////顯示數(shù)單位
  83.                
  84.          
  85.             
  86.   }
  87.        
  88. }
復(fù)制代碼
ID:330198 發(fā)表于 2018-6-11 08:42
你的主程序呢?BH1750讀出來的數(shù)據(jù)要先處理一下才能顯示。

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

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

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