cancel
Showing results for 
Search instead for 
Did you mean: 

How do I use timer 7 on an STM32L433 to time an event?

JMala.3
Associate III

I am trying to use timer7 to give me the number of counts when an event occurs.  I could not get this to work, and wrote some test code to show the issue.

This test code should set the initial count to zero, then fill an array with 10 timer values as the firmware run through the "for" loop. The problem is every time through the while(1) loop, I get an array with each element about 20 counts more than the last value, as expected, but the first value shows that the count is never set to zero.

I expect something like 10, 30, 50, 70 in the array every time through while loop, but instead I get 1010, 1030, 1050 on the first run, 3610, 3630, 3650 on the second run etc.

I have a really simple question,. how do I reset the count to zero?

Note that I am not looking for alternate methods.

    /* set counter pre-scale to 1 (16MHz) */
    TIM7->PSC = 0;
    TIM7->ARR = MAX_COUNT;                   /* set the reload value */
    TIM7->CNT = 0;
    TIM7->SR &= ~TIM_SR_UIF;                /* clear the update flag */
    TIM7->CR1 |= TIM_CR1_CEN;               /* Start the Timer */
 
    while(1)
    {
        TIM7->CNT = 0;
        for(i = 0; i < 10; i++)
        {
            CountArray[i] = TIM7->CNT;
        }
    }

2 REPLIES 2
Piranha
Chief II

The CNT is reset, but the problem is that most likely you are stopping the CPU with the debugger and during that time the timer keeps running. If you really want, you can freeze it by setting the bit DBG_TIM7_STOP in register DBGMCU_APB1FZR1.

JMala.3
Associate III

Thanks Piranha, that was the issue. I assumed that setting the breakpoint on the TIM7->CONT = 0 line would break before the counter was updated, but apparently not. I added a dummy line above that line and set the breakpoint there and now the array is filled with the values I expected, I also added your freeze suggestion, which I found was DBGMCU->APB1FZR1 |= DBGMCU_APB1FZR1_DBG_TIM7_STOP;

Thanks again for your help