AM3354之DHT11气温湿度传感器驱动和测试

废话不多说,直接上驱动代码,整体就是注册一个misc device 主要是注意时序。
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/slab.h>
#include <asm/gpio.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/gpio.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include <asm/unistd.h>

#define DEVICE_NAME "dht11"

/* Convert GPIO signal to GPIO pin number */
#define GPIO_TO_PIN(bank, gpio) (32 * (bank) + (gpio))

#define DHT11_GPIO GPIO_TO_PIN(1, 26)


/* 读取单字节(8 bit)数据 */
static char DHT11_read_byte ( void )
{
	char DHT11_byte ;
	unsigned char i ;
	unsigned char temp ;
	
	DHT11_byte = 0 ;
	for ( i = 0 ; i < 8 ; i ++ )
	{
		temp = 0 ;
		while ( ! (gpio_get_value(DHT11_GPIO) ) ) 
		{
			temp ++ ;
			if ( temp > 12 )
				return 1 ;
			udelay ( 5 ) ;
		}
		temp = 0 ;
		while ( gpio_get_value(DHT11_GPIO)  )
		{
			temp ++ ;
			if ( temp > 20 )
				return 1 ;
			udelay ( 5 ) ;
		}
		if ( temp > 6 )
		{
			DHT11_byte <<= 1 ;
			DHT11_byte |= 1 ;
		} 
		else
		{
			DHT11_byte <<= 1 ;
			DHT11_byte |= 0 ;
		}
	}
	return DHT11_byte ;
}

/* DHT11 数据读取函数 */

static ssize_t DHT11_read ( struct file* filp, char __user* buf, size_t count, loff_t* f_pos )
{
	unsigned char DataTemp;
	unsigned char i;
	unsigned char err;
	char tempBuf[5];
	//loff_t pos = *f_pos ;
	
	err = 0 ;

	/* 管脚设置为输出并拉低电平 */
	gpio_direction_output(DHT11_GPIO, 0);
	msleep ( 18 );
	//mdelay ( 18 );
	gpio_set_value(DHT11_GPIO, 1);

	udelay ( 40 );
	gpio_direction_input(DHT11_GPIO);
	 
	if ( !err )
	{
		DataTemp = 10 ;
		while ( !( gpio_get_value(DHT11_GPIO) ) && DataTemp )
		{
			DataTemp --;
			udelay ( 10 );
		}
		if ( !DataTemp )
		{
			err = 1;
			count = -EFAULT;
		}
	}
	if ( !err )
	{
		DataTemp = 10 ;
		while ( gpio_get_value(DHT11_GPIO) && DataTemp )
		{
			DataTemp --;
			udelay ( 10 );
		}
		if ( !DataTemp )
		{
			err = 1;
			count = -EFAULT;
		}
	}

	if ( !err )
	{
		for ( i = 0; i < 5; i ++ )
		{
			tempBuf[i] = DHT11_read_byte () ;
		}
		DataTemp = 0 ;
		for ( i = 0; i < 4; i ++ )
		{
			DataTemp += tempBuf[i] ;
		}
		if ( DataTemp != tempBuf[4] )
		{
			count = -EFAULT;
		}
		if ( count > 5 )
		{
			count = 5 ;
		}

		//pos += count;
		//if( copy_to_user ( buf , tempBuf + *f_pos , count ) )
		if ( copy_to_user ( buf , tempBuf , count ) )
		{
			count = -EFAULT ;
		}
		//*f_pos = pos;
	}
	
	/* 管脚设置为输出并拉高 */
	gpio_direction_output(DHT11_GPIO, 1);
	return count;
}


static struct file_operations dev_fops = {
	.owner = THIS_MODULE,
	.read = DHT11_read,
	};
static struct miscdevice misc = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = DEVICE_NAME,
	.fops = &dev_fops,
	};

static int __init DHT11_init_module ( void )
{
	int ret;
	/* 1、申请管脚 */
	ret = gpio_request(DHT11_GPIO, "dht11_gpio");
	if (ret)
		goto free_master;

	/* 2、管脚设置为输出并拉高 */
	gpio_direction_output(DHT11_GPIO, 1);

	/* 3、管脚注册成misc设备 */
	ret = misc_register(&misc);

	printk (DEVICE_NAME"\tinitialized\n");
	
free_master:
	return ret;
}

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

module_init ( DHT11_init_module );
module_exit ( DHT11_exit_module );

MODULE_LICENSE ( "GPL" ) ;
MODULE_AUTHOR ( "zuoyioo7@163.com" ) ;

Makefile文件

KERN_DIR = /home/zuoyi/kernel/3.2.0


all:
        make -C $(KERN_DIR) M=`pwd` modules


clean:
        make -C $(KERN_DIR) M=`pwd` modules clean
        rm -rf modules.order

obj-m   += dht11.o



但insmod dht11.ko驱动文件的时候


/ # cd dev/
/dev # ls -l dht11 
crw-------    1 0        0          10,  52 Jun 27 17:25 dht11
/dev # 


测试程序

#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>

int main ( void )
{
	int fd ;
	int retval ;
	char buf[5] ;
	fd = open ( "/dev/dht11" , O_RDONLY) ;
	if ( fd == -1 )
	{
		perror ( "open dht11 error\n" ) ;
		exit ( -1 ) ;
	}
	printf ( "open /dev/dht11 successfully\n" ) ;
	sleep ( 2 ) ;
	while ( 1 )
	{
		sleep ( 1 ) ;
		retval = read ( fd , buf , 5 );
		if ( retval == -1 )
		{
			perror ( "read dht11 error" ) ;
			printf ( "read dht11 error" ) ;
			exit ( -1 ) ;
		}
		printf ( "Humidity:%d.%d %%RH Temperature:%d.%d C  sum is:%d\n", buf[0], buf[1], buf[2], buf[3],buf[4] ) ;
	}
	close ( fd ) ;
}

运行测试程序


/nfsswap # ./dht11ts 
open /dev/dht11 successfully
Humidity:33.0 %RH Temperature:27.0 C  sum is:60
Humidity:33.0 %RH Temperature:27.0 C  sum is:60
Humidity:33.0 %RH Temperature:27.0 C  sum is:60




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值