STM32驅動DHT11,配置埠為開樓輸出模式

2021-08-20 14:47:59 字數 4531 閱讀 2617

//---定義結構體

typedef

struct _dht11_handlertype dht11_handlertype;

//---定義指標結構體

typedef

struct _dht11_handlertype *pdht11_handlertype;

//---定義

struct _dht11_handlertype

;//---外部呼叫介面

extern dht11_handlertype g_dht11_0;

extern pdht11_handlertype pdht11_0;

//---變數定義

dht11_handlertype g_dht11_0;

pdht11_handlertype pdht11_0=&g_dht11_0;

/////函    數:

//功    能:

//輸入引數: 

//輸出引數: 

//說    明:

//uint8_t dht11_init

(dht11_handlertype *dht11handlertype,

void(*msgdelayus)(uint32_t delay),

void(*msgdelayms)(uint32_t delay))

//---使能埠時鐘

gpiotask_clock(dht11handlertype->msgport,

1); ll_gpio_inittypedef gpio_initstruct;

//---gpio的初始化

gpio_initstruct.pin = dht11handlertype->msgbit;

//---對應的gpio的引腳

gpio_initstruct.mode =

ll_gpio_mode_output

;//---配置狀態為輸出模式

gpio_initstruct.speed =

ll_gpio_speed_freq_very_high

;//---gpio的速度

gpio_initstruct.outputtype =

ll_gpio_output_opendrain

;//---輸出模式---開漏輸出

gpio_initstruct.pull =

ll_gpio_pull_up

;//---上拉使能

//---初始化埠

ll_gpio_init(dht11handlertype->msgport,

&gpio_initstruct);

#endif

gpio_out_1(dht11handlertype->msgport, dht11handlertype->msgbit);

//---延時函式

if (msgdelayus !=

null)

if (msgdelayms !=

null)

//---訊息函式

dht11handlertype->

msgtask

=null

;return

ok_0;}

/////函    數:

//功    能:

//輸入引數: 

//輸出引數: 

//說    明:

//uint8_t dht11_deinit

(dht11_handlertype *dht11handlertype)

/////函    數:

//功    能:

//輸入引數: 

//輸出引數: 

//說    明:

//uint8_t dht11_reset

(dht11_handlertype *dht11handlertype)

gpio_out_0(dht11handlertype->msgport, dht11handlertype->msgbit);

//---觸發開始,匯流排拉低要大於18ms

if (dht11handlertype->

msgdelayms

!=null)

//---釋放匯流排

gpio_out_1(dht11handlertype->msgport, dht11handlertype->msgbit);

//主機拉高20~40us;等待dht11的低電平響應訊號

if (dht11handlertype->

msgdelayus

!=null)

}///

//函    數:

//功    能:

//輸入引數: 

//輸出引數: 

//說    明:1:不存在;0:存在 

//uint8_t dht11_check

(dht11_handlertype *dht11handlertype)

count++;

if (count>

100)

}if (count>

100)

count =0;

//---dht11高電平資料準備訊號再次拉高40~80us

while (gpio_get_state(dht11handlertype->msgport, dht11handlertype->msgbit) ==0)

count++;

if (count >

100)

}if (count >

100)

return

ok_0;}

/////函    數:

//功    能:

//輸入引數: 

//輸出引數: 

//說    明:1:不存在;0:存在 

//uint8_t dht11_start

(dht11_handlertype *dht11handlertype)

/////函    數:

//功    能:

//輸入引數: 

//輸出引數: 

//說    明:位資料「0」的格式為: 50 微秒的低電平和 26-28 微秒的高電平;

/  位資料「1」的格式為: 50 微秒的低電平加 70微秒的高電平

//uint8_t dht11_readbit

(dht11_handlertype *dht11handlertype)

count++;

if (count >

100)

}count =0;

//---等待變高電平

while (gpio_get_state(dht11handlertype->msgport, dht11handlertype->msgbit) ==0)

count++;

if (count >

100)

}//---等待40us;資料為0的訊號時間為26-28us,1則為70us

if (dht11handlertype->

msgdelayus

!=null)

//---讀取狀態

if (gpio_get_state(dht11handlertype->msgport, dht11handlertype->msgbit) !=0)

return0;

}///

//函    數:

//功    能:

//輸入引數: 

//輸出引數: 

//說    明:

//uint8_t dht11_readbyte

(dht11_handlertype *dht11handlertype)

return _return;

}///

//函 數: 

//功 能: 

//輸入引數: 

//輸出引數: 

//說 明: 

//uint8_t dht11_readdata

(dht11_handlertype *dht11handlertype)

; uint8_t i=0;

for (i=

0;i<

5;i++)

//---資料校驗

if ((temp[0]+temp[1]+temp[2]+temp[3])!=temp[4])

//---溫度整數部分

dht11handlertype->msgwendu = temp[0];

//---溫度小數部分

dht11handlertype->msgwendu = (dht11handlertype->msgwendu<<

8)+temp[1];

//---濕度整數部分

dht11handlertype->msgshidu = temp[2];

//---濕度小數部分

dht11handlertype->msgshidu = (dht11handlertype->msgshidu <<

8) + temp[3];

return

ok_0

;}

STM32驅動DHT11溫濕度感測器

dht11 是一款溼溫度一體化的數字感測器。該感測器包括乙個電阻式測溼元件和乙個 ntc 測溫元件,並與乙個高效能 8 位微控制器相連線。通過微控制器等微處理器簡單的電路連線就能夠 實時的採集本地濕度和溫度。dht11 與微控制器之間能採用簡單的單匯流排進行通訊,僅僅需要一 個 i o 口。感測器內...

STM32外設驅動篇 DHT11溫濕度感測器

已在stm32上進行過測試。本例使用pc0引腳連線dht11的data引腳。main函式中呼叫下面 中的dth11 test函式即可。資料定義 以下變數均為全域性變數 溫度高8位 u8t data h 溫度低8位 u8t data l 濕度高8位 u8rh data h 濕度低8位 u8rh dat...

STM32 HAL庫驅動DHT11讀取溫濕度程式

驅動dht11 dht22 ds18bb20等溫濕度模組時序是比較簡單的,關鍵在於控制好時序的延時時間,hal庫的延時函式hal delay是毫秒級別延時函式,所關鍵點就是實現微秒級別的延時函式。微秒級別延時函式實現見我的另一篇部落格 io方向設定 define dht11 io in pc10 i...