Pic gets lost after power outage
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
Confiuration fuses and Vdd
Hi Steve,
This is the configuration:
Code:
NOLIST
ifdef PM_USED
LIST
"Error: PM does not support this device. Use MPASM."
NOLIST
else
LIST
LIST p = 18F4550, r = dec, w = -311, f = inhx32
INCLUDE "P18F4550.INC" ; MPASM Header
;CONFIG PLLDIV = 1
CONFIG FOSC = INTOSCIO_EC
CONFIG WDT = OFF
CONFIG PBADEN = OFF
CONFIG LVP = off
CONFIG WRTB = ON
CONFIG CPB = off
CONFIG MCLRE = On
NOLIST
endif
LIST
EEPROM_START EQU 0F00000h
BLOCK_SIZE EQU 32
As for Vdd, It slowly goes down as the Super Cap bleeds down (The program needs to perform a couple of other things when the power goes out and the Super Cap is still holding voltage) I don't have this part written yet. When the power comes back on, Vdd comes back while charging the Super Cap up.
Thanks, Don