STM32-DHT11-main.c

by Marwen Maghrebi
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"
  3.  
  4. /* Private includes ----------------------------------------------------------*/
  5. /* USER CODE BEGIN Includes */
  6. #include<string.h>
  7. #include<stdio.h>
  8.  
  9. /* Private define ------------------------------------------------------------*/
  10. /* USER CODE BEGIN PD */
  11. #define DHT11_Pin GPIO_PIN_0
  12. #define DHT11_GPIO_Port GPIOA
  13. /* USER CODE END PD */
  14.  
  15. /* Private variables ---------------------------------------------------------*/
  16. TIM_HandleTypeDef htim1;
  17.  
  18. UART_HandleTypeDef huart1;
  19.  
  20. /* USER CODE BEGIN PV */
  21. char msg[50] ;
  22. char message1[16];
  23. char message1[16];
  24. uint8_t TOUT =0 , CheckSum , i;
  25. uint8_t T_Byte1 , T_Byte2 , RH_Byte1 , RH_Byte2 ;
  26.  
  27. /* USER CODE END PV */
  28.  
  29.  
  30. /* Private function prototypes -----------------------------------------------*/
  31. void SystemClock_Config(void);
  32. static void MX_GPIO_Init(void);
  33. static void MX_TIM1_Init(void);
  34. static void MX_USART1_UART_Init(void);
  35. /* USER CODE BEGIN PFP */
  36.  
  37. /* USER CODE BEGIN 0 */
  38. void start_signal (void){
  39. GPIO_InitTypeDef GPIO_InitStruct = {0};
  40.  
  41.  
  42. GPIO_InitStruct.Pin = DHT11_Pin;
  43. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  44. GPIO_InitStruct.Pull = GPIO_NOPULL;
  45. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  46. HAL_GPIO_Init(DHT11_GPIO_Port, &GPIO_InitStruct);
  47.  
  48. HAL_GPIO_WritePin(DHT11_GPIO_Port, DHT11_Pin, GPIO_PIN_RESET);
  49. HAL_Delay(18);
  50. HAL_GPIO_WritePin(DHT11_GPIO_Port, DHT11_Pin, GPIO_PIN_SET);
  51. delay_us(30);
  52.  
  53. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  54. HAL_GPIO_Init(DHT11_GPIO_Port, &GPIO_InitStruct);
  55.  
  56. }
  57.  
  58. void delay_us ( uint16_t us)
  59. {
  60. __HAL_TIM_SET_COUNTER(&htim1,0);
  61. while(__HAL_TIM_GET_COUNTER(&htim1) < us);
  62. }
  63.  
  64. uint8_t check_response(void){
  65. TOUT=0;
  66. __HAL_TIM_SET_COUNTER(&htim1,0);
  67. while(!HAL_GPIO_ReadPin(DHT11_GPIO_Port, DHT11_Pin) && (__HAL_TIM_GET_COUNTER(&htim1) < 100)) {};
  68. if(__HAL_TIM_GET_COUNTER(&htim1)>= 100){
  69. return 0; //timeout
  70. }
  71. while(HAL_GPIO_ReadPin(DHT11_GPIO_Port, DHT11_Pin) && (__HAL_TIM_GET_COUNTER(&htim1) < 100)) {};
  72. if(__HAL_TIM_GET_COUNTER(&htim1)>= 100){
  73. return 0; //timeout
  74. }
  75. return 1;
  76. }
  77.  
  78. uint8_t read_byte(void){
  79. uint8_t num =0 ;
  80. for(i=0 ;i<8; i++){
  81. while(!HAL_GPIO_ReadPin(DHT11_GPIO_Port, DHT11_Pin)) {};
  82. __HAL_TIM_SET_COUNTER(&htim1,0);
  83. while(HAL_GPIO_ReadPin(DHT11_GPIO_Port, DHT11_Pin)) {};
  84. if(__HAL_TIM_GET_COUNTER(&htim1) > 40)
  85. {
  86. num |= (1 << (7 - i));
  87. }
  88.  
  89. }
  90. return num;
  91. }
  92.  
  93. void process_sensor_data(void){
  94. RH_Byte1 = read_byte();
  95. RH_Byte2 = read_byte();
  96. T_Byte1 = read_byte();
  97. T_Byte2 = read_byte();
  98. CheckSum = read_byte();
  99.  
  100. uint8_t humidity_integer = RH_Byte1 ;
  101. uint8_t humidity_decimal = RH_Byte2 / 10 ;
  102.  
  103. uint8_t temperature_integer = T_Byte1 ;
  104. uint8_t temperature_decimal = T_Byte2 / 10 ;
  105.  
  106. if(CheckSum ==((RH_Byte1+RH_Byte2 +T_Byte1+T_Byte2)& 0xff)){
  107. snprintf(msg,sizeof(msg),"RH = %d.%d %%\r\n",humidity_integer,humidity_decimal);
  108. send_uart_message(msg);
  109. snprintf(msg,sizeof(msg),"temp = %d.%d C\r\n",temperature_integer,temperature_decimal);
  110. send_uart_message(msg);
  111.  
  112. }else
  113. {
  114. send_uart_message("Checksum Errors ! Trying Again ...\r\n");
  115. }
  116. }
  117.  
  118. void send_uart_message(char *messsage){
  119. HAL_UART_Transmit(&huart1, (uint8_t*)messsage, strlen(messsage), HAL_MAX_DELAY);
  120. }
  121. /* USER CODE END 0 */
  122.  
  123. int main(void)
  124. {
  125.  
  126. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  127. HAL_Init();
  128. /* Configure the system clock */
  129. SystemClock_Config();
  130. /* Initialize all configured peripherals */
  131. MX_GPIO_Init();
  132. MX_TIM1_Init();
  133. MX_USART1_UART_Init();
  134. /* USER CODE BEGIN 2 */
  135. HAL_TIM_Base_Start(&htim1);
  136. send_uart_message("Initilization complet \n\r");
  137. /* USER CODE END 2 */
  138.  
  139. /* Infinite loop */
  140. /* USER CODE BEGIN WHILE */
  141. while (1)
  142. {
  143. /* USER CODE END WHILE */
  144.  
  145. /* USER CODE BEGIN 3 */
  146. HAL_Delay(1000); // 1 second delay before starting
  147. start_signal();
  148. uint8_t check = check_response();
  149. if (!check){
  150. send_uart_message("No responce from the sensor \r\n");
  151. } else {
  152. process_sensor_data();
  153. }
  154.  
  155. }
  156. /* USER CODE END 3 */
  157. }
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
-
00:00
00:00
    -
    00:00
    00:00