Skip to main content
Graduate
July 19, 2024
Question

Clock Configuration, Timer and LED

  • July 19, 2024
  • 2 replies
  • 2366 views

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.

    This topic has been closed for replies.

    2 replies

    Super User
    July 19, 2024

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

    This prevents timer from running. 

    JW

    Super User
    July 19, 2024

    @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.

    027iconicAuthor
    Graduate
    July 22, 2024

    @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.

    Super User
    July 23, 2024

    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);