给GY-30数字光强测量传感器写了一个驱动,根据IIC通信协议和GY-30的数据手册编写,期间一个问题缠绕我许久:每次接收完数据时,主机向从机发送一个ACK,很必要,要不然测出的数据不准确。我把测出的数据显示在了LCD1602上,并且可以根据光强值来控制灯的开关。后续我会推出控制灯的亮度的设计。数据手册和代码献上。
单片机源程序如下:
- #include "lcd.h"
- #include "BH1750FVI.h"
- uchar brach[] = {"Lighty Intensity"};
- uchar str[] = {"Lx"};
- uint dat;
- float Light_intensity = 0.0f;
- uchar Wr, Qr, Br, Sr, Gr; //光强值的万位,千位,百位,十位,个位
- //uchar respond =0; //IIC调试用
- sbit Light = P2^2;
- void delay_ms(unsigned int k)
- {
- unsigned int i, j;
- for(i = 0; i < k; i++)
- {
- for(j = 0; j < 121; j++);
- }
- }
- void Sys_Init(void)
- {
- Light = 1;
- LcdInit();
- BH1750_Init();
- }
- void Set_BH1750(void)
- {
- WriteData_to_BH1750(0x01);
- // respond = c;
- WriteData_to_BH1750(0x10); //设置为高分辨率模式
- // respond += 0x30;
- delay_ms(180);
- }
- void datapros(uint Light_intensity)
- {
- Wr = Light_intensity / 10000;
- Wr = Wr + 0x30;
- Qr = Light_intensity % 10000 / 1000;
- Qr = Qr + 0x30;
- Br = Light_intensity % 1000 / 100;
- Br = Br + 0x30;
- Sr = Light_intensity % 100 / 10;
- Sr = Sr + 0x30;
- Gr = Light_intensity % 10;
- Gr = Gr + 0x30;
- }
- void main(void)
- {
- Sys_Init();
- while(1)
- {
- LcdShowStr(0,0,brach);
- Set_BH1750();
- // DisplayOneChar(12,1,respond); IIC调试用
- RecvData_f_BH1750(); //连续读数据
- dat = buff[0];
- dat = (dat << 8) | buff[1];
- Light_intensity = (float)dat / 1.2;
- datapros(Light_intensity); //数据处理
- DisplayOneChar(3,1,Wr);
- DisplayOneChar(4,1,Qr);
- DisplayOneChar(5,1,Br);
- DisplayOneChar(6,1,Sr);
- DisplayOneChar(7,1,Gr);
- LcdShowStr(9,1,str);
- if(Light_intensity <= 20)
- Light = 0;
- else
- Light = 1;
- }
- }
复制代码
所有资料51hei提供下载:
数字光强测量计.rar
(40.75 KB, 下载次数: 28)
bh1750FVI-中文数据手册.pdf
(1.5 MB, 下载次数: 14)
|