2025-04-29 2:53 AM - last edited on 2025-04-29 3:07 AM by Andrew Neil
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.
2025-04-29 5:50 AM
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 ?
2025-04-29 5:55 AM - edited 2025-04-29 6:20 AM
That's not a CAN related 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.
2025-04-29 6:22 AM
@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.
2025-04-29 6:51 AM
So i should use another timer instead of TIM6 so i can use it as input capture or PWM input ?
2025-04-29 8:11 AM
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)