Linux bh1750用户层驱动(I2C)

一、linux 应用层编写i2c驱动说明

在没有内核源码树不能直接写驱动时,可以通过i2c总线编写i2c驱动(用户层),通过分析linux i2c驱动总程序发现,通过调用用户层函数编写i2c设备驱动程序步骤如下:
1.打开对应的i2c适配器(/dev/i2c-x)
2.通过ioctl设置从机地址(i2c ioctl使用说明)
3.初始化设备
4.数据交互

二、bh1750 i2c实例

以下程序为6818开发板平台下 bh1750的应用层i2c驱动程序

//******************** NG.Respate(C)  **********************************
//* 产品名称: Respate 科技
//* 文件名称: led.c
//* 程序作者: 雷帕特公司(南宫子萍)
//* 程序版本: V1.1														   
//* 编制日期: 2019/1/29
//* 功能描述: led应用层驱动
//* QQ:	2085827949
//**********************************************************************/

/*************************************************
*头文件
*************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <strings.h>
#include <linux/i2c-dev.h>
#include <linux/i2c.h>

// BH1750 指令
#define BH_POW_DOWN		0x00      // 关闭模块
#define BH_POW_ON		0x01      // 打开模块等待测量指令
#define BH_RST       	0x07      // 重置数据寄存器值在PowerOn模式下有效
#define BH_MODE_H1      0x10      // 高分辨率 单位1lx 测量时间120ms
#define BH_MODE_H2		0x11      // 高分辨率模式2 单位0.5lx 测量时间120ms
#define BH_MODE_L		0x13      // 低分辨率 单位4lx 测量时间16ms
#define BH_SIG_MODE_H1	0x20      // 一次高分辨率 测量 测量后模块转到 PowerDown模式
#define BH_SIG_MODE_H2	0x21      // 同上类似
#define BH_SIG_MODE_L	0x23      // 上类似
#define SENSOR_I2C_ADDR 0x23      // 传感器地址

static int fd = -1;	//驱动文件设备描述符

/*********************************************************************************
** 函数名称: lap_close
** 功能描述: 关闭驱动文件(自动调用)
** 输入参数: None.        
** 输出参数: None.
** 返回参数: None.
**********************************************************************************/  
void lap_close(void)
{
    if(fd>=0) close(fd);
	printf("lap_close fd closeed!\n");
}


/*********************************************************************************
** 函数名称: lap_opend
** 功能描述: 打开驱动文件
** 输入参数: src:驱动路径        
** 输出参数: None.
** 返回参数: None.
**********************************************************************************/  
int lap_opend(const char *src)
{
	fd = open(src,O_RDWR);
	if(fd < 0)
	{
		printf("lap_opend open \"%s\" err!\n",src);
		return -1;
	}
	printf("lap_opend open \"%s\" ok!\n",src);

	//退出时自动关闭驱动文件
	atexit(lap_close);	
	
	return fd;
}

/*********************************************************************************
** 函数名称: bh1750_init
** 功能描述: bh1750初始化      
** 输出参数: None.
** 返回参数: None.
**********************************************************************************/  
void bh1750_init()
{
    int i=0;
    char buff[] = {BH_POW_ON,BH_RST,BH_MODE_L};
	// 2.设置从机地址
    ioctl(fd,I2C_SLAVE,SENSOR_I2C_ADDR,0x00);
    
    // 3.初始化设备
    for(i=0;i<sizeof(buff);i++)
    {
         write(fd,&buff[i],1);
        usleep(1000);   // 休眠一秒
    }
   
}

/*********************************************************************************
** 函数名称: bh1750_readData
** 功能描述: bh1750读取数据      
** 输出参数: None.
** 返回参数: None.
**********************************************************************************/  
float bh1750_readData()
{
    char buff[2];
    if (read(fd,buff,2) <0) return 0;
    return (float)(((buff[0]*256)+buff[1])/1.2); //合成光照强度数据 单位:LX
}


int main(void)
{
    
    float dat;
    // 1.打开i2c适配器(bh150挂接到i2c-1上)
    if(lap_opend("/dev/i2c-1") < 0) return -1;
	
    bh1750_init();
    while (1)
    {
        sleep(1);   // 休眠一秒
        dat = bh1750_readData();
        printf("dat:%0.3f\r\n",dat);
    }

	return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值