基于NanoPi的光感传感器驱动开发

NanoPi开发板资源

NanoPi WiKi链接

光线传感器

光敏传感器原理图


描述

1 可以检测周围环境的亮度和光强
2 灵敏度可调(图中蓝色数字电位器调节)
4 工作电压3.3V-5V
5 输出形式   a 模拟量电压输出
             b 数字开关量输出(016 设有固定螺栓孔,方便安装
7 小板PCB尺寸:3cm * 1.6cm
8 电源指示灯(红色)和数字开关量输出指示灯(绿色)
9 比较器采用LM393芯片,工作稳定

小板接口说明(4线制)

1 VCC  外接3.3V-5V
2 GND  外接GND
3 DO   小板数字量输出接口(014 AO   小板模拟量输出接口

使用说明

1 光敏电阻对光照最敏感,一般用来检测周围环境的亮度和光强。 
2 小板数字量输出接口可以与单片机IO口直接相连
3 小板模拟量输出方式和AD转换处理,可以获得更高的精度 

模块与开发板连接图

光感模块驱动源码

#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/uaccess.h>
#include <linux/list.h>

#include <linux/clk.h>
#include <linux/io.h>
#include <linux/gpio.h>
#include <plat/gpio-cfg.h>
#include <mach/gpio-samsung.h>

#define DEVICE_NAME "2451_light"

//nanopi2451
#define LGPIO S3C2410_GPG(7)        //模块GPIO脚


static int light_read(struct file *file, char * buffer, size_t count, loff_t * ppos)
{
    unsigned tmp;   
    unsigned long err;          
    tmp = gpio_get_value(LGPIO);
//  printk("==%d==\n",tmp);
    err = copy_to_user(buffer, &tmp, 1);  
    return 1;
}


static struct file_operations dev_fops={
    read:light_read,
};

static struct miscdevice misc = {
    .minor = MISC_DYNAMIC_MINOR,
    .name = DEVICE_NAME,
    .fops = &dev_fops,
};

static int __init my_light_init(void)
{
    int ret;
    s3c_gpio_cfgpin(LGPIO, S3C_GPIO_SFN(0));//设置io为输入
    s3c_gpio_setpull(LGPIO, S3C_GPIO_PULL_DOWN);//设置下拉电阻

    ret = misc_register(&misc);
    printk (DEVICE_NAME"\tinitialized\n");

    return ret; 
}

static void __exit my_light_exit(void)
{
    misc_deregister(&misc);
}

module_init(my_light_init);
module_exit(my_light_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("TONY www.91arm.com");

内核配置编译驱动模块

首先修改内核源码中字符设备目录的内核配置文件Kconfig与Makefile.

linux-4.x.y/drivers/char/Kconfig

config NANO_LIGHT
    tristate "nanoPi2451 light device GPG7"
    depends on MACH_MINI2451
    help
        91arm.com

linux-4.x.y/drivers/char/Makefile

 obj-$(CONFIG_NANO_LIGHT)        += nano2451_light.o

注意事项:Kconfig文件中的 NANO_LIGHT 同Makefile文件中CONFIG_ NANO_LIGHT要统一命名。
makefile中指定要生成nano2451_light.o文件,对应的当前目录下要存在nano2451_light.c的驱动程序源文件。

配置内核模块编译

make menuconfig

内核如果已经通过make命令编译完成,可以通过make modules 命令只编译模块,生成nano2451_light.ko文件存放在driver/char目录。拷贝到开发板任一目录 。

加载运行驱动模块

insmod nano2451_light.ko

驱动测试程序源码

#include     <stdio.h>      
#include     <stdlib.h>     
#include     <unistd.h>     
#include     <sys/types.h>  
#include     <sys/stat.h>   
#include     <fcntl.h>      
#include     <errno.h> 

#define DEV_FILE "/dev/2451_light"

int main()
{
    int fd_dev=-1;
    char dat;
    printf("nanoPi driver Test\n");

    fd_dev = open(DEV_FILE,O_RDWR);
    if(fd_dev<0){
        printf("open device err\n");
        return 0;
    }

    while(1){
        read(fd_dev,&dat,1);
        printf("Light status [%d]\n",dat);
        sleep(1);
    }

    return 0;
}

运行测试程序

程序下载到开发板,增加执行权限。

chmod +x   light_test

加载驱动后,运行测试程序。

./light_test

结果如下,光线正常输出0,光线暗输出1。

root@nanopi:/home# ./light_test 
nanoPi driver Test
Light status [0]
Light status [0]
Light status [0]
Light status [0]
Light status [0]
Light status [1]
Light status [1]
Light status [1]

Copyright © 2016 www.91arm.com 【91创客学堂】