About PIC timer module (help) - Page 1
Forum section: electronics / projects

author: danielks
author_rank: Contributor
timestamp: October 19, 2011, 12:14:20 pm
content: Can anyone tell me how to set the initial starting point of the timer ?For example, I want it to start counting from 50, how should i write it ?TMR=50 ?I seen people write like this..TMR=255-50but I have no idea what it means..

author: Psi
author_rank: Super Contributor
timestamp: October 19, 2011, 12:35:45 pm
content: Quote from: danielks on October 19, 2011, 12:14:20 pmIn terms of function TMR=255-50 is exactly the same as TMR=205The reason someone might write 255-50 is just for convenience.Lets say you have an overflow interrupt doing some work (overflow happens on an 8bit counter when it tries to count one more from 255).If you want 50 counts before the overflow happens you could set TMR=205 then start the counter and you'd get 50 counts then an overflow interrupt, but in the future you may want to change the count from 50 to 47.Writing it like 255-50 makes it easy to change later to 255-47 without having to keep trying to work out the new value.The compiler will do the math itself when you compile, so either approach will come up with exactly the same machine code.Note, i might be 1 out with my counts, i cant remember if there's an extra count to trigger the overflow, there probably is, so 205 is going to overflow at the 51th timer clock.

author: danielks
author_rank: Contributor
timestamp: October 19, 2011, 12:49:18 pm
content: So it's possible to set the initial starting point of the timer inside ISR ?Currently I'm generating PWM using timer interrupt and running on 20MHz crystal..I'm generating 50Hz servo signal but if the timer starts at 0 and I'll need around 391 times of overflow interrupt to get that PWM frequency. but 391 is too small.. I cant control my pulse width more preciselyI did try to set my timer starting point like 100.. theoretically if I'm still using 391 times of O.F interrupt, the PWM frequency will be more than 50Hz but i'm getting frequency lower than that...It only works when the TMR initial point is ranging from 0 to 55.. I have no idea what is going on..

author: Psi
author_rank: Super Contributor
timestamp: October 19, 2011, 12:54:40 pm
content: Quote from: danielks on October 19, 2011, 12:49:18 pmyes, definitely.I do that all the time to change how often the overflow ISR occurs.When it overflows and calls the interrupt you can give it a new value in the ISR and it will instantly start counting from there.=255 and it will overflow on the next clock or =0 and it will have to count all the way.You can then put a variable in the ISR that is incremented up to a set point to flag X counts has occurred. This flag can be used to do stuff in your main program loop.egCode:ISR{if (Sdelay!=0) { Sdelay++; if (Sdelay>3) {Sdelay=0;} }if (Ldelay!=0) { Ldelay++; if (Ldelay>200) {Ldelay=0;} }TMR=255-50;};main {while(1) {if (Sdelay==0) {// do some stuffSdelay=1;}}}The advantage of this is you can have many flags all set with different timing from the one timer ISRSo there are 3 ways to adjust your timing- The clock source/prescaler for the hardware timer your using. ( clk/256 etc.)- The timer's count register, high vales overflow quickly, low values will have to count up.- The set point of how many counts it takes before setting a flagNOTE: Most micros have PWM modes that do special things with output compare interrupts, if you put the timer into a special PWM mode it's behavior changes. So if you're using a hardware PWM mode the timer may not behave like a normal timer.

author: Simon
author_rank: Unknown
timestamp: October 19, 2011, 06:07:38 pm
content: after a certain frequency of PWM you will start to loose resolution, basically it is clock frequency/4/1024 = max PWM frequency at 10 bit resolution

author: Mechatrommer
author_rank: Super Contributor
timestamp: October 19, 2011, 07:10:46 pm
content: Quote from: danielks on October 19, 2011, 12:49:18 pmwhats your prescaler setting? not quite understand whether you have too fast or too slow issue?

author: danielks
author_rank: Contributor
timestamp: October 20, 2011, 04:04:23 am
content: Quote from: Mechatrommer on October 19, 2011, 07:10:46 pmI'm using 1:1 prescaler. I'm saying that using 391 times of over flow interrupt on 1:1 prescale and I'm getting 50Hz when timer starting point is 0 but if I set the initial starting point of the timer to for example 100, I should get more than 50Hz if i'm still using 391 times of interrupt.

author: Psi
author_rank: Super Contributor
timestamp: October 20, 2011, 04:49:04 am
content: a 20 mhz xtal and a 1:1 prescaler should result in..- an 8bit timer overflowing ~78000 times per second- an 16bit timer overflowing ~ 300 times per second(unless you change the counter register to make it overflows sooner)A 1:1 prescaler at 20mhz is pretty fast, i wouldn't have thought you'd need anything like that timer clock rate.

author: deephaven
author_rank: Frequent Contributor
timestamp: October 20, 2011, 08:07:14 am
content: Quote from: Psi on October 20, 2011, 04:49:04 amThe clock going into the prescaler/timer is 1/4 of the crystal frequency, so it's a 1/4 of those values.

author: Psi
author_rank: Super Contributor
timestamp: October 20, 2011, 08:34:29 am
content: Quote from: deephaven on October 20, 2011, 08:07:14 amAh. That makes more sense.I'm not all that familiar with PICs

author: Mechatrommer
author_rank: Super Contributor
timestamp: October 20, 2011, 10:14:10 am
content: Quote from: danielks on October 20, 2011, 04:04:23 amok. where did you put the TMR = 100 code? do you use polling or interrupt routine to check for OF? did your TMR=100 is executed each time OF is flagged? did you clear the flag? what PIC do you use, so somebody can check in the datasheet the timer and ISR behaviour. i'm afraid if i check in my pic16f690 or even pic10f200 datasheet and give you some advice, it wont apply to your PIC64F. so you will successfully have our time wasted. "usually" and if you do it correctly, setting TMR to larger value will have its IF registers flagged and interrupt vector called sooner. and as usual Microchip got some fancy procedure to do it correctly, such as you cannot set directly after OF, must wait n cycles bla bla etc etc. if i were you, i will blink a led on one of its pin each time the interrupt for debug purpose, so i can check in DSO what is happening wrong.

author: Psi
author_rank: Super Contributor
timestamp: October 20, 2011, 10:16:21 am
content: Yeah, as Mechatrommer says, try and get it working at a slow speed with leds.Then you can see and understand how and why it works before trying to do it at fast speeds.

author: Mechatrommer
author_rank: Super Contributor
timestamp: October 20, 2011, 10:34:39 am
content: Quote from: Psi on October 20, 2011, 10:16:21 amlol. when did i say work at slow speed? the fastest pwm (led blink) achievable with 5MIPS is only around 1MHz which can be easily seen in any decent DSO... and that you dont do timing do dont anything, just led blinksorry.

author: Psi
author_rank: Super Contributor
timestamp: October 20, 2011, 10:42:14 am
content: Quote from: Mechatrommer on October 20, 2011, 10:34:39 amSorry, i just meant to get it working with leds as you suggested.Slow speed was my idea so he can get his timer working and producing a visual delay that makes sense given all the variables etc.. xtal, prescaler, timer register.(We don't know if he has a CRO/DSO)