# Thread: Real time clock + external interrupt in one PIC possible?

1. ## Real time clock + external interrupt in one PIC possible?

Hello you all!

its several years since I postet something here.

My current problem:

I need to measure the average speed of a car. I got an input signal from the driveshaft (one per revolution) wich I use for a distance couter with a 16F88 . The program uses "on interrupt" Now I need to divide the distance by real time after pushing a button. I got two favorite PICs because I´m an total inept programmer : 16F88 and 18F1320. Wrote my last program some years ago.
The clock and the incomming driveshaft pulses both use interrupts and I think thats too much for one of these PICs. No I´m not migrating to DSPic or 24F.

Any suggestions out there?

2. ## Re: Real time clock + external interrupt in one PIC possible?

The PIC18F1320 would be my choice only given the 2 you offered. It easily can handle 2 Interrupts, and offers an internal Oscillator.

- I would run the OSC at 4 MHz.
- I would use INT0 (RB0) for the driveshaft trigger input. The Interrupt Handler should increment your counter variable every time the driveshaft makes another pass.
- I would use TMR1 to create the 1 second timer (actually 1/2 second timer).
> T1CON = %0011000X; .7 = TMR1H & TMR1L written to independently, <5-4> = 1:8 Prescale, .1 = Clock source is Fosc/4, .0 = On/Off
> Preload TMR1 = 3035 (T1 will tick at 0.000008 seconds, times 62500 (65535 - 3035) = 0.5 seconds); TMR1H = \$0B, TMR1L = \$DB

Since your TMR1_INT will occur every half second, it would probably be easiest to calculate your vehicle's speed by running the math around that. Say for instance your counter variable = 35 when your TMR1_INT overflows. You double it and get 70 driveshaft pulses per second. If you need better resolution, you could create a variable that will count 1/2 second TMR1 Interrupts and calculate vehicle speed only after 10 have occurred; which is 5 seconds. Be sure to clear your counter variable after using it in your calculation.

There is some detail here, and some things left ambiguous. Hopefully this guides you to a working piece.