011——人体感应模块驱动开发(SR501)

目录

[一、 模块简介](#一、 模块简介)

[二、 工作原理](#二、 工作原理)

[三、 软件及验证](#三、 软件及验证)


一、 模块简介

人体都有恒定的体温,一般在 37 度,所以会发出特定波长 10uM 左右的红外线,被动式红外探头就是靠探测人体发射的 10uM 左右的红外线而进行工作的。

人体发射的 10uM 左右的红外线通过菲泥尔滤光片增强后聚集到红外感应源上。红外感应源通常采用热释电元件,这种元件在接收到人体红外辐射温度发生变化时就会失去电荷平衡,向外释放电荷,后续电路经检测处理后就能产生报警信号。

人体红外模块是一种能够检测人或动物发射的红外线而输出电信号的传感器。广泛应用于各种自动化控制装置中。比如常见的楼道自动开关、防盗报警等。如果有人在量程内运动, DO 引脚将会输出有效信号。市面上人体红外模块有很多,但其外形和原理都差不多,去年做STM32的项目时用过不少如下是一个典型的人体红外模式原理图:

实物长这个样子

这个罩子是为了扩大检测范围的

之前用不是很灵敏这次试试在linux环境下的驱动程序灵敏度怎么样。

二、 工作原理

通过跳线来设置是否可以重复触发,默认为 L。其中 L 表示不可重复, H 表示可重复。含义如下:

① 不可重复触发方式:

感应到人体并输出高电平后,延时时间一结束,输出将自动从高电平变为低电平。

② 重复触发方式:

感应到人体后输出高电平后,在延时时间段内,如果有人体在其感应范围内活动,其输出将一直保持高电平,直到人离开后才延时将高电平变为低电平(感应模块检测到人体的每一次活动后会自动顺延一个延时时间段,并且以最后一次活动的时间为延时时间的起始点)。可以通过电位器实现封锁时间和检测距离的调节:

① 调节检测距离:

即有效距离的远近。调节距离电位器顺时针旋转,感应距离增大(约 7 米);反之,感应距离减小(约 3 米)。

② 封锁时间:

感应模块在每一次感应输出后(高电平变为低电平),可以紧跟着设置一个封锁时间,在此时间段内感应器不接收任何感应信号。此功能可以实现(感应输出时间和封锁时间)两者的间隔工作,可应用于间隔探测产品;同时此功能可有效抑制负载切换过程中产生的各种干扰。调节延时电位器顺时针旋转,感应延时加长(约 300S),反之,感应延时减短(约 0.5S)。

老师很贴心做了防呆设计

补充信息:

人进入其感应范围则输出高电平,人离开感应范围则自动延时关闭高电平,输出低电平

工作电压:DC4.5V~20V

静态电流:<50uA

电平输出:高3.3V/低0V0

延时时间:8s~200s(可调)

封锁时间:2.5s

触发方式:可重复触发/不可重复触发(通过跳线帽选择)感应角度:<100度锥角

电路板外形尺寸:32mm*24mm

工作温度:-15~+70°℃

感应透镜尺寸:直径23mm

小于120度锥角,7米以内

VCC:电源正极

GND:电源负极

OUT:信号输出

隐藏接口

RT:温度补偿接口(可接热敏电阻,在夏天当环境温度升高至30~32°℃,探测距离稍变短,温度补

偿可作一定的性能补偿。)

RL(CDS):光敏控制接口(可接光敏电阻,白天或者光线较强时控制模块不感应)

三、 软件及验证

一番紧张刺激的配置网络后给大家推荐个新工具

很好用谁用谁知道。

这样一个终端可以控制所有的机器。

模块有点不灵敏呢。

我们接线用了拓展资源板

  • GPIO4 ==> 第3组 ===> 起始编号 = 3*32 = 96

  • GPIO4_19的编号:96+19=115

cpp 复制代码
#include <linux/module.h>
#include <linux/poll.h>

#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/slab.h>
#include <linux/fcntl.h>
#include <linux/timer.h>

struct gpio_desc{
	int gpio;
	int irq;
    char *name;
    int key;
	struct timer_list key_timer;
} ;

static struct gpio_desc gpios[2] = {
    {115, 0, "sr501", },
};

/* 主设备号                                                                 */
static int major = 0;
static struct class *gpio_class;

/* 环形缓冲区 */
#define BUF_LEN 128
static int g_keys[BUF_LEN];
static int r, w;

struct fasync_struct *button_fasync;

#define NEXT_POS(x) ((x+1) % BUF_LEN)

static int is_key_buf_empty(void)
{
	return (r == w);
}

static int is_key_buf_full(void)
{
	return (r == NEXT_POS(w));
}

static void put_key(int key)
{
	if (!is_key_buf_full())
	{
		g_keys[w] = key;
		w = NEXT_POS(w);
	}
}

static int get_key(void)
{
	int key = 0;
	if (!is_key_buf_empty())
	{
		key = g_keys[r];
		r = NEXT_POS(r);
	}
	return key;
}


static DECLARE_WAIT_QUEUE_HEAD(gpio_wait);


/* 实现对应的open/read/write等函数,填入file_operations结构体                   */
static ssize_t gpio_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
	//printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	int err;
	int key;

	if (is_key_buf_empty() && (file->f_flags & O_NONBLOCK))
		return -EAGAIN;
	
	wait_event_interruptible(gpio_wait, !is_key_buf_empty());
	key = get_key();
	err = copy_to_user(buf, &key, 4);
	
	return 4;
}


static unsigned int gpio_drv_poll(struct file *fp, poll_table * wait)
{
	//printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	poll_wait(fp, &gpio_wait, wait);
	return is_key_buf_empty() ? 0 : POLLIN | POLLRDNORM;
}

static int gpio_drv_fasync(int fd, struct file *file, int on)
{
	if (fasync_helper(fd, file, on, &button_fasync) >= 0)
		return 0;
	else
		return -EIO;
}


/* 定义自己的file_operations结构体                                              */
static struct file_operations gpio_key_drv = {
	.owner	 = THIS_MODULE,
	.read    = gpio_drv_read,
	.poll    = gpio_drv_poll,
	.fasync  = gpio_drv_fasync,
};


static irqreturn_t gpio_key_isr(int irq, void *dev_id)
{
	struct gpio_desc *gpio_desc = dev_id;
	int val;
	int key;

	printk("gpio_key_isr key %d irq happened\n", gpio_desc->gpio);

	val = gpio_get_value(gpio_desc->gpio);

	//printk("key_timer_expire key %d %d\n", gpio_desc->gpio, val);
	key = (gpio_desc->key) | (val<<8);
	put_key(key);
	wake_up_interruptible(&gpio_wait);
	kill_fasync(&button_fasync, SIGIO, POLL_IN);

	return IRQ_HANDLED;
}


/* 在入口函数 */
static int __init gpio_drv_init(void)
{
    int err;
    int i;
    int count = sizeof(gpios)/sizeof(gpios[0]);
    
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	
	for (i = 0; i < count; i++)
	{		
		gpios[i].irq  = gpio_to_irq(gpios[i].gpio);

		err = request_irq(gpios[i].irq, gpio_key_isr, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, gpios[i].name, &gpios[i]);
	}

	/* 注册file_operations 	*/
	major = register_chrdev(0, "100ask_gpio_key", &gpio_key_drv);  /* /dev/gpio_desc */

	gpio_class = class_create(THIS_MODULE, "100ask_gpio_key_class");
	if (IS_ERR(gpio_class)) {
		printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
		unregister_chrdev(major, "100ask_gpio_key");
		return PTR_ERR(gpio_class);
	}

	device_create(gpio_class, NULL, MKDEV(major, 0), NULL, "sr501"); /* /dev/sr501 */
	
	return err;
}

/* 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数
 */
static void __exit gpio_drv_exit(void)
{
    int i;
    int count = sizeof(gpios)/sizeof(gpios[0]);
    
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);

	device_destroy(gpio_class, MKDEV(major, 0));
	class_destroy(gpio_class);
	unregister_chrdev(major, "100ask_gpio_key");

	for (i = 0; i < count; i++)
	{
		free_irq(gpios[i].irq, &gpios[i]);
	}
}


/* 7. 其他完善:提供设备信息,自动创建设备节点                                     */

module_init(gpio_drv_init);
module_exit(gpio_drv_exit);

MODULE_LICENSE("GPL");
cpp 复制代码
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <poll.h>
#include <signal.h>

static int fd;

/*
 * ./button_test /dev/sr501
 *
 */
int main(int argc, char **argv)
{
	int val;
	struct pollfd fds[1];
	int timeout_ms = 5000;
	int ret;
	int	flags;

	int i;
	
	/* 1. 判断参数 */
	if (argc != 2) 
	{
		printf("Usage: %s <dev>\n", argv[0]);
		return -1;
	}


	/* 2. 打开文件 */
	fd = open(argv[1], O_RDWR);
	if (fd == -1)
	{
		printf("can not open file %s\n", argv[1]);
		return -1;
	}

	while (1) 
	{
		if (read(fd, &val, 1) > 0)
		{
			if (val  == 0x100)
			{
				printf("get button: %#x ,有人\n", val);
			}
			else
			{
				printf("get button: %#x ,无人\n", val);
			}
		}
		else
			printf("get button: -1\n");
	}


	close(fd);
	
	return 0;
}

这个传感器给的高电平的值居然是100不是1,不对不是传感器的锅,可能是韦东山老师驱动的bug,应用层我做了对应修改,这几天把驱动都捋一下然后我集中改一下这些bug然后对项目做适配。站在巨人的肩膀上就好哈哈。

相关推荐
A小辣椒2 天前
TShark:Wireshark CLI 功能
linux
A小辣椒2 天前
TShark:基础知识
linux
AlfredZhao2 天前
OCI 明明分配了 200G 系统盘,为什么 df 只看到 30G?
linux·oci
AlfredZhao3 天前
vi 删除指定范围的行,不用再反复按 dd
linux·vi
用户9718356334663 天前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
猪脚踏浪3 天前
linux 拷贝文件或目录到指定的位置
linux
摇滚侠4 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
bush44 天前
嵌入式linux学习记录十四、术语
linux·嵌入式
载数而行5204 天前
Linux 11 动态监控指令top
linux
不会C语言的男孩4 天前
Linux 系统编程 · 第 8 章:进程基础
linux·c语言