你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32 读取DHT20,跑的官方的例程一直读取失败,初学者,各位大佬能指点一下不

[复制链接]
月亮不睡我不睡 提问时间:2024-11-11 20:35 / 未解决
#include "stm32f10x.h"
#include "DHT20.h"
#include "Delay.h"

void AHT20_Clock_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
}

void SDA_Pin_Output_High(void)   //½«PB15ÅäÖÃΪÊä³ö £¬ ²¢ÉèÖÃΪ¸ßµçƽ£¬ PB15×÷ΪI2CµÄSDA
{
GPIO_InitTypeDef  GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;//ÍÆÍìÊä³ö
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,& GPIO_InitStruct);
GPIO_SetBits(GPIOB,GPIO_Pin_15);
}

void SDA_Pin_Output_Low(void)  //½«P15ÅäÖÃΪÊä³ö  ²¢ÉèÖÃΪµÍµçƽ
{

GPIO_InitTypeDef  GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;//ÍÆÍìÊä³ö
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,& GPIO_InitStruct);
GPIO_ResetBits(GPIOB,GPIO_Pin_15);
}

void SDA_Pin_IN_FLOATING(void)  //SDAÅäÖÃΪ¸¡¿ÕÊäÈë
{

GPIO_InitTypeDef  GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;//
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOB,&GPIO_InitStruct);
}

void SCL_Pin_Output_High(void) //SCLÊä³ö¸ßµçƽ£¬P14×÷ΪI2CµÄSCL
{
GPIO_SetBits(GPIOB,GPIO_Pin_14);
}

void SCL_Pin_Output_Low(void) //SCLÊä³öµÍµçƽ
{
GPIO_ResetBits(GPIOB,GPIO_Pin_14);
}

void Init_I2C_Sensor_Port(void) //³õʼ»¯I2C½Ó¿Ú,Êä³öΪ¸ßµçƽ
{
GPIO_InitTypeDef  GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;//ÍÆÍìÊä³ö
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,& GPIO_InitStruct);
GPIO_SetBits(GPIOB,GPIO_Pin_15);//Êä³ö¸ßµçƽ

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;//ÍÆÍìÊä³ö
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,& GPIO_InitStruct);
GPIO_SetBits(GPIOB,GPIO_Pin_14);//Êä³ö¸ßµçƽ
}
void I2C_Start(void)         //I2CÖ÷»ú·¢ËÍSTARTÐźÅ
{
SDA_Pin_Output_High();
Delay_us(8);
SCL_Pin_Output_High();
Delay_us(8);
SDA_Pin_Output_Low();
Delay_us(8);
SCL_Pin_Output_Low();
Delay_us(8);
}

void AHT20_WR_Byte(uint8_t Byte) //ÍùAHT20дһ¸ö×Ö½Ú
{
uint8_t Data,N,i;
Data=Byte;
i = 0x80;
for(N=0;N<8;N++)
{
SCL_Pin_Output_Low();
Delay_us(4);
if(i&Data)
{
SDA_Pin_Output_High();
}
else
{
SDA_Pin_Output_Low();
}

SCL_Pin_Output_High();
Delay_us(4);
Data <<= 1;

}
SCL_Pin_Output_Low();
Delay_us(8);
SDA_Pin_IN_FLOATING();
Delay_us(8);
}

uint8_t AHT20_RD_Byte(void)//´ÓAHT20¶ÁÈ¡Ò»¸ö×Ö½Ú
{
uint8_t Byte,i,a;
Byte = 0;
SCL_Pin_Output_Low();
SDA_Pin_IN_FLOATING();
Delay_us(8);
for(i=0;i<8;i++)
{
SCL_Pin_Output_High();
Delay_us(5);
a=0;
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)) a=1;
Byte = (Byte<<1)|a;
SCL_Pin_Output_Low();
Delay_us(5);
}
SDA_Pin_IN_FLOATING();
Delay_us(8);
return Byte;
}

uint8_t Receive_ACK(void)   //¿´AHT20ÊÇ·ñÓлظ´ACK
{
uint16_t CNT;
CNT = 0;
SCL_Pin_Output_Low();
SDA_Pin_IN_FLOATING();
Delay_us(8);
SCL_Pin_Output_High();
Delay_us(8);
while((GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15))  && CNT < 100)
CNT++;
if(CNT == 100)
{
return 0;
}
SCL_Pin_Output_Low();
Delay_us(8);
return 1;
}

void Send_ACK(void)       //Ö÷»ú»Ø¸´ACKÐźÅ
{
SCL_Pin_Output_Low();
Delay_us(8);
SDA_Pin_Output_Low();
Delay_us(8);
SCL_Pin_Output_High();
Delay_us(8);
SCL_Pin_Output_Low();
Delay_us(8);
SDA_Pin_IN_FLOATING();
Delay_us(8);
}

void Send_NOT_ACK(void) //Ö÷»ú²»»Ø¸´ACK
{
SCL_Pin_Output_Low();
Delay_us(8);
SDA_Pin_Output_High();
Delay_us(8);
SCL_Pin_Output_High();
Delay_us(8);
SCL_Pin_Output_Low();
Delay_us(8);
SDA_Pin_Output_Low();
Delay_us(8);
}

void Stop_I2C(void)   //Ò»ÌõЭÒé½áÊø
{
SDA_Pin_Output_Low();
Delay_us(8);
SCL_Pin_Output_High();
Delay_us(8);
SDA_Pin_Output_High();
Delay_us(8);
}

uint8_t AHT20_Read_Status(void)//¶ÁÈ¡AHT20µÄ״̬¼Ä´æÆ÷
{

uint8_t Byte_first;
I2C_Start();
AHT20_WR_Byte(0x71);
Receive_ACK();
Byte_first = AHT20_RD_Byte();
Send_NOT_ACK();
Stop_I2C();
return Byte_first;
}

uint8_t AHT20_Read_Cal_Enable(void)  //²éѯcal enableλÓÐûÓÐʹÄÜ
{
uint8_t val = 0;//ret = 0,
val = AHT20_Read_Status();
if((val & 0x68)==0x08)
return 1;
else  return 0;
}

void AHT20_SendAC(void) //ÏòAHT20·¢ËÍACÃüÁî
{

I2C_Start();
AHT20_WR_Byte(0x70);
Receive_ACK();
AHT20_WR_Byte(0xac);//0xAC²É¼¯ÃüÁî
Receive_ACK();
AHT20_WR_Byte(0x33);
Receive_ACK();
AHT20_WR_Byte(0x00);
Receive_ACK();
Stop_I2C();
}

//CRCУÑéÀàÐÍ£ºCRC8/MAXIM
//¶àÏîʽ£ºX8+X5+X4+1
//Poly£º0011 0001  0x31
//¸ßλ·Åµ½ºóÃæ¾Í±ä³É 1000 1100 0x8c
//CÏÖʵ´úÂ룺
uint8_t Calc_CRC8(uint8_t *message,uint8_t Num)
{
uint8_t i;
uint8_t byte;
uint8_t crc=0xFF;
for(byte=0; byte<Num; byte++)
{
crc^=(message[byte]);
for(i=8;i>0;--i)
{
if(crc&0x80) crc=(crc<<1)^0x31;
else crc=(crc<<1);
}
}
return crc;
}

void AHT20_Read_CTdata(uint32_t *ct) //ûÓÐCRCУÑ飬ֱ½Ó¶ÁÈ¡AHT20µÄζȺÍʪ¶ÈÊý¾Ý
{
volatile uint8_t  Byte_1th=0;
volatile uint8_t  Byte_2th=0;
volatile uint8_t  Byte_3th=0;
volatile uint8_t  Byte_4th=0;
volatile uint8_t  Byte_5th=0;
volatile uint8_t  Byte_6th=0;
uint32_t RetuData = 0;
uint16_t cnt = 0;
AHT20_SendAC();//ÏòAHT10·¢ËÍACÃüÁî
Delay_ms(80);//ÑÓʱ80ms×óÓÒ
cnt = 0;
while(((AHT20_Read_Status()&0x80)==0x80))//Ö±µ½×´Ì¬bit[7]Ϊ0£¬±íʾΪ¿ÕÏÐ״̬£¬ÈôΪ1£¬±íʾæ״̬
{
Delay_us(1508);
if(cnt++>=100)
{
break;
}
}
I2C_Start();
AHT20_WR_Byte(0x71);
Receive_ACK();
Byte_1th = AHT20_RD_Byte();//״̬×Ö£¬²éѯµ½×´Ì¬Îª0x98,±íʾΪæ״̬£¬bit[7]Ϊ1£»×´Ì¬Îª0x1C£¬»òÕß0x0C£¬»òÕß0x08±íʾΪ¿ÕÏÐ״̬£¬bit[7]Ϊ0
Send_ACK();
Byte_2th = AHT20_RD_Byte();//ʪ¶È
Send_ACK();
Byte_3th = AHT20_RD_Byte();//ʪ¶È
Send_ACK();
Byte_4th = AHT20_RD_Byte();//ʪ¶È/ζÈ
Send_ACK();
Byte_5th = AHT20_RD_Byte();//ζÈ
Send_ACK();
Byte_6th = AHT20_RD_Byte();//ζÈ
Send_NOT_ACK();
Stop_I2C();

RetuData = (RetuData|Byte_2th)<<8;
RetuData = (RetuData|Byte_3th)<<8;
RetuData = (RetuData|Byte_4th);
RetuData =RetuData >>4;
ct[0] = RetuData;//ʪ¶È
RetuData = 0;
RetuData = (RetuData|Byte_4th)<<8;
RetuData = (RetuData|Byte_5th)<<8;
RetuData = (RetuData|Byte_6th);
RetuData = RetuData&0xfffff;
ct[1] =RetuData; //ζÈ
}

void AHT20_Read_CTdata_crc(uint32_t *ct) //CRCУÑéºó£¬¶ÁÈ¡AHT20µÄζȺÍʪ¶ÈÊý¾Ý
{
volatile uint8_t  Byte_1th=0;
volatile uint8_t  Byte_2th=0;
volatile uint8_t  Byte_3th=0;
volatile uint8_t  Byte_4th=0;
volatile uint8_t  Byte_5th=0;
volatile uint8_t  Byte_6th=0;
volatile uint8_t  Byte_7th=0;
uint32_t RetuData = 0;
uint16_t cnt = 0;
// uint8_t  CRCDATA=0;
uint8_t  CTDATA[6]={0};//ÓÃÓÚCRC´«µÝÊý×é

AHT20_SendAC();//ÏòAHT10·¢ËÍACÃüÁî
Delay_ms(80);//ÑÓʱ80ms×óÓÒ
cnt = 0;
while(((AHT20_Read_Status()&0x80)==0x80))//Ö±µ½×´Ì¬bit[7]Ϊ0£¬±íʾΪ¿ÕÏÐ״̬£¬ÈôΪ1£¬±íʾæ״̬
{
Delay_us(1508);
if(cnt++>=100)
{
break;
}
}

I2C_Start();

AHT20_WR_Byte(0x71);
Receive_ACK();
CTDATA[0]=Byte_1th = AHT20_RD_Byte();//״̬×Ö£¬²éѯµ½×´Ì¬Îª0x98,±íʾΪæ״̬£¬bit[7]Ϊ1£»×´Ì¬Îª0x1C£¬»òÕß0x0C£¬»òÕß0x08±íʾΪ¿ÕÏÐ״̬£¬bit[7]Ϊ0
Send_ACK();
CTDATA[1]=Byte_2th = AHT20_RD_Byte();//ʪ¶È
Send_ACK();
CTDATA[2]=Byte_3th = AHT20_RD_Byte();//ʪ¶È
Send_ACK();
CTDATA[3]=Byte_4th = AHT20_RD_Byte();//ʪ¶È/ζÈ
Send_ACK();
CTDATA[4]=Byte_5th = AHT20_RD_Byte();//ζÈ
Send_ACK();
CTDATA[5]=Byte_6th = AHT20_RD_Byte();//ζÈ
Send_ACK();
Byte_7th = AHT20_RD_Byte();//CRCÊý¾Ý
Send_NOT_ACK();                           //×¢Òâ: ×îºóÊÇ·¢ËÍNAK
Stop_I2C();

if(Calc_CRC8(CTDATA,6)==Byte_7th)
{
RetuData = (RetuData|Byte_2th)<<8;
RetuData = (RetuData|Byte_3th)<<8;
RetuData = (RetuData|Byte_4th);
RetuData =RetuData >>4;
ct[0] = RetuData;//ʪ¶È
RetuData = 0;
RetuData = (RetuData|Byte_4th)<<8;
RetuData = (RetuData|Byte_5th)<<8;
RetuData = (RetuData|Byte_6th);
RetuData = RetuData&0xfffff;
ct[1] =RetuData; //ζÈ

}
else
{
ct[0]=0x00;
ct[1]=0x00;//УÑé´íÎó·µ»ØÖµ£¬¿Í»§¿ÉÒÔ¸ù¾Ý×Ô¼ºÐèÒª¸ü¸Ä
}//CRCÊý¾Ý
}

void AHT20_Init(void)   //³õʼ»¯AHT20
{
Init_I2C_Sensor_Port();
I2C_Start();
AHT20_WR_Byte(0x70);
Receive_ACK();
AHT20_WR_Byte(0xa8);//0xA8½øÈëNOR¹¤×÷ģʽ
Receive_ACK();
AHT20_WR_Byte(0x00);
Receive_ACK();
AHT20_WR_Byte(0x00);
Receive_ACK();
Stop_I2C();

Delay_ms(10);//ÑÓʱ10ms×óÓÒ

I2C_Start();
AHT20_WR_Byte(0x70);
Receive_ACK();
AHT20_WR_Byte(0xbe);//0xBE³õʼ»¯ÃüÁAHT20µÄ³õʼ»¯ÃüÁîÊÇ0xBE,   AHT10µÄ³õʼ»¯ÃüÁîÊÇ0xE1
Receive_ACK();
AHT20_WR_Byte(0x08);//Ïà¹Ø¼Ä´æÆ÷bit[3]ÖÃ1£¬ÎªÐ£×¼Êä³ö
Receive_ACK();
AHT20_WR_Byte(0x00);
Receive_ACK();
Stop_I2C();
Delay_ms(10);//ÑÓʱ10ms×óÓÒ
}
void JH_Reset_REG(uint8_t addr)
{

uint8_t Byte_first,Byte_second,Byte_third,Byte_fourth;
I2C_Start();
AHT20_WR_Byte(0x70);//Ô­À´ÊÇ0x70
Receive_ACK();
AHT20_WR_Byte(addr);
Receive_ACK();
AHT20_WR_Byte(0x00);
Receive_ACK();
AHT20_WR_Byte(0x00);
Receive_ACK();
Stop_I2C();

Delay_ms(5);//ÑÓʱ5ms×óÓÒ
I2C_Start();
AHT20_WR_Byte(0x71);//
Receive_ACK();
Byte_first = AHT20_RD_Byte();
Send_ACK();
Byte_second = AHT20_RD_Byte();
Send_ACK();
Byte_third = AHT20_RD_Byte();
Send_NOT_ACK();
Stop_I2C();

Delay_ms(10);//ÑÓʱ10ms×óÓÒ
I2C_Start();
AHT20_WR_Byte(0x70);///
Receive_ACK();
AHT20_WR_Byte(0xB0|addr);////¼Ä´æÆ÷ÃüÁî
Receive_ACK();
AHT20_WR_Byte(Byte_second);
Receive_ACK();
AHT20_WR_Byte(Byte_third);
Receive_ACK();
Stop_I2C();

Byte_second=0x00;
Byte_third =0x00;
}

void AHT20_Start_Init(void)
{
JH_Reset_REG(0x1b);
JH_Reset_REG(0x1c);
JH_Reset_REG(0x1e);
}

//int32_t main(void)
//{
//    uint32_t CT_data[2];
//  volatile int  c1,t1;
//  /***********************************************************************************/
//  /**///¢Ù¸ÕÉϵ磬²úƷоƬÄÚ²¿¾ÍÐ÷ÐèҪʱ¼ä£¬ÑÓʱ100~500ms,½¨Òé500ms
//  /***********************************************************************************/
//  Delay_1ms(500);
//  /***********************************************************************************/
//  /**///¢ÚÉϵçµÚÒ»´Î·¢0x71¶Áȡ״̬×Ö£¬ÅжÏ״̬×ÖÊÇ·ñΪ0x18,Èç¹û²»ÊÇ0x18,½øÐмĴæÆ÷³õʼ»¯
//  /***********************************************************************************/
//  if((AHT20_Read_Status()&0x18)!=0x18)
//  {
//  AHT20_Start_Init(); //ÖØгõʼ»¯¼Ä´æÆ÷
//  Delay_1ms(10);
//  }
//
//  /***********************************************************************************/
//  /**///¢Û¸ù¾Ý¿Í»§×Ô¼ºÐèÇó·¢²âÁ¿ÃüÁî¶ÁÈ¡ÎÂʪ¶ÈÊý¾Ý£¬µ±Ç°while£¨1£©Ñ­»··¢²âÁ¿ÃüÁî¶ÁÈ¡ÎÂʪ¶ÈÊý¾Ý£¬½ö¹©²Î¿¼
//  /***********************************************************************************/
//  while(1)
//  {
//   AHT20_Read_CTdata(CT_data);       //²»¾­¹ýCRCУÑ飬ֱ½Ó¶ÁÈ¡AHT20µÄζȺÍʪ¶ÈÊý¾Ý    ÍƼöÿ¸ô´óÓÚ1S¶ÁÒ»´Î
//    //AHT20_Read_CTdata_crc(CT_data);  //crcУÑéºó£¬¶ÁÈ¡AHT20µÄζȺÍʪ¶ÈÊý¾Ý
//

//   c1 = CT_data[0]*100*10/1024/1024;  //¼ÆËãµÃµ½Êª¶ÈÖµc1£¨·Å´óÁË10±¶£©
//   t1 = CT_data[1]*200*10/1024/1024-500;//¼ÆËãµÃµ½Î¶ÈÖµt1£¨·Å´óÁË10±¶£©
//  ////ÏÂÒ»²½¿Í»§´¦ÀíÏÔʾÊý¾Ý£¬
//   }
// }

main.c

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "Serial.h"
#include "DHT20.h"

int main(void)
{
Serial_Init();

uint32_t CT_data[2];
volatile int  c1,t1;
float c,t;
/***********************************************************************************/
/**///¢Ù¸ÕÉϵ磬²úƷоƬÄÚ²¿¾ÍÐ÷ÐèҪʱ¼ä£¬ÑÓʱ100~500ms,½¨Òé500ms
/***********************************************************************************/
Delay_ms(500);
/***********************************************************************************/
/**///¢ÚÉϵçµÚÒ»´Î·¢0x71¶Áȡ״̬×Ö£¬ÅжÏ״̬×ÖÊÇ·ñΪ0x18,Èç¹û²»ÊÇ0x18,½øÐмĴæÆ÷³õʼ»¯
/***********************************************************************************/
if((AHT20_Read_Status()&0x18)!=0x18)
{
AHT20_Start_Init(); //ÖØгõʼ»¯¼Ä´æÆ÷
Delay_ms(10);
}

/***********************************************************************************/
/**///¢Û¸ù¾Ý¿Í»§×Ô¼ºÐèÇó·¢²âÁ¿ÃüÁî¶ÁÈ¡ÎÂʪ¶ÈÊý¾Ý£¬µ±Ç°while£¨1£©Ñ­»··¢²âÁ¿ÃüÁî¶ÁÈ¡ÎÂʪ¶ÈÊý¾Ý£¬½ö¹©²Î¿¼
/***********************************************************************************/
while(1)
{
AHT20_Read_CTdata(CT_data);       //²»¾­¹ýCRCУÑ飬ֱ½Ó¶ÁÈ¡AHT20µÄζȺÍʪ¶ÈÊý¾Ý    ÍƼöÿ¸ô´óÓÚ1S¶ÁÒ»´Î
//AHT20_Read_CTdata_crc(CT_data);  //crcУÑéºó£¬¶ÁÈ¡AHT20µÄζȺÍʪ¶ÈÊý¾Ý

c1 = CT_data[0]*100*10/1024/1024;  //¼ÆËãµÃµ½Êª¶ÈÖµc1£¨·Å´óÁË10±¶£©
t1 = CT_data[1]*200*10/1024/1024-500;//¼ÆËãµÃµ½Î¶ÈÖµt1£¨·Å´óÁË10±¶£©
//ÏÂÒ»²½¿Í»§´¦ÀíÏÔʾÊý¾Ý£¬
c=c1/10.0;
t=t1/10.0;
/**///ÑÓʱ2s£¬½¨Òé¼ä¸ô2s¶ÁÒ»´Î
/***********************************************************************************/
Delay_ms(4000);
printf("ζȣº%.3f\n",t);
printf("滦飼%.3f\n\r",c);
}
}

结果:

温度:-50.000
湿度:0.000
温度:-50.000
湿度:0.000
温度:-50.000
湿度:0.000
温度:-50.000
湿度:0.000
收藏 评论2 发布时间:2024-11-11 20:35

举报

2个回答
STMWoodData 回答时间:4 天前

你这上面也太乱了

月亮不睡我不睡 回答时间:4 天前

STMWoodData 发表于 2024-11-12 09:43
你这上面也太乱了

改好了改好了

关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版