cancel
Showing results for 
Search instead for 
Did you mean: 

Can someone tell me what is the error in my code? (DHT11, STM32F429i-disc1)

CMart.2
Associate II

I have the STM32F4291-disc1 microcontroller and the dht11 sensor that measures temperature and humidity. I'm trying to get the sensor to work, but I don't want the data (Temperature and humidity) on the screen and I don't know what could be wrong. My main.c code is as follows:

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */
/* USER CODE END Header */
 
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "cmsis_os.h"
#include "usb_host.h"
 
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "i2c-lcd.h"
#include "stdio.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 ---------------------------------------------------------*/
CRC_HandleTypeDef hcrc;
 
DMA2D_HandleTypeDef hdma2d;
 
I2C_HandleTypeDef hi2c3;
 
LTDC_HandleTypeDef hltdc;
 
SPI_HandleTypeDef hspi5;
 
TIM_HandleTypeDef htim1;
TIM_HandleTypeDef htim6;
 
UART_HandleTypeDef huart1;
 
SDRAM_HandleTypeDef hsdram1;
 
osThreadId defaultTaskHandle;
/* USER CODE BEGIN PV */
 
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_CRC_Init(void);
static void MX_DMA2D_Init(void);
static void MX_FMC_Init(void);
static void MX_I2C3_Init(void);
static void MX_LTDC_Init(void);
static void MX_SPI5_Init(void);
static void MX_TIM1_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_TIM6_Init(void);
void StartDefaultTask(void const * argument);
 
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void delay (uint16_t time)
{
	/* change your code here for the delay in microseconds */
	__HAL_TIM_SET_COUNTER(&htim6, 0);
	while ((__HAL_TIM_GET_COUNTER(&htim6))<time);
}
 
void Display_Temp (float Temp)
{
	char str[20] = {0};
	lcd_put_cur(0, 0);
 
	sprintf (str, "TEMP:- %.2f ", Temp);
	lcd_send_string(str);
	lcd_send_data('C');
}
 
void Display_Rh (float Rh)
{
	char str[20] = {0};
	lcd_put_cur(1, 0);
 
	sprintf (str, "RH:- %.2f ", Rh);
	lcd_send_string(str);
	lcd_send_data('%');
}
 
uint8_t Rh_byte1, Rh_byte2, Temp_byte1, Temp_byte2;
uint16_t SUM, RH, TEMP;
 
float Temperature = 0;
float Humidity = 0;
uint8_t Presence = 0;
 
void Set_Pin_Output (GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	GPIO_InitStruct.Pin = GPIO_Pin;
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);
}
 
void Set_Pin_Input (GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	GPIO_InitStruct.Pin = GPIO_Pin;
	GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);
}
 
#define DHT11_PORT GPIOC
#define DHT11_PIN GPIO_PIN_3
 
void DHT11_Start (void)
{
	Set_Pin_Output (DHT11_PORT, DHT11_PIN);  // set the pin as output
	HAL_GPIO_WritePin (DHT11_PORT, DHT11_PIN, 0);   // pull the pin low
	delay (18000);   // wait for 18ms
    HAL_GPIO_WritePin (DHT11_PORT, DHT11_PIN, 1);   // pull the pin high
	delay (20);   // wait for 20us
	Set_Pin_Input(DHT11_PORT, DHT11_PIN);    // set as input
}
 
uint8_t DHT11_Check_Response (void)
{
	uint8_t Response = 0;
	delay (40);
	if (!(HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN)))
	{
		delay (80);
		if ((HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN))) Response = 1;
		else Response = -1; // 255
	}
	while ((HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN)));   // wait for the pin to go low
 
	return Response;
}
 
uint8_t DHT11_Read (void)
{
	uint8_t i,j;
	for (j=0;j<8;j++)
	{
		while (!(HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN)));   // wait for the pin to go high
		delay (40);   // wait for 40 us
		if (!(HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN)))   // if the pin is low
		{
			i&= ~(1<<(7-j));   // write 0
		}
		else i|= (1<<(7-j));  // if the pin is high, write 1
		while ((HAL_GPIO_ReadPin (DHT11_PORT, DHT11_PIN)));  // wait for the pin to go low
	}
	return i;
}
/* 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_CRC_Init();
  MX_DMA2D_Init();
  MX_FMC_Init();
  MX_I2C3_Init();
  MX_LTDC_Init();
  MX_SPI5_Init();
  MX_TIM1_Init();
  MX_USART1_UART_Init();
  MX_TIM6_Init();
  /* USER CODE BEGIN 2 */
  HAL_TIM_Base_Start(&htim6);
 
  lcd_init();
  lcd_send_string("INITIALISING>>>>");
  HAL_Delay(2000);
  lcd_clear ();
  /* USER CODE END 2 */
 
  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */
 
  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */
 
  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */
 
  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */
 
  /* Create the thread(s) */
  /* definition and creation of defaultTask */
  osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
  defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
 
  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */
 
  /* Start scheduler */
  osKernelStart();
 
  /* We should never get here as control is now taken by the scheduler */
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
	  Display_Temp(Temperature);
	  Display_Rh(Humidity);
 
	  DHT11_Start();
	  	  Presence = DHT11_Check_Response();
	  	  Rh_byte1 = DHT11_Read ();
	  	  Rh_byte2 = DHT11_Read ();
	  	  Temp_byte1 = DHT11_Read ();
	  	  Temp_byte2 = DHT11_Read ();
	  	  SUM = DHT11_Read();
 
	  	  TEMP = Temp_byte1;
	  	  RH = Rh_byte1;
 
	  	  Temperature = (float) TEMP;
	  	  Humidity = (float) RH;
  }
  /* USER CODE END 3 */
}

22 REPLIES 22

Then use a debugger to look at the values or output to a serial port.

Get the screen output routines functional, so they can provide useful information.

The values read may provide some clue as to where the problem is, as the code as provided can't be compiled by anyone else.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Tektronics TDS 210

Saleae Logic Pro 16

If you know what signals you're actually generating, and you know how the DHT expects them, you can look for discrepancies.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

How could I take the output to a serial port and get the screen output routines work, so they can provide useful information?

I don't think I can get either one..

rromano001
Senior

Hello ,your code has a big flaw so never get executed.

From few line is clear you enabled FREERTOS, main loop is there but remember never get executed.

/* Start scheduler */
  osKernelStart(); <<-- THIS IS LAST LINE EXECUTED, After this control is passed to OS executive.
 
  /* We should never get here as control is now taken by the scheduler */
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */ ----> DEAD CODE follow
  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
	  Display_Temp(Temperature);
	  Display_Rh(Humidity);
 
	  DHT11_Start();
	  	  Presence = DHT11_Check_Response();
........

Your code MUST be moved to default task or additional task you have there.

Remember uSecond delay are not reliable, OS can switch task anytime, this void timing.

DHT11, 22 use a variant of OneWire protocol, so strict timing are out of luck on preemptive OSes.

Your code resemble what I remember from Arduino code. This code was used by my pupils, I suggested to add timeout, it was blocking too often.

Again strict timing and preemption are incompatible, when you are looping OS can preempt your task and get control after a long while expecially if you use network service.

Suggested solution can be to use DMA and USART, STM32 has a one wire mode I used to access DS with just one pin. It can be simply tailored to your application.

I don't use anymore DHT product, check alternate like HTU21D, 31, BME280/680, Si7021 and why not ST you can find on development. I2C product can share LCD bus without trouble. Just remember use a state machine on one task to avoid contention.

Regards

hi CMart2,

>I don't know if you read them successfully, because the screen appears blank and does not show any value.<

This sentence has no sense to us, we cannot read nothing on your side.

Inferring you seen a blank screen, I suppose

  lcd_init();
  lcd_send_string("INITIALISING>>>>");
  HAL_Delay(2000);
  lcd_clear ();

this segment is executed in front of scheduler and you see just text then a "blank screen"

rest of code follow what I wrote.

I mean when I run the code, the screen doesn't show me the temperature and humidity values, and I don't know what to do to show it.

I have used stwstudio to know if, even if it doesn't show the temperature and humidity values, it reads them. But the graph shows that he doesn't read it.

I do not understand because the code I am using now works with the stm32f407g board, but with the stm32f429i it does not.

I understand that the problem is "osKernelStart ();", but I don't understand how I can solve the problem and make it work "while (1)

 {".

I have tried using a USART pin but nothing appears. I would like to be able to change the sensor, but I am forced to do it with the dht11.

Is FREERTOS also enabled on f407 ?

Same code cannot run.

move to defaulttask or one of tasks

Is no FREERTOS on f407?

Then no scheduler get control and superloop get CPU service

Somewhere in generated code some task handler skeleton are provided.

Preemptive multitask is transferred there by the executive of oskernel.

void StartDefaultTask(void const * argument)
{
  /* USER CODE BEGIN 5 */
  /* Infinite loop */
  for(;;)
  {
     osDelay(1);
  }
  /* USER CODE END 5 */ 
}
 
/* USER CODE BEGIN Header_StartTask02 */
/**
* @brief Function implementing the myTask02 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask02 */
void StartTask02(void const * argument)
{
  /* USER CODE BEGIN StartTask02 */
  /* Infinite loop */
  for(;;)
  {
	 osDelay(1);
  }
  /* USER CODE END StartTask02 */
}
 

here code get control. Main is no more executed after scheduler line

No difference changing processor, Smallest F030 or F7xx,, non ST too behave same.