程序代码
mian.c
unsigned char buff[10]; unsigned char temp,humi;/* USER CODE END 0 *//** * [url=home.php?mod=space&uid=247401]@brief[/url] The application entry point. * @retval int */int main(void){ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash inteRFace and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock*/ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ LCD_INIT(); DHT11_Init(); while(1) { /**********功能1:显示温湿度数据******************/ DHT11_Read_Data(&temp,&humi);//读取DHT11的温湿度数据 sprintf((char *)buff,"%.2d %.2d",temp,humi); LCD_WRITE_StrDATA(buff,0,0); /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */}
DHT11.c
//从DHT11读取一次数据//temp:温度值(范围:0~50°)//humi:湿度值(范围:20%~90%)//返回值:0,正常;1,读取失败unsigned char DHT11_Read_Data(unsigned char *temp,unsigned char *humi) { unsigned char buf[5]; unsigned char i; DHT11_Rst(); if(DHT11_Check()==0) { //HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET); for(i=0;i<5;i++)//读取40位数据 { buf=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; }
|