开发软件:PSOC 5LP
单片机:CY8CKIT-050
外加模块:DHT22
用PSOC写温湿度测量的时候,DHT22可以正常接收主机发送的开始信号,并且发送数据(已经在示波器上捕捉到了正确的数据),但是主机没有办法接收到DHT22的数据,读取数据的引脚始终为高电平,是设定GPIO的时候出现了问题吗?还是其他说明原因,有什么办法解决吗?/(ㄒoㄒ)/~~
附代码:(接收pin名称叫Data,设定为同时勾选input和output)
#include<project.h>
#include<stdio.h>
int main()
{
LCD_Start();
LCD_Posi
tion(0,0);
LCD_PrintString("Ready") ;
uint8 t;
uint8 num[5] = {0};
uint8 sum = 0;
uint16 temp = 100;
uint16 wet = 200;
CyDelay(2000);
LCD_Position(0,0);
LCD_PrintString(" ");
LCD_Position(0,0);
LCD_PrintString("Temp");
LCD_Position(0,7);
LCD_PrintString("Wet");
//Data_SetDriveMode(PIN_DM_STRONG);
Data_Write(1);
CyDelayUs(1000);
Data_Write(0);
CyDelayUs(300);
//这里为止都是正常的,示波器可以捕捉到DHT22传输的数据
Data_SetDriveMode(PIN_DM_DIG_HIZ);
CyDelayUs(20);
t = Data_ReadDataReg();//从这里开始不能正常读数了,始终读到低电平,如果用Data_Read()则始终读到高电平
//while (t) t = Data_Read();
if (t == 0)
{
while (!t) t = Data_ReadDataReg();
while (t) t = Data_ReadDataReg();
int i,j,temp;
for (i = 0; i<5; i++)
{
for (j = 0; j<8; j++)
{
while (!t) t = Data_ReadDataReg();
CyDelayUs(30);
t = Data_ReadDataReg();
temp = t;
num[i]<<=1;
num[i] = num[i] | temp;
}
if (i<4) sum+=num[i];
}
Data_SetDriveMode(PIN_DM_STRONG);
Data_Write(0);
CyDelayUs(50);
Data_Write(1);
if (sum == num[4])
{
wet=num[0];
wet<<=4;
wet+=num[1];
temp=num[2];
temp<<=4;
temp+=num[3];
LCD_Position(1,0);
LCD_PrintString(" ");
LCD_Position(1,0);
LCD_PrintNumber(temp);
LCD_PutChar('C');
LCD_Position(1,7);
LCD_PrintString(" ");
LCD_Position(1,7);
LCD_PrintNumber(wet);
LCD_PutChar('%');
}
else
{
LCD_Position(1,0);
LCD_PrintString(" ");
LCD_Position(1,0);
LCD_PrintString("ERROR");
}
}
else
{
LCD_Position(1,0);
LCD_PrintString(" ");
LCD_Position(1,0);
LCD_PrintString("Fail");
}
}
一周热门 更多>