#include "dht11.h" void Delay_us(uint16_t delay) { __HAL_TIM_DISABLE(&htim3); __HAL_TIM_SET_COUNTER(&htim3,0); __HAL_TIM_ENABLE(&htim3); uint16_t curCnt=0; while(1) { curCnt=__HAL_TIM_GET_COUNTER(&htim3); if(curCnt>=delay) break; } __HAL_TIM_DISABLE(&htim3); } void DHT11_OUT(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitStruct.Pin = GPIO_PIN_8; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); } void DHT11_IN(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitStruct.Pin = GPIO_PIN_8; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); } void DHT11_Strat(void) { DHT11_OUT(); HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_RESET); HAL_Delay(20); HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_SET); Delay_us(30); } uint8_t DHT11_Check(void) { uint8_t retry = 0 ; DHT11_IN(); while(GPIO_PIN_SET == HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_8) && retry <100) { retry++; Delay_us(1);//1us } if(retry>=100) {return 1;} else retry = 0 ; while(GPIO_PIN_RESET == HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_8) && retry<100) { retry++; Delay_us(1);//1us } if(retry>=100) {return 1;} return 0 ; } uint8_t DHT11_Read_Bit(void) { uint8_t retry = 0 ; while(GPIO_PIN_SET==HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8) && retry <100) { retry++; Delay_us(1); } retry = 0 ; while(GPIO_PIN_RESET==HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8) && retry<100) { retry++; Delay_us(1); } Delay_us(40); if(GPIO_PIN_SET==HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8)) return 1; else return 0 ; } uint8_t DHT11_Read_Byte(void) { uint8_t i , dat ; dat = 0 ; for(i=0; i<8; i++) { dat <<= 1; dat |= DHT11_Read_Bit(); } return dat ; } uint8_t DHT11_Read_Data(uint8_t* temp , uint8_t* humi) { uint8_t buf[5]; uint8_t i; DHT11_Strat(); if(DHT11_Check() == 0) { for(i=0; i<5; i++) { buf[i] = DHT11_Read_Byte(); } if(buf[0]+buf[1]+buf[2]+buf[3] == buf[4]) { *humi = buf[0]; *temp = buf[2]; } }else return 1; return 0 ; } void func_1() { uint8_t temperature = 1 ; uint8_t humidity = 1; uint8_t aTXbuf[32] ; while(1){ DHT11_Read_Data(&temperature , &humidity); sprintf((char*)aTXbuf,"%d , %d %% \r\n" ,temperature ,humidity); HAL_UART_Transmit(&huart1, aTXbuf, strlen((const char*)aTXbuf), 200); HAL_Delay(5000); } } int temperature_humidity_device_control(protocol_package_t *pk) { printf("temperature_humidity_device_control\r\n"); if(pk->function == 0x16 && pk->data[0] == 0x00) { printf("temperature_humidity_device_control success\r\n"); uint8_t temperature = 1 ; uint8_t humidity = 1; uint8_t aTXbuf[32] ; //DHT11_Read_Data(&temperature , &humidity); pk->data[0] = 0x35; } return 0; }改错
时间: 2023-09-02 14:06:24 浏览: 129
在你提供的代码中,没有包含头文件 "main.h" 和 "stdio.h"。你需要添加这两个头文件到你的代码中,这样才能正确使用其中的函数和变量。
另外,你在调用 sprintf 函数时,第二个参数类型应该是 char*,而不是 uint8_t*。因此,你需要将 aTXbuf 声明为 char 类型数组,而不是 uint8_t 类型数组。同时,在使用 strlen 函数计算字符串长度时,应该传入一个 char* 类型的参数。
最后,你可以将 DHT11_Read_Data 函数的返回值直接返回,而不需要使用 if-else 语句判断后再返回。因为函数内部已经有了返回值,可以直接将其返回给调用者。
以下是修改后的代码:
```c
#include "main.h"
#include "stdio.h"
#include "dht11.h"
extern UART_HandleTypeDef huart1;
extern TIM_HandleTypeDef htim3;
void Delay_us(uint16_t delay) {
__HAL_TIM_DISABLE(&htim3);
__HAL_TIM_SET_COUNTER(&htim3,0);
__HAL_TIM_ENABLE(&htim3);
uint16_t curCnt=0;
while(1)
{
curCnt=__HAL_TIM_GET_COUNTER(&htim3);
if(curCnt>=delay)
break;
}
__HAL_TIM_DISABLE(&htim3);
}
void DHT11_OUT(void) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void DHT11_IN(void) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void DHT11_Strat(void) {
DHT11_OUT();
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_RESET);
HAL_Delay(20);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_SET);
Delay_us(30);
}
uint8_t DHT11_Check(void) {
uint8_t retry = 0 ;
DHT11_IN();
while(GPIO_PIN_SET == HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_8) && retry <100)
{
retry++;
Delay_us(1);//1us
}
if(retry>=100)
{return 1;}
else retry = 0 ;
while(GPIO_PIN_RESET == HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_8) && retry<100)
{
retry++;
Delay_us(1);//1us
}
if(retry>=100)
{return 1;}
return 0 ;
}
uint8_t DHT11_Read_Bit(void) {
uint8_t retry = 0 ;
while(GPIO_PIN_SET==HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8) && retry <100)
{
retry++;
Delay_us(1);
}
retry = 0 ;
while(GPIO_PIN_RESET==HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8) && retry<100)
{
retry++;
Delay_us(1);
}
Delay_us(40);
if(GPIO_PIN_SET==HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8)) return 1;
else return 0 ;
}
uint8_t DHT11_Read_Byte(void) {
uint8_t i , dat ;
dat = 0 ;
for(i=0; i<8; i++)
{
dat <<= 1;
dat |= DHT11_Read_Bit();
}
return dat ;
}
uint8_t DHT11_Read_Data(uint8_t* temp , uint8_t* humi) {
uint8_t buf[5];
uint8_t i;
DHT11_Strat();
if(DHT11_Check() == 0)
{
for(i=0; i<5; i++)
{
buf[i] = DHT11_Read_Byte();
}
if(buf[0]+buf[1]+buf[2]+buf[3] == buf[4])
{
*humi = buf[0];
*temp = buf[2];
return 0;
}
}
return 1;
}
void func_1() {
uint8_t temperature = 1;
uint8_t humidity = 1;
char aTXbuf[32];
while(1){
DHT11_Read_Data(&temperature , &humidity);
sprintf(aTXbuf,"%d , %d %% \r\n" ,temperature ,humidity);
HAL_UART_Transmit(&huart1, (uint8_t*)aTXbuf, strlen(aTXbuf), 200);
HAL_Delay(5000);
}
}
int temperature_humidity_device_control(protocol_package_t *pk) {
printf("temperature_humidity_device_control\r\n");
if(pk->function == 0x16 && pk->data[0] == 0x00)
{
printf("temperature_humidity_device_control success\r\n");
uint8_t temperature = 1 ;
uint8_t humidity = 1;
char aTXbuf[32];
DHT11_Read_Data(&temperature , &humidity);
sprintf(aTXbuf,"%d , %d %% \r\n" ,temperature ,humidity);
strncpy((char*)pk->data, aTXbuf, strlen(aTXbuf));
pk->data_len = strlen(aTXbuf);
pk->data[0] = 0x35;
}
return 0;
}
```
阅读全文