資源簡(jiǎn)介
C語(yǔ)言,基于飛凌OK6410開(kāi)發(fā)板的DHT11驅(qū)動(dòng),內(nèi)有詳細(xì)注釋

代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#define?GPC_7?S3C64XX_GPC(7)
static?volatile?unsigned?char?values[4]={0000};
static?struct?class?*?dhtdrv_class;
static?struct?device?*?dht11_drv_device;
volatile?unsigned?long?*gpccon?=?NULL;
volatile?unsigned?long?*gpcdat?=?NULL;
int?major;
static?int?dht11_drv_open(struct?inode?*inodestruct?file?*?file)
{
/*設(shè)置GPC7為輸出引腳*/
*gpccon?&=?~(?0xf?<(4*7));
/*GPC7輸出高電平*/
*gpcdat?|=?(1<<7);
return?0;
}
static?char?dht11_drv_read_byte?(?void?)
{
char?dht11_byte?;
unsigned?char?i?;
unsigned?char?temp?;
dht11_byte?=?0?;
/*接收數(shù)據(jù),并利用|和&存儲(chǔ)到dht11_byte*/
for?(?i?=?0?;?i?8?;?i?++?)
{
temp?=?0?;
/*50us傳輸間隔時(shí)間*/
while?(?!?(gpio_get_value?(?GPC_7?)?)?)
{
temp?++?;
if?(?temp?>?12?)
return?1?;
udelay?(?5?)?;
}
temp?=?0?;
/*根據(jù)接收到高電平持續(xù)的時(shí)長(zhǎng)判斷數(shù)據(jù)是0還是1*/
while?(?gpio_get_value?(?GPC_7?)?)
{
temp?++?;
if?(?temp?>?20?)
return?1?;
udelay?(?5?)?;
}
/*大于6的話,也就是大于35us,數(shù)據(jù)為1,否則數(shù)據(jù)為0*/
if?(?temp?>?6?)
{
dht11_byte?<<=?1?;
dht11_byte?|=?1?;
}?
else
{
dht11_byte?<<=?1?;
dht11_byte?|=?0?;
}
}
return?dht11_byte?;
}
static?ssize_t?dht11_drv_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];
err?=?0?;
/*設(shè)置GPC7為輸出引腳*/
*gpccon?&=?~(?0xf?<(?4*7)?);
*gpccon?|=?(?0x1?<(4*7));
/*GPC7輸出低電平*/
*gpcdat?&=?~(?1<<7?);
/*
等同于
s3c_gpio_cfgpin?(?GPC_7??S3C_GPIO_OUTPUT?);
gpio_set_value?(?GPC_7??0?);
*/
/*延時(shí)18毫秒,用于DHT11從待機(jī)狀態(tài)進(jìn)入工作狀態(tài)*/
msleep?(?18?);//msleep()休眠延時(shí)函數(shù),并不精確,實(shí)際延時(shí)時(shí)間會(huì)大于設(shè)置值
/*GPC7輸出高電平*/
*gpcdat?|=?(?1<<7?);
//gpio_set_value?(?GPC_7??1?);
udelay?(?40?); //udelay()忙等待函數(shù)延遲過(guò)程中無(wú)法運(yùn)行其他任務(wù).這個(gè)延遲的時(shí)間是準(zhǔn)確但是浪費(fèi)CPU,不適用魚(yú)長(zhǎng)時(shí)間延時(shí)。
/*設(shè)置GPC7為輸入引腳*/
*gpccon?&=?~(?0xf?<(?4*7?));
/*
等同于
s3c_gpio_cfgpin?(?GPC_7??S3C_GPIO_INPUT?);
*/
/*
這時(shí)候DHT會(huì)發(fā)出80us的低電平信號(hào),利用while等待信號(hào)結(jié)束。
下面這個(gè)函數(shù)在此間拔出?DHT11的話,將會(huì)進(jìn)入死循環(huán),
所以需要添加一個(gè)判斷。
while?(?!(?gpio_get_value?(?GPC_7?)?)?)
;
*/
while?(?!(?gpio_get_value?(?GPC_7?)?)?&&?DataTemp?)
{
DataTemp?--;
udelay?(?10?);
}
if?(?!DataTemp?)
{
err?=?1;
count?=?-EFAULT;
}
/*
同上,消掉80us的高電平信號(hào)。
while?(?(?gpio_get_value?(?GPC_7?)?)?)
;
*/
if?(?!err?)
{
DataTemp?=?10?;
while?(?(?gpio_get_value?(?GPC_7?)?)?&&?DataTemp?)
{
DataTemp?--;
udelay?(?10?);
}
if?(?!DataTemp?)
{
err?=?1;
count?=?-EFAULT;
}
}
if(?!err?)
{
/*利用數(shù)組接收數(shù)據(jù)
分別是:
8bit濕度整數(shù)數(shù)據(jù)
8bit濕度小數(shù)數(shù)據(jù)
8bi溫度整數(shù)數(shù)據(jù)
8bit溫度小數(shù)數(shù)據(jù)
8bit校驗(yàn)和
*/
for?(?i?=?0;?i?5
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件????????162??2014-12-21?10:25??Makefile
?????文件????????609??2014-12-21?10:25??open.c
?????文件???????4622??2014-12-21?10:25??dht11_ok6410.c
-----------?---------??----------?-----??----
?????????????????5393????????????????????3
評(píng)論
共有 條評(píng)論