Hello,
I have a program that uses two comparators and Timer0. It is supposed to turn off the led when the voltage from the bridge rectifier drops below Comparator 2 and turn on when it >= Comparator 1. It seems to work like it is supposed to except when the voltage stays below Comparator 2 reference until the super cap discharges and the pic actually shuts down. When the voltage comes back to turn both comparators on the program seems lost. If you vary the voltage above and below Comp1 and Comp2 reference, the program works again. I have tied MCLR to Vdd thru 1k. I have posted the code below. Does anybody know whats going on? Pic is an 18F4550
Thanks Don
Code:
OSCCON = $60 'Set up oscillator 4mhz
CMCON = %00000101
TRISB = %01000000 'Set port B to output
TRISA = %11001111 'Set port A to input
T0CON = %01000110 'Set up Timer 0 Config bits prescale 128
INTCON.6 = 1 'Enable all unmasked peripheral interrupts
RCON.7 = 0 'Disable interrupt priority
Intcon.5 = 1 'Timer 0 overflow interrupt enable bit
'** While the output of Cout1 and Cout2 is 0 (Bridge Rectifier voltage is
'** greater than reference) this loops in the While Wend. If one or the
'** other changes to a 1 which it does every pulse unless the
'** voltage drops below reference for longer than 20 ms in which case
'** Intcon.2 bit is set, it drops down and reloads the timer and loops
'** back to Main.
Main:
TMR0L = $64 'Preload Timer0 to overflow after 20ms
T0con.7 = 1 'Start Timer0
Intcon.2 = 0 'Clear Timer0 overflow interrupt bit
While CMCON.6 = 0 and CMCON.7 = 0 'If it stays in this loop longer than 20ms
'Intcon.2 bit is set
if Intcon.2 = 1 then gosub Led 'If this bit is set, Timer0 has overflowed
'meaning Cout1 and Cout2 has remained low for
'longer than 20ms and branches to Led.
wend
TMR0L = $64 'Reload Timer0 to overflow after 20ms
goto main
LED:
Low portb.4 'Turn off Led to signify outage
Intcon.2 = 0 'Reset Timer0 overflow bit
While CMCON.6 = 0 'If the voltage is below Comparator 1
'reference or between Comparator 1 and
'Comparator 2 the program loops here
'until voltage rises enough to turn on
'Comparator 1.
wend
high portb.4 'Turn on Led to signify power on
return
End
Bookmarks