cancel
Showing results for 
Search instead for 
Did you mean: 

Clock Configuration, Timer and LED

027iconic
Associate II

I am still new to STM and I am trying to perform a task on timer, I have written my code to configure the clock the my desired frequency and also I have written the code to set up my timer but when I downloaded the code to my board (stm32g070rb), my LED is supposed to blink for 3 seconds but it seems nothing is happening.

6 REPLIES 6

> TIM3->ARR = 1 - 1; // Auto-reload value for 1 microsecond tick

This prevents timer from running. 

JW

TDK
Guru

@waclawek.jan shows why the timer isn't running, but there are other issues.

You cannot interrupt the system at 1 MHz. There just isn't enough cpu resources to handle one interrupt before the next one happens, so your code will get stuck re-entering the interrupt and make no progress anywhere else.

Limit your interrupt frequency to 10 kHz or so. Less is better.

With a target 3 second toggle rate, set your timer update rate to 10 Hz or so to minimize overhead due to timer interrupts.

If you feel a post has answered your question, please click "Accept as Solution".

@TDK Can you kindly help me edit the code to meet this specification??? I've tried from my end the implement the change but then I still could not get it to work.

The HAL_Delay function will give you a ms-precision delay. Given you are blinking LEDs, that should be more than sufficient.

 

If you need more precision, a free-running 32 bit counter (TIM2 or TIM5) is the best way to implement a delay. After setting up and starting the timer, the code to do the delay is very simple:

 

uint32_t start = TIM2->CNT;

while (TIM2->CNT - start < delta);

 

If you feel a post has answered your question, please click "Accept as Solution".

I am not using HAL library, is there any material you can recommend for programming STM32 from scratch using uVision. I use to have issues navigating the necessary materials online, all I have been seeing are either using cubeIDE or HAL library.

The reference manual has all the info you need if you want to do register-level programming.

If you feel a post has answered your question, please click "Accept as Solution".