华为云IoT智慧物流案例04 | 4G网卡拨号(广和通L610-CAT.1模组)与北斗定位模组(中科微电子)数据解析
回顾
衔接上个章节,稍微调整代码,令1个IIC接口同时接入2个设备并获取数据。
程序修改
//作者:实柏
//日期:2021-6-30
/************************************************************/
//包含头文件
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/ioctl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<sys/select.h>
#include<sys/time.h>
#include<errno.h>
//宏定义
#define SHT30_Address 0x44 //1000100
#define BH1750_Address 0x23 //0100011
#define I2C_RETRIES 0x0701
#define I2C_TIMEOUT 0x0702
#define I2C_SLAVE 0x0703 //IIC从器件的地址设置
#define I2C_BUS_MODE 0x0780
typedef unsigned char uint8;
int fd = -1;
//函数声明
static uint8 dev_Init(void);
static uint8 BH1750_IIC(void);
static uint8 SHT30_IIC(void);
static uint8 BH1750_Dat_To_Lux(uint8* buf);
//
static uint8 dev_Init(void)
{
fd = open("/dev/i2c-0", O_RDWR); // open file and enable read and write
if (fd < 0)
{
perror("Can't open /dev/BH1750 \n"); // open i2c dev file fail
exit(1);
}
printf("open /dev/i2c-0 success !\n"); // open i2c dev file succes
}
static uint8 BH1750_IIC(void)
{
if (ioctl(fd, I2C_SLAVE,BH1750_Address) < 0)
{ //set i2c address
printf("fail to set i2c device slave address!\n");
close(fd);
return - 1;
}
printf("set slave address to 0x%x success!\n", BH1750_Address);