【IAR工程】STM8S基于ST标准库读取DHT11数据
✨申明:本文章仅发表在CSDN网站,任何其他网站,未注明来源,见此内容均为盗链和爬取,请多多尊重和支持原创! 🍁对于文中所提供的相关资源链接将作不定期更换。 🔖基于ST STM8S/A标准外设库:STSW-STM8069,版本号:2.3.1
📌STSW-STM8069官方资源下载地址:https://www.st.com/zh/embedded-software/stsw-stm8069.html
🔧IAR编译器版本:IAR Assembler for STMicroelectronics STM8 3.11.1
📌STM8S207/208RBT6最小系统板:https://oshwhub.com/perseverance51/stm8s207rbt6-kai-fa-ban
🎯本工程使用STM8S208RB+DHT11实物验证没有问题。
🌿温湿度传感器
📑引脚定义
✨如果是其他型号可以根据自由更换其他引脚。注意修改相关定义。
STM8S单片机-- > DHT11
PC2-- > DAT
3.3 V-- -- VCC
📓STM8驱动代码
# include "dht11.h"
static uint8_t debug[ 5 ] = { 0 } ;
static uint8_t dht11_bit_get ( void )
{
uint8_t temp = 0 ;
for ( int j = 0 ; j < 8 ; j++ )
{
while ( DHT11_READ == RESET ) ;
delay_us ( 40 ) ;
temp <<= 1 ;
if ( DHT11_READ != RESET )
{
temp |= 0x01 ;
}
while ( DHT11_READ != RESET ) ;
}
return temp;
}
static BitStatus dht11_data_get ( uint8_t * data )
{
DHT11_OUT;
DHT11_H;
delay_ms ( 2 ) ;
DHT11_L;
delay_ms ( 25 ) ;
DHT11_H;
delay_us ( 30 ) ;
DHT11_IN;
if ( DHT11_READ != RESET )
{
return RESET;
}
while ( DHT11_READ == RESET ) ;
while ( DHT11_READ != RESET ) ;
for ( int i = 0 ; i < 5 ; i++ )
{
data[ i] = dht11_bit_get ( ) ;
}
DHT11_OUT;
DHT11_H;
return SET;
}
BitStatus dht11_read_data ( double * temp, double * hum )
{
uint8_t dht11_data[ 5 ] = { 0 } ;
if ( ! dht11_data_get ( dht11_data ) )
return RESET;
memcpy ( debug, dht11_data, 5 ) ;
if ( dht11_data[ 0 ] + dht11_data[ 1 ] + dht11_data[ 2 ] + dht11_data[ 3 ] != dht11_data[ 4 ] )
return RESET;
* hum = dht11_data[ 0 ] + dht11_data[ 1 ] * 0.1 ;
* temp = dht11_data[ 2 ] + dht11_data[ 3 ] * 0.1 ;
return SET;
}
# ifndef _DHT11_H
# define _DHT11_H
# include <stm8s.h>
# include "delay.h"
# include <string.h>
# define DHT11_PORT GPIOC
# define DHT11_PIN GPIO_PIN_2
# define DHT11_OUT GPIO_Init ( DHT11_PORT, DHT11_PIN, GPIO_MODE_OUT_PP_HIGH_FAST)
# define DHT11_IN GPIO_Init ( DHT11_PORT, DHT11_PIN, GPIO_MODE_IN_PU_NO_IT)
# define DHT11_H GPIO_WriteHigh ( DHT11_PORT, DHT11_PIN)
# define DHT11_L GPIO_WriteLow ( DHT11_PORT, DHT11_PIN)
# define DHT11_READ GPIO_ReadInputPin ( DHT11_PORT, DHT11_PIN)
BitStatus dht11_read_data ( double * temp, double * hum) ;
# endif
📝主程序代码
# include "stm8s.h"
# include "delay.h"
# include "led.h"
# include "usart.h"
# include "dht11.h"
# include <stdio.h>
int main ( void )
{
u8 i = 0 ;
double temp, hum;
disableInterrupts ( ) ;
CLK_SYSCLKConfig ( CLK_PRESCALER_HSIDIV1 ) ;
LED_Init ( ) ;
USART1_Init ( 9600 ) ;
while ( dht11_read_data ( & temp, & hum) == RESET )
{
printf ( "DHT11 Error \r\n" ) ;
delay_ms ( 500 ) ;
}
enableInterrupts ( ) ;
while ( 1 )
{
i++ ;
if ( i % 20 == 0 )
{
LED1_TOGGLE;
LED2_TOGGLE;
}
if ( i % 100 == 0 )
{
if ( dht11_read_data ( & temp, & hum) )
{
printf ( "DHT11_Temp: %2d.%2d℃ DHT11_Hum:%2d.%2d%%\r\n" , ( u16 ) temp, Get_decimal ( temp, 2 ) , ( u16 ) hum, Get_decimal ( hum, 2 ) ) ;
}
else
{
printf ( "DHT11 Error \r\n" ) ;
}
}
delay_ms ( 10 ) ;
}
}
void assert_failed ( u8* file, u32 line )
{
while ( 1 )
{
}
}
📚程序源码
✨申明:本文章仅发表在CSDN网站,任何其他网站,未注明来源,见此内容均为盗链和爬取,请多多尊重和支持原创! 🍁对于文中所提供的相关资源链接将作不定期更换。
链接: https:
提取码: 9 h9x