2018-10-07 10:24 AM
Hello,
I'm having some trouble using the timer based interrupts.
I have some very basic experience in mcu programing but I'm new to the STM32 platform and I had to adapt to it rather quickly. Therefore I lack some of the basic knowledge.
My first question is how to set up a timer interrupts properly.
I've looked it up of course but there is something I don't understand.
The prescaler is supposed to divide the clock frequency by a given value.
The timer should now has the frequency of the base clock/(prescaler value+1) and then a counter should count every tick of the timer up to the amount given by the Counter Period.
This is my clock configuration. I assume the timer I've set will have a frequency of 108 MHz.
Setting the prescaler to 1079 should give me 108 MHz/1080 = 100 KHz frequency.
If I want to have 1KHz sampling rate (one sample in every ms) I can set the Counter Period to 99 and I'll get 100*(1/100 KHz) = 0.001s as results.
However I've measured the results and I never get a frequency that is divisible by 10 if I set the prescaler to 1079. So I assume the source of my timer is different from what I believed it to be.
My timer's clock source is set to 'Internal clock'. But according to my measurements it's neither 216 MHz, nor 108 MHz.
I've managed to calculate my way out of this problem and found a Prescaler/Counter Period value that'll give me my required frequency but I'm still interested in how can I find the frequency of my timer?
My second question is how can I use this interrupt to get data from a sensor with a constant sampling rate.
I've used some functions from the HAL libs to test and measure the frequency of my interrupt. I got the results I've wanted so I've implemented my already written code into the interrupt.
I communicate with the sensor via i2c. I use 400 KHz fast mode and my sensor's data rate is set to 1 KHz (increasing it didn't help).
After running the code everything works fine but the sampling rate drops. I assume the execution of the functions in the interrupt routine takes longer than it takes to the timer to overflow and restart the interrupt.
I've removed some functions from the routine and the frequency got faster but still not satisfying.
I know I should only put very basic instructions into a time-critical interrupt. And of course I should avoid using functions too. But I work with a 216 MHz device so I don't think it's supposed to a problem with a 1 KHz interrupt.
I think the problem could be with the i2c communication. But I only send one and receive maybe two small strings.
Is this an expected problem in this scenario with an already existing solutions or is the problem most likely lies in my code?