PDA

View Full Version : Help with Comparator and Timer0



dbachman
- 14th February 2009, 04:04
I have been trying to write some code around the GIF that Darrel Taylor showed me but with mixed results. I have included the GIF and the code. Can someone take a look at it to see if what I am doing is a start? I am just using the LED to see if anything is working. It flashes but not when I expect it should

Thanks Don


(From Darrel Taylor)
I'll second the Comparator idea, but with a little difference. (Don't filter the signal)

Set a timer to have a delay of 20ms or more.
Using the comparator Interrupts, reload that timer every time you get a pulse.

If the voltage drops below the comparator's trigger point, the timer will overflow.
Use the timer interrupt/flag to determine when there's an error condition




Here is what I have for code:

OSCCON = $60 'Set up oscillator 4mhz
CMCON = %11000001
TRISB = %00000000 'Set port B to output
TRISA = $%11111111 'Set port A to input
T0CON = %01000110 'Set up Timer 0 Config bits prescale 128
INTCON.7 = 1 'Enable all unmasked interrupts
INTCON.6 = 1 'Enable all unmasked peripheral interrupts
RCON.7 = 0 'Disable interrupt priority
Intcon.5 = 1 'Timer 0 overflow interrupt enable bit
PIE2.6 = 1 'Comparator interrupt enable bit

Main:

While Pir2.6 = 0 or Intcon.2 = 0 'Check to see if comparator interrupt bit or Timer0 interrupt bit is set
wend
if TMR0IF = 1 then gosub LED
TMR0L = $64 'Preload Timer 0
Pir2.6 = 0 'Clear comparator interrupt bit
goto main


LED:

High portb.4
pause 100
low portb.4
pause 100
goto LED

End

Darrel Taylor
- 14th February 2009, 20:19
When you enable GIE (INTCON.7) you have to have an Interrupt Handler for it to jump to.

If you just want to Poll the flags (as shown), then don't set GIE.
<br>