2024-06-17 03:28 AM
I am using a STM32L431KCU6 microcontroller and reading data from I2C every 90ms. I would like to implement a sleep mode to achieve lower power consumption, I am using timer6 which generates an interrupt every1ms for system run time. I was thinking I can change the timer period to be 90ms prior to put the device on sleep and when TIM6 interrupt happens (wake up the processor) I can change the period to 1ms for normal operation. is this possible? and how can I achieve this?
I don't have any experience in using sleep mode but I have been reading similar questions and I've seen WFI coming up very frequently. Is there any example codes available? or documentation which can assist in writing code?
Thanks in advance and appreciate any help.
Kind Regards
Manpreet Singh
2024-08-01 06:07 AM
Hello @msingh08,
Yes, you can change the timer period to 90ms before putting the device to sleep and then change it back to 1ms upon waking up, the code should look something like this:
__HAL_TIM_SET_AUTORELOAD(&htim6, 90000 - 1); // Set Timer6 period to 90ms
__HAL_TIM_CLEAR_FLAG(&htim6, TIM_FLAG_UPDATE); // Clear update flag
__HAL_TIM_ENABLE_IT(&htim6, TIM_IT_UPDATE); // Enable update interrupt
__HAL_TIM_ENABLE(&htim6); // Start Timer6
// Enter sleep mode
// Wake up and reconfigure Timer6 for normal operation
__HAL_TIM_SET_AUTORELOAD(&htim6, 1000 - 1); // Set Timer6 period to 1ms
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.