新手入门
- 积分
- 9
- 金钱
- 9
- 注册时间
- 2020-11-1
- 在线时间
- 3 小时
|
4金钱
uint8_t dat[5]={0x00,0x00,0x00,0x00,0xFF};
void Delay_us(uint16_t us)
{
uint16_t fac_us=0xffff-us;
__HAL_TIM_SET_COUNTER(&htim6,fac_us);
HAL_TIM_Base_Start(&htim6);
while(fac_us<0xffff)
{
fac_us=__HAL_TIM_GET_COUNTER(&htim6);
}
HAL_TIM_Base_Stop(&htim6);
}
void DHT11_PortOutput(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOG,GPIO_PIN_6,GPIO_PIN_SET);
}
void DHT11_PortInput(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
}
uint8_t DHT11_Read_Byte(void)
{
uint8_t ReadData=0;
int i;
int retry=0;
for(i=0;i<8;i++)
{
while(HAL_GPIO_ReadPin(GPIOG,GPIO_PIN_6)==GPIO_PIN_RESET&&retry<100)
{
Delay_us(1);
retry++;
}
Delay_us(30);
if(HAL_GPIO_ReadPin(GPIOG,GPIO_PIN_6)==GPIO_PIN_SET)
{
ReadData<<=1;
ReadData|=0x01;
printf("1\n");
}
else
{
ReadData<<=1;
ReadData|=0x00;
printf("0\n");
}
retry=0;
while(HAL_GPIO_ReadPin(GPIOG,GPIO_PIN_6)==GPIO_PIN_SET&&retry<100)
{
Delay_us(1);
retry++;
}
retry=0;
}
return ReadData;
}
uint8_t DHT11_Read(void)
{
uint8_t i;
int retry=0;
DHT11_PortOutput();
HAL_GPIO_WritePin(GPIOG,GPIO_PIN_6,GPIO_PIN_RESET);
Delay_us(20000);
HAL_GPIO_WritePin(GPIOG,GPIO_PIN_6,GPIO_PIN_SET);
Delay_us(36);
DHT11_PortInput();
while(HAL_GPIO_ReadPin(GPIOG,GPIO_PIN_6)==GPIO_PIN_RESET&&retry<100)
{
Delay_us(1);
retry++;
}
printf("retry=%d\n",retry);
retry=0;
while(HAL_GPIO_ReadPin(GPIOG,GPIO_PIN_6)==GPIO_PIN_SET&&retry<100)
{
Delay_us(1);
retry++;
}
printf("retry=%d\n",retry);
retry=0;
for(i=0;i<5;i++)
{
dat[i]=DHT11_Read_Byte();
}
if(dat[0]+dat[1]+dat[2]+dat[3]==dat[4])
{
return 1;
}
else
return 0;
}
上面是整个程序流程定时器6间隔1us。求教为何它的输出为什么一直都是高电平?从输出上可看到有短暂的低电平,之后就一直都是高电平
|
|