2018-02-21 09:25 AM
Hey guys.
I'm working with an ultra sound piece for measuring the distance.
When i set the timer1 to count every 1 ms it works(i set PSC 59999 and ARR 1000 to get 1 second with 120M cpu clock.).
But when i set the PSC 11 and ARR 1 to get a 0.1 us interrupt its not correct and it doesn't count correctly.
Do you know what is the problem?
2018-02-21 09:31 AM
You aren't going to be able to interrupt at 10 MHz (100ns), you're going to need to be realistic, and do things at high rates in hardware.
Depending on the load the interrupt causes, you might expect to support several hundred KHz.
2018-02-21 09:37 AM
Your interval is too short for the mcu.
Much better for you to ask for a better approach to do what you want to do, than to try the impossible.
2018-02-21 12:53 PM
I want to work with this HC_SR04 module to measure distance.
The ultra sound speed is 340 m/s so for a centimeter accuracy i need at least 30 us .
What should i do?
2018-02-21 01:09 PM
What if i do not use interrupt and just use the timer?
In this case am i able to count every at least 1 us(1MHz clock)?
2018-02-21 01:31 PM
TIM2 is 32-bit, have it count maximally, ie ARR=0xFFFFFFFF. You can set the prescaler to count at 1 MHz, 10 MHz or 60 MHz (ie 16.67ns ticks)
If you have an input pulse you could use one of the channels and latch the count, ie timestamp on your chosen timebase.
The DWT_CYCCNT is also available, it is 32-bit wide and counts 120 MHz core ticks.
2018-02-21 01:38 PM
The other problem i have is that when i use HAL DRIVERS to generate the code i don't know how to reset the timer.I have seen the whole hal libraries PDF and searched through it but i just found start and stop timer.There was no reset option.I decided to do it with TIM1->CNT but it was 0 all the time the timer was working and it didn't increment.
2018-02-21 01:58 PM
Depends on the nature of the signal you are trying to capture. For example if you are trying to measure the distance between two pulses, you can use the capture function of the timer.
You have to understand the signal before you decide on an approach. Not the other way around.
2018-02-21 02:42 PM
Writing CNT should work, if it is not counting you have other issues, you'd want to inspect the TIM registers, and make sure the peripheral clock was enabled in the APB2ENR and not held in reset in APB2RST
2018-02-22 04:37 AM
The problem is that i was working on LPC series about a year and it wasn't as complex as stm mcu's are especially i used only registers to write my code without any problem.But these mcu's have a lot of registers and working with them is not easy and they are a little different.For example in lpc series i just enabled the timer and the timer counter counted until it reaches its final value,but in stm series you need to set ARR to set the max value and i thought its just for generating interrupt.So the problem was when i didn't use interrupt i didn't give ARR any number so the CNT did not count.
Thank u guys for the help,