PDA

View Full Version : Timers using Pic16F819



bearpawz
- 26th October 2004, 15:46
Please forgive me if this all has been covered before, Ive tried searching and just cant seem to find the answer I need...

Here is the Problem I need to solve: I am building an electronic odometer. The specs for the vehicle in question say that there are 4000 pulses from the speed sensor per mile. So I need to count pulses some how. Because the PIC is going to be sending out serial information (the odometer reading) to VFD, I want to be sure that I do not miss any pulse counts so instead of using code to poll a pin I am guessing using the Timer1 as a counter is what would work best. Is this right?


If that is right, then how do I scale the timer so that it rolls over at 4000 counts and sets the interupt flag?

Melanie
- 27th October 2004, 03:29
Clock (Pulse) Input is on RB6, T1OSC must be disabled. See Figure 7-2.

T1CON=%00000111

See Register 7-1 as to why.

TMR1 will interrupt when it rolls from $FFFF thru to $0000, so for 4000 counts preset the counter (Registers TMR1H & TMR1L) with $F060 ($0000-$0FA0). That means you'll get 4000 counts ($0FA0 Counts) before it interrupts.

Your Interrupt Service Routine must reset TMR1 back to $F060 after each interrupt (as well as resetting the appropriate interrupt Flag)..

I’ll let you sort out the configuration of INTCON (see section 12-10, INTCON Register 2-3, PIE1 Register 2-4 and PIR1 register 2-5) – after all, you’ve got to do something…

Tip: If you interrupt every 400 rather than every 4000 counts, you'll be able to count increments of tenths of a mile - not just miles.

Depending on the latency of your program, several pulses may have been counted after an interrupt has been generated before you get around to servicing the interrupt. Use a similar technique to that described in the Olympic Timer example to account for the extra pulses so none get missed.

Melanie

bearpawz
- 27th October 2004, 15:20
Thanks so very much. Being I'm some what new to the MCU world Im just not always sure Im reading the data sheets right (when I even understand what Im reading)