Author Topic: About PIC timer module (help)  (Read 5487 times)

0 Members and 1 Guest are viewing this topic.

Offline danielksTopic starter

  • Contributor
  • Posts: 19
  • Country: my
  • Newbie from Malaysia
About PIC timer module (help)
« on: October 19, 2011, 12:14:20 pm »
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-50 but I have no idea what it means..  :-[
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 10229
  • Country: nz
Re: About PIC timer module (help)
« Reply #1 on: October 19, 2011, 12:35:45 pm »
I seen people write like this.. TMR=255-50 but I have no idea what it means..  :-[

In terms of function   TMR=255-50 is exactly the same as TMR=205
The 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.
« Last Edit: October 19, 2011, 12:49:33 pm by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline danielksTopic starter

  • Contributor
  • Posts: 19
  • Country: my
  • Newbie from Malaysia
Re: About PIC timer module (help)
« Reply #2 on: October 19, 2011, 12:49:18 pm »
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 precisely

I 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...  :o

It only works when the TMR initial point is ranging from 0 to 55.. I have no idea what is going on..  :-[
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 10229
  • Country: nz
Re: About PIC timer module (help)
« Reply #3 on: October 19, 2011, 12:54:40 pm »
So it's possible to set the initial starting point of the timer inside ISR ?

yes, 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.

eg
Code: [Select]
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 stuff
               Sdelay=1;
           }
      }
}

The advantage of this is you can have many flags all set with different timing from the one timer ISR

So 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 flag


NOTE: 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.
« Last Edit: October 19, 2011, 01:32:04 pm by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Simon

  • Global Moderator
  • *****
  • Posts: 18038
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: About PIC timer module (help)
« Reply #4 on: October 19, 2011, 06:07:38 pm »
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
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11714
  • Country: my
  • reassessing directives...
Re: About PIC timer module (help)
« Reply #5 on: October 19, 2011, 07:10:46 pm »
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 precisely
I 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...  :o
It only works when the TMR initial point is ranging from 0 to 55.. I have no idea what is going on..  :-[
whats your prescaler setting? not quite understand whether you have too fast or too slow issue?
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 

Offline danielksTopic starter

  • Contributor
  • Posts: 19
  • Country: my
  • Newbie from Malaysia
Re: About PIC timer module (help)
« Reply #6 on: October 20, 2011, 04:04:23 am »
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 precisely
I 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...  :o
It only works when the TMR initial point is ranging from 0 to 55.. I have no idea what is going on..  :-[
whats your prescaler setting? not quite understand whether you have too fast or too slow issue?

I'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.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 10229
  • Country: nz
Re: About PIC timer module (help)
« Reply #7 on: October 20, 2011, 04:49:04 am »
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.
« Last Edit: October 20, 2011, 04:56:46 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline deephaven

  • Frequent Contributor
  • **
  • Posts: 796
  • Country: gb
  • Civilization is just one big bootstrap
    • Deephaven Ltd
Re: About PIC timer module (help)
« Reply #8 on: October 20, 2011, 08:07:14 am »
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.

The clock going into the prescaler/timer is 1/4 of the crystal frequency, so it's a 1/4 of those values.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 10229
  • Country: nz
Re: About PIC timer module (help)
« Reply #9 on: October 20, 2011, 08:34:29 am »
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.

The clock going into the prescaler/timer is 1/4 of the crystal frequency, so it's a 1/4 of those values.

Ah. That makes more sense.
I'm not all that familiar with PICs
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11714
  • Country: my
  • reassessing directives...
Re: About PIC timer module (help)
« Reply #10 on: October 20, 2011, 10:14:10 am »
I'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.
ok. 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.
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 10229
  • Country: nz
Re: About PIC timer module (help)
« Reply #11 on: October 20, 2011, 10:16:21 am »
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.
« Last Edit: October 20, 2011, 10:18:01 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11714
  • Country: my
  • reassessing directives...
Re: About PIC timer module (help)
« Reply #12 on: October 20, 2011, 10:34:39 am »
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.
lol. 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 blink :D sorry.
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 10229
  • Country: nz
Re: About PIC timer module (help)
« Reply #13 on: October 20, 2011, 10:42:14 am »
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.
lol. 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 blink :D sorry.

Sorry, 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)
« Last Edit: October 20, 2011, 10:45:35 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf