/**
*@breif the BH1750 driver and test demo
*/
#include <stdio.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>
#include <errno.h>
//#define I2C_ADDR 0x5c
#define I2C_ADDR 0X23 //the BH1750 device i2c address
/**
@breif main for get light
*/
int main(void)
{
int fd = 0;
char buf[3] = {0,};
char val = 0;
char value = 0;
float flight = 0.0;
/*open the devices for light sensor*/
fd=open("/dev/i2c-2",O_RDWR);
if(fd<0)
{
printf("err open file:%s\r\n",strerror(errno));
return 1;
}
/*set the I2C slave device address*/
if(ioctl( fd,I2C_SLAVE,I2C_ADDR)<0 )
{
printf("ioctl error : %s\r\n",strerror(errno));return 1;
}
/*config the BH1750 */
val=0x01;
if(write(fd,&val,1)<0)
{
printf("write 0x01 err\r\n");
}
/*config the BH1750 */
val=0x10;
if(write(fd,&val,1)<0)
{
printf("write 0x10 err\r\n");
}
/*loop to read the light value */
while(1)
{
if(read(fd,&buf,3)) /*read the light value from bh1750*/
{
flight=(buf[0]*256+buf[1])/1.2; /*calculate the real light power*/
printf("The current light is %6.3f lx\r\n",flight);
}
else
{
printf("read light error\r\n");
}
usleep(100000);//sleep 0.1s
}
}
基于IIC的應(yīng)用程序的Demo(BH1750)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。