Skip to main content
Associate III
April 29, 2025
Question

Using TIM6 and TIM2 in STM32f407 Discovery board: DHT11 sensor and CAN frames

  • April 29, 2025
  • 6 replies
  • 1649 views

Hello everyone,

I'm using the STM32f407 Discovery board.
I'm trying to use TIM2 to send a frame in a cyclic way and also i'm using TIM6 to send the data of a DHT11 sensor. The problem is that when i added the TIM6 i no longer receiver the frames.
Who should i adjust my code in order to work with the DHT11 sensor data and still send my frames.
I tried to use the DHT11 sensor separately to make sure that the code works and it works perfectly.

6 replies

mƎALLEm
Technical Moderator
April 29, 2025

Hello,

First, could you please state on this thread? if the provided answer solved your issue please accept it as solution.

Second, 


@Azizz wrote:

The problem is that when i added the TIM6 i no longer receiver the frames.


Which frame? CAN frames? if yes from which node?

"To give better visibility on the answered topics, please click on ""Accept as Solution"" on the reply which solved your issue or answered your question."
AzizzAuthor
Associate III
April 29, 2025

The problem occurs in these lines 

 if (HAL_TIM_Base_Start_IT(&htim2) != HAL_OK)
 {
 Error_Handler();
 }

 HAL_TIM_OC_Start_IT(&htim2, TIM_CHANNEL_1);

I cannot send any frame neither the cyclic one or the frame sent by request.

Andrew Neil
Super User
April 29, 2025

@Azizz wrote:

The problem occurs in these lines 


What problem occurs, exactly?

Does it hit the Error-Handler ?

If so, look at what value HAL_TIM_Base_Start_IT returned; step into it to find why ...

 


@Azizz wrote:

when i added the TIM6 i no longer receiver the frames.


Is that because nothing is transmitted at all, or the transmission becomes "bad"?

If "bad", in what way(s) is it bad?

 

PS:

 


@Azizz wrote:

I'm trying to use TIM2 to send a frame in a cyclic way and also i'm using TIM6 to send the data of a DHT11 sensor. The problem is that when i added the TIM6 i no longer receiver the frames.


If you just start TIM6 without doing any DHT11 stuff, are the [CAN?] frames still OK

ie, is it the timer itself, or something else in your DHT11 code that's causing the problem?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
AzizzAuthor
Associate III
April 29, 2025

Yess when i remove the line commented i receive the frames

void Send_Cyclic_Frame_A (void) {
	uint8_t temp = 0, humi = 0;

 //DHT11_GetData(&temp, &humi);

	TxHeader.StdId = 0x103;
	TxHeader.RTR = CAN_RTR_DATA;
	TxHeader.IDE = CAN_ID_STD;
	TxHeader.DLC = 2;
	TxHeader.TransmitGlobalTime = DISABLE;

	TxData[0] = temp;
	TxData[1] = humi;

	if (HAL_CAN_AddTxMessage(&hcan1, &TxHeader, TxData, &TxMailbox)!= HAL_OK )
	{
		 Error_Handler();
	}
}
AzizzAuthor
Associate III
April 29, 2025

It is not transmitted at all and when i remove these lines the program works but the frame is no longer send cyclically.
It doesn't hit he error handler when i try to use the step over button in the line showed below, it blocks the program.

 if (HAL_TIM_Base_Start_IT(&htim2) != HAL_OK)



Andrew Neil
Super User
April 29, 2025

@Azizz wrote:

 when i try to use the step over button in the line showed below, it blocks the program.


So step into that function to see where & why it blocks ...

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
AzizzAuthor
Associate III
April 29, 2025
void delay_us(uint16_t time) {
 __HAL_TIM_SET_COUNTER(&htim6, 0);
 while (__HAL_TIM_GET_COUNTER(&htim6) < time);
}

Here is the delay_us function that i implemented. You can find the whole dht11.c in the zip file i sent earlier.
Who can i add a delay of 170us ?

mƎALLEm
Technical Moderator
April 29, 2025

@Azizz wrote:
void delay_us(uint16_t time) {
 __HAL_TIM_SET_COUNTER(&htim6, 0);
 while (__HAL_TIM_GET_COUNTER(&htim6) < time);
}

Here is the delay_us function that i implemented. You can find the whole dht11.c in the zip file i sent earlier.
Who can i add a delay of 170us ?


So I think all your code is based on the polling which is not a good idea. As I said rewrite your code and use interrupts instead of polling.

"To give better visibility on the answered topics, please click on ""Accept as Solution"" on the reply which solved your issue or answered your question."
AzizzAuthor
Associate III
April 29, 2025

So i should use another timer instead of TIM6 so i can use it as input capture or PWM input ?

mƎALLEm
Technical Moderator
April 29, 2025

That's not a CAN related subject it's related to how your code is organized and how you selected the NVIC priorities for the system tick, timer, and CAN.

Need to rewrite DHT11_Check_Response() with interrupt instead of polling on a GPIO pin status. I don't know how the DHT11 sensor is working but think to use a timer as input capture or PWM input to read the sensor instead of that blocking code.

"To give better visibility on the answered topics, please click on ""Accept as Solution"" on the reply which solved your issue or answered your question."
AzizzAuthor
Associate III
April 29, 2025

So i tried adding the interrupt function by adding this portion of code 

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) {
	if (htim->Instance == TIM3){
		 // Lire la valeur capturée
		 capture_time2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);

		 // Si capture_time2 est plus grand que capture_time1, cela signifie que c'est une transition de montée
		 if (capture_time2 > capture_time1) {

		 	 // Calculer la durée de la pulse entre capture_time1 et capture_time2
		 uint32_t pulse_width = capture_time2 - capture_time1;

		 // Déterminer si le pulse correspond à un bit 0 ou 1 en fonction de sa durée
		 if (pulse_width > 50 && pulse_width < 100) { // Environ 26-28 µs pour bit bas
		 // Bit 0
		 data_buffer[bit_count / 8] &= ~(1 << (7 - (bit_count % 8))); // Mettre à 0 le bit correspondant
		 }

		 else if (pulse_width > 100 && pulse_width < 150) { // Environ 70 µs pour bit haut
		 // Bit 1
		 data_buffer[bit_count / 8] |= (1 << (7 - (bit_count % 8))); // Mettre à 1 le bit correspondant
		 }

		 bit_count++;

		 if (bit_count >= DHT11_MAX_BITS) {
		 // Lecture terminée
		 // Traiter les données (par exemple, les afficher ou les transmettre à une autre partie du programme)
		 Process_DHT11_Data();
		 }
		 }

		 // Mettre à jour capture_time1 pour la prochaine capture
		 capture_time1 = capture_time2;
 }
}

The problem is i never get the data from the DHT11 sensor because the program never enters this code and always repeats the function void HAL_TIM_PeriodElapsedCallback (TIM_HandleTypeDef *htim)