Electronics > Projects, Designs, and Technical Stuff

(1/3) > >>

danielks:
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..  :-[

Psi:

--- Quote from: danielks on October 19, 2011, 12:14:20 pm ---I seen people write like this.. TMR=255-50 but I have no idea what it means..  :-[

--- End quote ---

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.

danielks:
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..  :-[

Psi:

--- Quote from: danielks on October 19, 2011, 12:49:18 pm ---So it's possible to set the initial starting point of the timer inside ISR ?

--- End quote ---

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: ---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;
}
}
}

--- End code ---

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

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

Simon:
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