Link to home
Start Free TrialLog in
Avatar of vpool
vpool

asked on

PIC16f877 Timer

Hi everyone,
I cant unerstand how the followin piece of code generates a pulse of 10 us please help....please give your comments

Note - assumes a 20MHz crystal, which is 5MHz timer clock
//A 1:4 prescaler is used to give a 1.25MHz timer count (0.8uS per tick)
//PIC16f877    

        TMR1H = 0xff;                                        // prepare timer for 10uS pulse
      TMR1L = -14;
      T1CON = 0x21;                                       // 1:4 prescale and running
      TMR1IF = 0;      
      trig = 1;                                           // start trigger pulse
      while(!TMR1IF);                    // wait 10uS
      trig = 0;                        // end trigger pulse
      TMR1ON = 0;                  // stop timer
Avatar of PaulCaswell
PaulCaswell
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi vpool,

I'd suggest you look here:

http://ww1.microchip.com/downloads/en/DeviceDoc/30292c.pdf

I am sorry but its much too hardware for me so I cant answer your question but I got to page 55 but my eyes started to glaze over at that point.  

Paul

By the way, this question might be better off in the 'Assember' page here at EE? I can move it there if you like. Just post here and I'll move it.
Hi there - this is my understanding of the code, although I have never used this chip:

The pulse is started here:
 trig = 1;                                    // start trigger pulse

Now the timer starts incremeneting its count every clock cycle.  The while loop below says "loop while TMR1 has not reached 31" - because the loop body contains no code there are no instructions to execute, it just keeps looping.

while(!TMR1IF);                 // wait 10uS

When TMR1 reaches 31 the loop ends and the pulse is ended with this line:

trig = 0;                    // end trigger pulse


The fact that 31 clock cycles = 10uS simply reflects the clock speed (and you have to factor in the prescale setting which determines how much the timer is incremented per clock cycle).  If the clock went twice as fast the same code would give a 5uS pulse.  If the clock was half as fast the same code would give a 20uS pulse.
ASKER CERTIFIED SOLUTION
Avatar of steveiam
steveiam

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of steveiam
steveiam

A couple of comments on basicinstinct's answer..

> while(!TMR1IF);                 // wait 10uS
> When TMR1 reaches 31 the loop ends and the pulse is ended with this line:

Not sure where you're getting your 31 from!

This while loop is watching the TMR1 overflow flag.  When the overflow happens, its set to 0, and then exits the while loop.  You aren't actually watching the value in the timer itself, but the flag it sets when the timer has counted all the way up to its maximum, and rolled over to 0.

> The fact that 31 clock cycles = 10uS simply reflects the clock speed
> (and you have to factor in the prescale setting which determines how
> much the timer is incremented per clock cycle)

This is partly right.  However, the timer is always incremented by one, every time it is incremented.  The prescaler (elsewhere in the code) sets how fast the increments happen.  The load value for the timer (-14) is not clock cycles.

It is correct, though, that the speed of the timer is dependant on the clock speed of the chip... either the external oscillator that you use, or the internal oscillator.