1.1K
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include<string.h>
- #include<stdio.h>
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- #define DHT11_Pin GPIO_PIN_0
- #define DHT11_GPIO_Port GPIOA
- /* USER CODE END PD */
- /* Private variables ---------------------------------------------------------*/
- TIM_HandleTypeDef htim1;
- UART_HandleTypeDef huart1;
- /* USER CODE BEGIN PV */
- char msg[50] ;
- char message1[16];
- char message1[16];
- uint8_t TOUT =0 , CheckSum , i;
- uint8_t T_Byte1 , T_Byte2 , RH_Byte1 , RH_Byte2 ;
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_TIM1_Init(void);
- static void MX_USART1_UART_Init(void);
- /* USER CODE BEGIN PFP */
- /* USER CODE BEGIN 0 */
- void start_signal (void){
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- GPIO_InitStruct.Pin = DHT11_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(DHT11_GPIO_Port, &GPIO_InitStruct);
- HAL_GPIO_WritePin(DHT11_GPIO_Port, DHT11_Pin, GPIO_PIN_RESET);
- HAL_Delay(18);
- HAL_GPIO_WritePin(DHT11_GPIO_Port, DHT11_Pin, GPIO_PIN_SET);
- delay_us(30);
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- HAL_GPIO_Init(DHT11_GPIO_Port, &GPIO_InitStruct);
- }
- void delay_us ( uint16_t us)
- {
- __HAL_TIM_SET_COUNTER(&htim1,0);
- while(__HAL_TIM_GET_COUNTER(&htim1) < us);
- }
- uint8_t check_response(void){
- TOUT=0;
- __HAL_TIM_SET_COUNTER(&htim1,0);
- while(!HAL_GPIO_ReadPin(DHT11_GPIO_Port, DHT11_Pin) && (__HAL_TIM_GET_COUNTER(&htim1) < 100)) {};
- if(__HAL_TIM_GET_COUNTER(&htim1)>= 100){
- return 0; //timeout
- }
- while(HAL_GPIO_ReadPin(DHT11_GPIO_Port, DHT11_Pin) && (__HAL_TIM_GET_COUNTER(&htim1) < 100)) {};
- if(__HAL_TIM_GET_COUNTER(&htim1)>= 100){
- return 0; //timeout
- }
- return 1;
- }
- uint8_t read_byte(void){
- uint8_t num =0 ;
- for(i=0 ;i<8; i++){
- while(!HAL_GPIO_ReadPin(DHT11_GPIO_Port, DHT11_Pin)) {};
- __HAL_TIM_SET_COUNTER(&htim1,0);
- while(HAL_GPIO_ReadPin(DHT11_GPIO_Port, DHT11_Pin)) {};
- if(__HAL_TIM_GET_COUNTER(&htim1) > 40)
- {
- num |= (1 << (7 - i));
- }
- }
- return num;
- }
- void process_sensor_data(void){
- RH_Byte1 = read_byte();
- RH_Byte2 = read_byte();
- T_Byte1 = read_byte();
- T_Byte2 = read_byte();
- CheckSum = read_byte();
- uint8_t humidity_integer = RH_Byte1 ;
- uint8_t humidity_decimal = RH_Byte2 / 10 ;
- uint8_t temperature_integer = T_Byte1 ;
- uint8_t temperature_decimal = T_Byte2 / 10 ;
- if(CheckSum ==((RH_Byte1+RH_Byte2 +T_Byte1+T_Byte2)& 0xff)){
- snprintf(msg,sizeof(msg),"RH = %d.%d %%\r\n",humidity_integer,humidity_decimal);
- send_uart_message(msg);
- snprintf(msg,sizeof(msg),"temp = %d.%d C\r\n",temperature_integer,temperature_decimal);
- send_uart_message(msg);
- }else
- {
- send_uart_message("Checksum Errors ! Trying Again ...\r\n");
- }
- }
- void send_uart_message(char *messsage){
- HAL_UART_Transmit(&huart1, (uint8_t*)messsage, strlen(messsage), HAL_MAX_DELAY);
- }
- /* USER CODE END 0 */
- int main(void)
- {
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* Configure the system clock */
- SystemClock_Config();
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_TIM1_Init();
- MX_USART1_UART_Init();
- /* USER CODE BEGIN 2 */
- HAL_TIM_Base_Start(&htim1);
- send_uart_message("Initilization complet \n\r");
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- HAL_Delay(1000); // 1 second delay before starting
- start_signal();
- uint8_t check = check_response();
- if (!check){
- send_uart_message("No responce from the sensor \r\n");
- } else {
- process_sensor_data();
- }
- }
- /* USER CODE END 3 */
- }