0% found this document useful (0 votes)
72 views16 pages

USER CODE BEGIN Header

The document is source code for a microcontroller program that reads temperature and humidity from a DHT11 sensor and displays the values on an LCD screen. It initializes GPIO pins and timers for sensor communication and LCD control. Functions are defined to read values from the sensor and display output.

Uploaded by

Eya Toumi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views16 pages

USER CODE BEGIN Header

The document is source code for a microcontroller program that reads temperature and humidity from a DHT11 sensor and displays the values on an LCD screen. It initializes GPIO pins and timers for sensor communication and LCD control. Functions are defined to read values from the sensor and display output.

Uploaded by

Eya Toumi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

/* USER CODE BEGIN Header */

/**

******************************************************************************
* @file : main.c
* @brief : Main program body

******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE
file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*

******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------
*/
#include "main.h"

/* Private includes ----------------------------------------------------------


*/
/* USER CODE BEGIN Includes */
#include "lcd.h"
#include "stdio.h"
#include "string.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------


*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------


*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------


*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------


*/
TIM_HandleTypeDef htim1;

/* USER CODE BEGIN PV */


/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------


*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM1_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------


*/
/* USER CODE BEGIN 0 */
#define DHT11_PORT GPIOC
#define DHT11_PIN GPIO_PIN_8
uint8_t RHI, RHD, TCI, TCD, SUM;
uint32_t pMillis, cMillis;
float tCelsius = 0;
float tFahrenheit = 0;
float RH = 0;
Lcd_HandleTypeDef lcd;
void microDelay (uint16_t delay)
{
__HAL_TIM_SET_COUNTER(&htim1, 0);
while (__HAL_TIM_GET_COUNTER(&htim1) < delay);
}

uint8_t DHT11_Start (void)


{
uint8_t Response = 0;
GPIO_InitTypeDef GPIO_InitStructPrivate = {0};
GPIO_InitStructPrivate.Pin = DHT11_PIN;
GPIO_InitStructPrivate.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructPrivate.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStructPrivate.Pull = GPIO_NOPULL;
HAL_GPIO_Init(DHT11_PORT, &GPIO_InitStructPrivate); // set the pin as output
HAL_GPIO_WritePin (DHT11_PORT, DHT11_PIN, 0); // pull the pin low
HAL_Delay(20); // wait for 20ms
HAL_GPIO_WritePin (DHT11_PORT, DHT11_PIN, 1); // pull the pin high
microDelay (30); // wait for 30us
GPIO_InitStructPrivate.Mode = GPIO_MODE_INPUT;
GPIO_InitStructPrivate.Pull = GPIO_PULLUP;
HAL_GPIO_Init(DHT11_PORT, &GPIO_InitStructPrivate); // set the pin as input
microDelay (40);
if (!(HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN)))
{
microDelay (80);
if ((HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN))) Response = 1;
}
pMillis = HAL_GetTick();
cMillis = HAL_GetTick();
while ((HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN)) && pMillis + 2 > cMillis)
{
cMillis = HAL_GetTick();
}
return Response;
}

uint8_t DHT11_Read (void)


{
uint8_t a,b;
for (a=0;a<8;a++)
{
pMillis = HAL_GetTick();
cMillis = HAL_GetTick();
while (!(HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN)) && pMillis + 2 >
cMillis)
{ // wait for the pin to go high
cMillis = HAL_GetTick();
}
microDelay (40); // wait for 40 us
if (!(HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN))) // if the pin is low
b&= ~(1<<(7-a));
else
b|= (1<<(7-a));
pMillis = HAL_GetTick();
cMillis = HAL_GetTick();
while ((HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN)) && pMillis + 2 >
cMillis)
{ // wait for the pin to go low
cMillis = HAL_GetTick();
}
}
return b;
}
/* USER CODE END 0 */

/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/* 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();
MX_TIM1_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start(&htim1);
Lcd_PortType ports[]={D4_GPIO_Port,D5_GPIO_Port,D6_GPIO_Port,D7_GPIO_Port};
Lcd_PinType pins[]={D4_Pin,D5_Pin,D6_Pin,D7_Pin};
lcd=Lcd_create(ports, pins, RS_GPIO_Port, RS_Pin, E_GPIO_Port, E_Pin,
LCD_4_BIT_MODE);
//Lcd_cursor(&lcd,0,0);
//float x=12;
//Lcd_float(&lcd,x);
/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if(DHT11_Start())
{
RHI = DHT11_Read(); // Relative humidity integral
RHD = DHT11_Read(); // Relative humidity decimal
TCI = DHT11_Read(); // Celsius integral
TCD = DHT11_Read(); // Celsius decimal
SUM = DHT11_Read(); // Check sum
if (RHI + RHD + TCI + TCD == SUM)
{
// Can use RHI and TCI for any purposes if whole number only
needed
tCelsius = (float)TCI + (float)(TCD/10.0);
printf(RHI);
printf(TCD);
tFahrenheit = tCelsius * 9/5 + 32;
RH = (float)RHI + (float)(RHD/10.0);
Lcd_cursor(&lcd,0,0);
Lcd_string(&lcd,"tCelsius=");
Lcd_cursor(&lcd,0,11);
Lcd_float(&lcd,tCelsius);
Lcd_cursor(&lcd,1,0);
Lcd_string(&lcd,"RH=");
Lcd_cursor(&lcd,1,5);
Lcd_float(&lcd,RH);
// Can use tCelsius, tFahrenheit and RH for any purposes
}
HAL_Delay(2000);
}
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */


}
/* USER CODE END 3 */
}

/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

/** Configure the main internal regulator output voltage


*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 72;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)


{
Error_Handler();
}
}

/**
* @brief TIM1 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM1_Init(void)
{

/* USER CODE BEGIN TIM1_Init 0 */

/* USER CODE END TIM1_Init 0 */

TIM_ClockConfigTypeDef sClockSourceConfig = {0};


TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM1_Init 1 */

/* USER CODE END TIM1_Init 1 */


htim1.Instance = TIM1;
htim1.Init.Prescaler = 71;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 65535;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM1_Init 2 */

/* USER CODE END TIM1_Init 2 */

/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};

/* GPIO Ports Clock Enable */


__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();

/*Configure GPIO pin Output Level */


HAL_GPIO_WritePin(GPIOC, E_Pin|RS_Pin, GPIO_PIN_RESET);

/*Configure GPIO pin Output Level */


HAL_GPIO_WritePin(GPIOA, D4_Pin|D5_Pin|D6_Pin, GPIO_PIN_RESET);

/*Configure GPIO pin Output Level */


HAL_GPIO_WritePin(D7_GPIO_Port, D7_Pin, GPIO_PIN_RESET);

/*Configure GPIO pins : E_Pin RS_Pin */


GPIO_InitStruct.Pin = E_Pin|RS_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

/*Configure GPIO pins : D4_Pin D5_Pin D6_Pin */


GPIO_InitStruct.Pin = D4_Pin|D5_Pin|D6_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/*Configure GPIO pin : D7_Pin */


GPIO_InitStruct.Pin = D7_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(D7_GPIO_Port, &GPIO_InitStruct);

/*Configure GPIO pin : DHT11_Pin */


GPIO_InitStruct.Pin = DHT11_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(DHT11_GPIO_Port, &GPIO_InitStruct);

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state
*/
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}

#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line
number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line)
*/
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
This is an embedded C program for a STM32 microcontroller, likely running
an RTOS such as FreeRTOS. The program is used to interface with a DHT11
temperature and humidity sensor, and display the results on an LCD screen.
Here is an explanation of each section of the code:

 /* USER CODE BEGIN Header */ : This is a user-defined section of the


code, where the user can include any relevant comments or information
about the code.
 #include "main.h" : This line includes the main.h header file, which
contains the necessary configurations and definitions for the main
function.
 #include "lcd.h" : This line includes the lcd.h header file, which
contains the necessary configurations and definitions for interfacing with
the LCD screen.
 #include "stdio.h" : This line includes the standard input/output library,
which is used for the printf() function.
 #include "string.h" : This line includes the string library, which is used
for the strcat() function.
 TIM_HandleTypeDef htim1; : This line declares a handle for the TIM1
timer, which is used for delay functions.
 void SystemClock_Config(void); : This is a function prototype for
the SystemClock_Config() function, which is used to configure the
system clock.
 static void MX_GPIO_Init(void); : This is a function prototype for
the MX_GPIO_Init() function, which is used to initialize the GPIO pins.
 static void MX_TIM1_Init(void); : This is a function prototype for
the MX_TIM1_Init() function, which is used to initialize the TIM1 timer.
 #define DHT11_PORT GPIOC : This line defines the DHT11 port as GPIOC.
 #define DHT11_PIN GPIO_PIN_8 : This line defines the DHT11 pin as
GPIO_PIN_8.
 uint8_t RHI, RHD, TCI, TCD, SUM; : These lines declare variables for
storing the relative humidity integral, relative humidity decimal, celsius
integral, celsius decimal, and checksum.
 float tCelsius = 0; float tFahrenheit = 0; float RH = 0; : These
lines declare variables for storing the temperature in celsius and
fahrenheit, and the relative humidity.
 Lcd_HandleTypeDef lcd; : This line declares a handle for the LCD
screen.
 void microDelay (uint16_t delay) : This is a function that creates a
delay using the TIM1 timer.
 uint8_t DHT11_Start (void) : This is a function that starts the
communication with the DHT11 sensor.
 uint8_t DHT11_Read (void) : This is a function that reads a byte from the
DHT11 sensor.
 SystemClock_Config() : This function configures the system clock.
 MX_GPIO_Init() : This function initializes the GPIO pins.
 MX_TIM1_Init() : This function initializes the TIM1 timer.
 main(): This is the main function, where the program starts.
 HAL_TIM_Base_Start(&htim1); : This line starts the TIM1 timer.
 Lcd_create() : This function creates an LCD handle.
 while (1): This is an infinite loop, where the program runs indefinitely.
 DHT11_Start() : This function starts the communication with the DHT11
sensor.
 DHT11_Read() : This function reads the temperature and humidity values
from the DHT11 sensor.
 printf(): This function prints the temperature and humidity values on
the LCD screen.
 HAL_Delay(2000); : This line creates a delay of 2 seconds.

In summary, this program initializes the necessary peripherals, communicates


with the DHT11 sensor to read the temperature and humidity values, and
displays the results on an LCD screen.
Is this conversation helpful so far?

why did we use a timer


Edit

The timer was used in this program to introduce a delay between the
transmission of the start signal and the beginning of the data transmission
from the DHT11 sensor. This delay is necessary because the DHT11 sensor
requires a certain amount of time to prepare the data after receiving the start
signal.
The timer is configured to generate an interrupt after a specified time period.
In this program, the timer is configured to generate an interrupt after 20
milliseconds. This delay allows the DHT11 sensor to prepare the data.

After the timer interrupt, the program reads the data from the DHT11 sensor.
The data is then processed and displayed on the LCD screen.

Here is the code snippet that demonstrates the use of the timer:

EditDownloadCopy code
1void SystemClock_Config(void);
2void MX_GPIO_Init(void);
3void MX_TIM1_Init(void);
4
5int main(void)
6{
7 HAL_Init();
8 SystemClock_Config();
9 MX_GPIO_Init();
10 MX_TIM1_Init();
11
12 Lcd_Init();
13 Lcd_Clear();
14
15 while (1)
16 {
17 HAL_TIM_Base_Start_IT(&htim1); // Start the timer in interrupt mode
18 HAL_Delay(200); // Wait for the timer interrupt to occur
19
20 DHT11_Start(); // Send the start signal to the DHT11 sensor
21 DHT11_Read(); // Read the temperature and humidity data from the DHT11 sensor
22
23 Lcd_SetCursor(0, 0);
24 Lcd_WriteString("Temp: ");
25 Lcd_WriteFloat(tCelsius);
26 Lcd_WriteString(" C");
27
28 Lcd_SetCursor(0, 1);
29 Lcd_WriteString("Humidity: ");
30 Lcd_WriteFloat(RH);
31 Lcd_WriteString("%");
32
33 HAL_Delay(2000); // Wait for 2 seconds before reading the sensor again
34 }
35}
In this code snippet, the HAL_TIM_Base_Start_IT() function is used to start the
timer in interrupt mode. The HAL_Delay() function is used to introduce a delay
of 200 milliseconds before reading the data from the DHT11 sensor. This
delay allows the DHT11 sensor to prepare the data.

After the delay, the program reads the temperature and humidity data from the
DHT11 sensor using the DHT11_Read() function. The data is then processed
and displayed on the LCD screen.

The timer is used in this program to introduce a delay between the


transmission of the start signal and the beginning of the data transmission
from the DHT11 sensor. This delay is necessary because the DHT11 sensor
requires a certain amount of time to prepare the data after receiving the start
signal.
Is this conversation helpful so far?

what start signal


Edit

Sources: controllerstech.com (1) blog.embeddedexpert.io (2) sheepdogguides


.com (3) edaboard.com (4)

The start signal used in the DHT11_Start function is a sequence of setting and
resetting the output of a specific pin ( DHT11_PIN on DHT11_PORT). Here is the
relevant code:

EditDownloadCopy code
1GPIO_InitTypeDef GPIO_InitStructPrivate = {0};
2GPIO_InitStructPrivate.Pin = DHT11_PIN;
3GPIO_InitStructPrivate.Mode = GPIO_MODE_OUTPUT_PP;
4GPIO_InitStructPrivate.Speed = GPIO_SPEED_FREQ_LOW;
5GPIO_InitStructPrivate.Pull = GPIO_NOPULL;
6HAL_GPIO_Init(DHT11_PORT, &GPIO_InitStructPrivate);
7
8HAL_GPIO_WritePin (DHT11_PORT, DHT11_PIN, 0); // pull the pin low
9HAL_Delay(20); // wait for 20ms
10HAL_GPIO_WritePin (DHT11_PORT, DHT11_PIN, 1); // pull the pin high
11microDelay (30); // wait for 30us
This code sets the pin as an output, pulls it low for 20ms, then pulls it high for
30us. This is the start signal for the DHT11 sensor.
Is this conversation helpful so far?

how is the data displayed


Edit

After the data is read from the DHT11 sensor, it is processed and then
displayed on an LCD screen. Here is the relevant code:

EditDownloadCopy code
1Lcd_cursor(&lcd,0,0);
2Lcd_string(&lcd,"tCelsius=");
3Lcd_cursor(&lcd,0,11);
4Lcd_float(&lcd,tCelsius);
5
6Lcd_cursor(&lcd,1,0);
7Lcd_string(&lcd,"RH=");
8Lcd_cursor(&lcd,1,5);
9Lcd_float(&lcd,RH);

This code sets the cursor position on the LCD screen and displays the
temperature in Celsius and the relative humidity in percentage.
The Lcd_cursor function sets the cursor position, the Lcd_string function
displays a string, and the Lcd_float function displays a floating point number.

Note that the Lcd_float function is a custom function defined in the lcd.c file,
which is not included in the code snippet provided. This function converts a
floating point number to a string and displays it on the LCD screen.

The Lcd_handleTypeDef struct, which is used to initialize and control the LCD
screen, is also defined in the lcd.c file.

So, the data is displayed on the LCD screen using


the Lcd_cursor, Lcd_string, and Lcd_float functions, which are defined in
the lcd.c file.
Is this conversation helpful so far?
and how is it prossessed
Edit

After the data is read from the DHT11 sensor, it is processed to convert the
raw binary data to temperature and humidity values. Here is the relevant
code:

EditDownloadCopy code
1RHI = DHT11_Read(); // Relative humidity integral
2RHD = DHT11_Read(); // Relative humidity decimal
3TCI = DHT11_Read(); // Celsius integral
4TCD = DHT11_Read(); // Celsius decimal
5SUM = DHT11_Read(); // Check sum
6
7if (RHI + RHD + TCI + TCD == SUM) { // Check sum correct
8 // Can use RHI and TCI for any purposes if whole number only needed
9 tCelsius = (float)TCI + (float)(TCD/10.0);
10 tFahrenheit = tCelsius * 9/5 + 32;
11 RH = (float)RHI + (float)(RHD/10.0);
12 Lcd_cursor(&lcd,0,0);
13 Lcd_string(&lcd,"tCelsius=");
14 Lcd_cursor(&lcd,0,11);
15 Lcd_float(&lcd,tCelsius);
16 Lcd_cursor(&lcd,1,0);
17 Lcd_string(&lcd,"RH=");
18 Lcd_cursor(&lcd,1,5);
19 Lcd_float(&lcd,RH);
20}

This code reads the raw binary data from the DHT11 sensor using
the DHT11_Read function, and stores it in the variables RHI, RHD, TCI, TCD,
and SUM. The check sum is then calculated by adding up all the data values
and comparing it to the check sum value read from the sensor. If the check
sum is correct, the temperature and humidity values are calculated by
converting the raw binary data to decimal values and storing them in the
variables tCelsius, tFahrenheit, and RH. The temperature and humidity
values are then displayed on the LCD screen using
the Lcd_cursor, Lcd_string, and Lcd_float functions, which are defined in
the lcd.c file.
Note that the Lcd_float function is a custom function defined in the lcd.c file,
which is not included in the code snippet provided. This function converts a
floating point number to a string and displays it on the LCD screen.

So, the raw binary data is processed to convert it to temperature and humidity
values by calculating the decimal values of the raw binary data and storing it
in the variables tCelsius, tFahrenheit, and RH. The temperature and humidity
values are then displayed on the LCD screen.
Is this conversation helpful so far?

why did we need a start function for output input


Edit

In the code, the DHT11_Start function is used to send a start signal to the
DHT11 sensor to initiate a data transfer. This is necessary because the
DHT11 sensor is a digital sensor that uses a single data line for both input and
output.

The start signal is used to wake up the sensor and indicate that the
microcontroller is ready to receive data. The start signal is generated by
pulling the data line low for at least 18 milliseconds, then releasing it and
waiting for the sensor to pull the data line low. This indicates that the sensor is
ready to send data.

The DHT11_Start function is defined as follows:

EditDownloadCopy code
1uint8_t DHT11_Start (void) {
2 uint8_t Response = 0;
3 GPIO_InitTypeDef GPIO_InitStructPrivate = {0};
4 GPIO_InitStructPrivate.Pin = DHT11_PIN;
5 GPIO_InitStructPrivate.Mode = GPIO_MODE_OUTPUT_PP;
6 GPIO_InitStructPrivate.Speed = GPIO_SPEED_FREQ_LOW;
7 GPIO_InitStructPrivate.Pull = GPIO_NOPULL;
8 HAL_GPIO_Init(DHT11_PORT, &GPIO_InitStructPrivate);
9 // set the pin as output
10 HAL_GPIO_WritePin (DHT11_PORT, DHT11_PIN, 0);
11 // pull the pin low
12 HAL_Delay(20);
13 // wait for 20ms
14 HAL_GPIO_WritePin (DHT11_PORT, DHT11_PIN, 1);
15 // pull the pin high
16 microDelay (30);
17 // wait for 30us
18 GPIO_InitStructPrivate.Mode = GPIO_MODE_INPUT;
19 GPIO_InitStructPrivate.Pull = GPIO_PULLUP;
20 HAL_GPIO_Init(DHT11_PORT, &GPIO_InitStructPrivate);
21 // set the pin as input
22 microDelay (40);
23 if (!(HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN))) {
24 microDelay (80);
25 if ((HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN)))
26 Response = 1;
27 }
28 pMillis = HAL_GetTick();
29 cMillis = HAL_GetTick();
30 while ((HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN)) && pMillis + 2 > cMillis) {
31 cMillis = HAL_GetTick();
32 }
33 return Response;
34}

The function first initializes the data pin as an output, then pulls it low for 20
milliseconds, then releases it and waits for the sensor to pull the data line low.
It then initializes the data pin as an input and waits for the sensor to send the
response signal. If the response signal is received, the function returns 1,
otherwise, it returns 0.

So, the DHT11_Start function is used to send a start signal to the DHT11
sensor to initiate a data transfer, and it also checks for the response signal
from the sensor. This is necessary to ensure that the sensor is ready to send
data and that the data transfer can proceed correctly.

You might also like