cancel
Showing results for 
Search instead for 
Did you mean: 

Strange values when reading timer CNT

Guidoted
Associate III

I have a timer (TIM10) with a period of 500 us, running without interrupt or any other function.

Then I have a rising+falling signal interrupt on PB0 (EXTI0) driven with a 31.25 kHz square wave.

In EXTI0 I read the CNT value into a variable New, evaluate the difference with the previous CNT value Old (obviously considering the case when Old value is greater than New value) and copy the New into Old for the next EXTI0 interrupt.

What I see is that many times the difference between New and Old, which should be about 16 us, is even 40, 80, 100 us or more!

I've checked with a signal output on a pin and a scope that I have EXTI0 interrupts correctly every 16 us, without any interrupt loose or other strange behaviours.

Seem's like sometimes (many times...) the CNT value read is not the current one but some strange old value.

What could be the problem?

Thanks

 

15 REPLIES 15

Please read "the interrupt happens every about 16 microseconds"

 

If you stop mcu execution by breakpoint and the external pulse train and/or TIM keep running, the results are confusing.

Chose a different method of debugging - using debuggers is intrusive.

JW

Thanks JW, but I don't watch the CNT values, I watch the values of variables timerDiff16, timerNew16 and timerOld16.

Are you saying that debugger is changing the variable values?

 

> Are you saying that debugger is changing the variable values?

No.

The debugger stops execution upon your breakpoint, but TIM10 continues to run and rolls over many times until you start executing again (and also EXTI continues to be triggered by the incoming pulses).

That, unless you have set the respective DBGMCU_APBx_FZ.DBG_TIMx_STOP bit, directly or indirectly through some tickbox in your debugging environment/IDE). But even then, the "problem" with EXTI being already triggered while execution is stopped, remains, so the next difference will not reflect the true difference between edges anyway.

I repeat, debug this in some other way, which does not involve breakpoints and execution stops. Or, if you insist, take this effect into account - e.g. have a counter in the ISR, which allows it to be executed at least twice in a row without the breakpoint, and only then allow the breakpoint:

static uint32_t cnt;

... // do the thing with reading TIMx_CNT and making the diff here
if (cnt == 2) {
  __NOP; // place breakpoint here
  cnt = 0;
} else {
  cnt++;
}

JW

BarryWhit
Senior

unless you have set the respective DBGMCU_APBx_FZ.DBG_TIMx_STOP bit, 

Configuring the chip to stop the timers when the debugger breaks makes life much easier when debugging capture values.

I repeat, debug this in some other way, which does not involve breakpoints and execution stops.

Monitoring the captured values using the "Live expressions" functionality in CubeIDE would work, but note you can't be 100% sure you're not blinking precisely when some outlier event occurs, since the values are constantly updating.

 

Simplest would be to check the value in software and turn on a LED if it exceeds some threshold (do not reset the LED once set). If the LED stays off, you're ok.

- If a post has answered your question, please acknowledge the help you received by clicking "Accept as Solution".
- Once you've solved your issue, please consider posting a summary of any additional details you've learned. Your new knowledge may help others in the future.

Many thanks JW and Barry,

now I understood what happens and clearly my problem is that EXTIO0 interrupt contnues to run with my square wave input on PB0 and continues to change the variable values.

I will do more test to understand it better.

Thanks again and best regards