驱动:dht11驱动程序

DHT11读写时序详解(时序+上板实测)_mcu 读取thd11 接收数据开始-CSDN博客文章浏览阅读733次,点赞20次,收藏21次。DHT11有两种,一种三线制,一种四线制。其中都包括1个VDD,1个GND,和1个DATA串行数据引脚。不同的是四线制其中一个引脚悬空。_mcu 读取thd11 接收数据开始https://blog.csdn.net/weixin_53042232/article/details/141131210?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522E8679AEF-F11D-47FF-B55F-38AC4ADAF96A%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=E8679AEF-F11D-47FF-B55F-38AC4ADAF96A&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~rank_v31_ecpm-1-141131210-null-null.nonecase&utm_term=DHT11%E8%AF%BB%E5%86%99%E6%97%B6%E5%BA%8F%E8%AF%A6%E8%A7%A3%EF%BC%88%E6%97%B6%E5%BA%8F%2B%E4%B8%8A%E6%9D%BF%E5%AE%9E%E6%B5%8B%EF%BC%89&spm=1018.2226.3001.4450

内核: vi drivers/char/dht11.c 

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <asm/io.h>
#include <asm/string.h>
#include <asm/uaccess.h>
#include <linux/miscdevice.h>
#include <asm-generic/errno-base.h>
#include <mach/gpio-nrs.h>
#include <mach/gpio.h>
#include <linux/delay.h>

#define DEV_NAME "dht11"
#define PIN_DHT11 S3C2410_GPF(6)

static void pin_init(void)
{
	gpio_request(PIN_DHT11, "dht11");
}

static void dht11_start(void)
{
	gpio_direction_output(PIN_DHT11, 0);
	mdelay(20);
	gpio_set_value(PIN_DHT11, 1);
	udelay(30);
	gpio_direction_input(PIN_DHT11);
}

static int dht11_wait_repon(void)
{
	int time = 100;
	while(gpio_get_value(PIN_DHT11))   // wait H end
	{
		udelay(1);
		if(!time--)
			return -1;
	}

	time = 100;
	while(!gpio_get_value(PIN_DHT11) && time)  // wait 80us L end
	{
		udelay(1);
		if(!time--)
			return -2;
	}

	time = 100;
	while(gpio_get_value(PIN_DHT11))   // wait 80us H end
	{
		udelay(1);
		if(!time--)
			return -3;
	}

	return 0;
}

static int dht11_get_bit(void)
{
	int time = 100;
	while(!gpio_get_value(PIN_DHT11))  // wait 50us L end
	{
		udelay(1);	
		if(!time--)
			return -1;
	}

	udelay(30);

	if(0 == gpio_get_value(PIN_DHT11))
		return 0;

	while(gpio_get_value(PIN_DHT11))   // wait 40us H end
	{
		udelay(1);	
		if(!time--)
			return -2;
	}

	return 1;
}

static int dht11_get_value(unsigned char * data)
{
	int i = 0;
	int j = 0;
	unsigned char sum = 0;
	for(i = 0; i < 5; i++)
	{
		data[i] = 0;
		for(j = 0; j < 8; j++)
		{
			char bit = dht11_get_bit();	
			if(bit < 0)
				return bit;
			data[i] <<= 1;
			data[i] |= bit;
		}
	}

	for(i = 0; i < 4; i++)
	{
		sum += data[i];
	}

	if(sum == data[4])
		return 4;
	else
		return -1;
}

static int open (struct inode * inode, struct file * file)
{
	pin_init();
	printk("dht11 open ...\n");
	return 0;
}

static ssize_t read (struct file * file, char __user * buf, size_t len, loff_t * offset)
{
	unsigned char data[4] = {0};
	int ret = 0;
	dht11_start();
	ret = dht11_wait_repon();
	if(ret < 0)
		goto err_wait;

	ret = dht11_get_value(data);
	if(ret < 0)
		goto err;

	copy_to_user(buf, &data, sizeof(data));

	printk("dht11 read ...\n");
	return sizeof(data);

err_wait:
	printk("dht11_wait_repon err ... \n");
	return ret;

err:
	printk("dht11_get_value err ...\n");
	return ret;
}

static ssize_t write (struct file * file, const char __user * buf, size_t len, loff_t * offset)
{
	return 0;
}

static int close (struct inode * inode, struct file * file)
{
	printk("dht11 close ...\n");
	return 0;
}

static struct file_operations fops = 
{
	.owner = THIS_MODULE,
	.open = open,
	.read = read,
	.write = write,
	.release = close
};

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

static int __init dht11_init(void)
{
	int ret = misc_register(&misc);
	if(ret < 0)
		goto err_misc_register;

	printk("dht11_init  ...\n");
	return ret;

err_misc_register:
	misc_deregister(&misc);
	printk("dht11 misc_register faidht11\n");	
	return ret;
}

static void __exit dht11_exit(void)
{
	misc_deregister(&misc);
	printk("dht11_exit  ###############################\n");
}

module_init(dht11_init);
module_exit(dht11_exit);
MODULE_LICENSE("GPL");

应用层:~/nfs/rootfs$ vi dht11_app.c

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

int main(int argc, const char *argv[])
{
	int fd = open("/dev/dht11",O_RDWR);
	if(fd < 0 )
	{
		perror("open dht11");
		return -1;
	}
	while(1)
	{
		unsigned char data[4] = {0};
		int ret = read(fd,data,sizeof(data));
		printf("ret = %d\n",ret);
		int i = 0;
		for(i = 0; i < 4; ++i)
		{
			printf("%x\t",data[i]);
		}
		printf("\n");
		sleep(2);
	}
	close(fd);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值