Hola y gracias por responder: Da igual si grabo in circuit o no, me da el mismo problema.
Me parece que lo que dice sispic es lo que me ocurre pero... es extraño que si pongo una pausa antes del sleep me funcione sin problemas, y si la quito nunca puedo recuperar el pic (no se deja regrabar). Claro que es porque corre el programa y se pone en modo sleep antes que yo pueda iniciar la grabación.
Aqui os dejo el programa, es un poco extenso.
Es un dispositivo receptor-transmisor por RF. Recibe la información y la compara con su propio código y si es correcto la reenvía. Básicamente es así.
;Device: CC1020
;System parameters:
;X-tal frequency: 14.745600 MHz Internal
;X-tal accuracy: +/- 10 ppm
;RF frequency A: 434.052000 MHz Active Rx
;RF frequency B: 434.052000 MHz Inactive Tx
;Frequency separation: 4.950 kHz
;Data rate: 4.800 kBaud
;Data Format: Manchester Accurate
;RF output power: 10 dBm
;Channel spacing: 25 kHz
;Modulation: GFSK Dithering enabled
;Lock: Continuous
;Carrier sense offset: 10 dBm DCLK squelch disabled
;Operator Mode: Rx
;; IMPORTANT : INTERFACE REGISTER IS SET AS 0x0C BECAUSE IS CONTROLLING
;; THE ONBOARD ANTENNA SWITCH.
List p=16F819 ;Tipo de procesador
include "P16F819.INC" ;Definiciones de registros internos
__config _CP_OFF & _INTRC_IO & _WDT_OFF & _PWRTE_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_ON & _CCP1_RB3
ERRORLEVEL -302 ;suppress "not in bank 0" message
ERRORLEVEL -203 ;suppress message
ERRORLEVEL -305 ;suppress message
ERRORLEVEL -205 ;suppress message
;SPI variables
Rxddato equ 20h ;configuration data received from 1020 buffer
Txddir equ 21h ;address config buffer for SPI
Txddato equ 22h ;data config buffer for SPI
;General variables
PDel0 equ 23h ;delay var
PDel1 equ 24h ;delay var
PDel2 equ 25h ;delay var
contdata equ 26h ;data counter for cc1020 registers table
delaycont equ 27h ;counter for TMR1 delay timer
shiftreg equ 28h ;data in/out shift register from/to cc1020 DIO
cont127 equ 29h ;outter counter for lock timeout loop
cont255 equ 0x2a ;inner counter for lock timeout loop
bitcounter equ 0x2b ;8 bits counter for RX and TX data ring
cont0 equ 0x2c ;counter for preamble 5x 55h
cont2 equ 0x2d ;TMR0 loop counter
cont3 equ 0x2e ;TMR0 loop counter
Flag equ 0x2f ;flag general porpouse
preambleerror equ 30h ;preamble error counter
recdata equ 31h ;received data buffer
preamblecount equ 32h ;preamble ok counter
buffTx equ 33h ; buffer TX datos
cont1 equ 34h ;counter for beeps of turn on alarm
itemp equ 35h ;TMR0 counter, increment 1 bit on every TMR0 overflow (10msx50=500ms) used for alarm flip-flop sound
itemp2 equ 36h ;TMR0 counter, increment 1 bit every itemp time out (500msx80=40s) used for RX ok timeout after 40s
;and start sounding the alarm
cont4 equ 37h ;contador parpadeo led en sby
;itemp3 equ 38h ;temporizador contador 3x33ms =100ms
;Save special register variables
temp_WREG equ 0x70 ;W register save
temp_STATUS equ 0x71 ;STATUS register save
temp_WImage equ 0x72 ; errr, dunno what´s this
temp_FSR equ 0x73 ;FSR save
__WImage equ 0x74 ;errr, dunno either
;Constants
preamble55h equ 55h ;preamble option 1 value
preambleaah equ 0xAA ;preamble option 2 value
U1code equ 0x33 ;first ID byte
U2code equ 0xcc ;second ID byte
sby equ 0x4f ;standby special code with high power output setting
period equ .63 ;2khz PWM period
duty equ .32 ;2khz PWM duty cycle
highpower equ 0xf0; high power value for PA_POWER +10db
lowpower equ 0fh ; low power value for PA_POWER -20db
boyaH equ .4-1 ; data value (buoy ID)
boyaL equ boyaH+80h
shutdown equ 0x99
#define timeoutRX Flag,7 ; flag timeout recepcion retry
#define RXOK Flag,6 ;flag de recepción buena
#define alarmduty Flag,5 ;alarm duty cycle 0= cycle off , 1= cycle on
#define alarmflag Flag,4 ;alarm flag on=1 , off=0
#define poweroutput Flag,3 ;output power flag: 1=low power 0= high power
#define U2found Flag,2 ;second ID header found flag
#define U1found Flag,1 ;first ID header found flag
#define preamblefound Flag,0 ;preamble found flag
; Define 16f819 and cc1020 ports
#define LEDR PORTA,1 ;TX send & low bat test led
#define LEDV PORTA,0 ;TX send & bat ok test led
#define LEDA PORTA,2 ;calibration done ok yellow led (test)
#define DIO PORTB,7 ;data in/out from/to 1020
;#define LOWBAT PORTA,7 ;input from bat low sensor circuitry
#define POWER PORTB,0 ;in/out pin for magnetic switch on/off (interrupt)
#define PDO PORTB,1 ;configuration data output from 1020 (read register)
#define PDI PORTB,2 ;configuration data input to 1020 (write register)
#define PWM PORTB,3 ;PWM output for alarm
#define PCLK PORTB,4 ;configuration clock output to 1020
#define DCLK PORTB,5 ;data clock from 1020
#define PSEL PORTB,6 ;1020 chip select configuration pin (low enable)
;#define LATCH PORTB,7 ;power suply latch output pin
;MACROS
push MACRO
movwf temp_WREG ;save copy w register
movf STATUS,w
bcf STATUS,RP0 ;set bank to 0
movwf temp_STATUS ;save status
movf FSR,w ;
movwf temp_FSR ;save copy of FSR
movf __WImage,w ;
movwf temp_WImage ;save copy of __WImage
ENDM
pop MACRO
bcf STATUS,RP0 ;ensure bank is set to 0
movf temp_WImage,w ;
movwf __WImage ;restore __WImage
movf temp_FSR,w ;
movwf FSR ;restore FSR
movf temp_STATUS,w
movwf STATUS ;restore status and bank information
swapf temp_WREG,f ;
swapf temp_WREG,w ;restore working register
ENDM
ORG 0x00
goto init ;go to initialize program
ORG 0x04
push
call interrupt
pop
retfie
include "1020spiuniv.INC" ;Include SPI configuration registers routine
;Data table for cc1020 configuration registers
tabladat clrf PCLATH
addwf PCL,F
retlw 0x00 ;main (no se usa pero debo ponerlo) 00h
retlw 0x0C ;interface 01h
retlw 0xff ;reset 02h
retlw 0x8f ;sequencing 03h
retlw 0x3a ;freq 2a 04h
retlw 0x14;0x10 ;freq 1a 05h
retlw 0x99;0x01 ;freq 0a 06h
retlw 0x3a ;clock a 07h
retlw 0x3a ;freq 2b 08h
retlw 0x1f;0x1a ;freq 1b 09h
retlw 0x45;0xab ;freq 0b 0Ah
retlw 0x3a ;clock b 0Bh
retlw 0x44 ;VCO 0Ch
retlw 0x51 ;MODEM 0Dh
retlw 0x9b ;deviation 0Eh
retlw 0x87;0xc6 ;AFC CONTROL 0Fh
retlw 0x2f ;FILTER 10h
retlw 0x65;0x61 ;VGA1 11h
retlw 0x57;0x55 ;VGA2 12h
retlw 0x2f ;VGA3 13h
retlw 0x2d;0x33 ;VGA4 14h
retlw 0x20;0x41 ;LOCK 15h
retlw 0x78 ;FRONTEND 16h
retlw 0x46;0x47 ;ANALOG 17h
retlw 0x54 ;Buff Swing 18h
retlw 0x22 ;Buff Current 19h
retlw 0xAE ;PLL BW 1Ah
retlw 0x35 ;Calibrate 1Bh
retlw 0xff ;PA POWER 1Ch
retlw 0x00 ;MATCH 1Dh
retlw 0x00 ;PHASE COMP 1Eh
retlw 0x00 ;GAIN COMP 1Fh
retlw 0x00 ;POWERDOWN 20h
;Initializing the microprocessor
init clrf PORTB ;clear port B
clrf PORTA ;clear port A
bsf STATUS,RP0
bcf STATUS,RP1 ;bank 1
movlw 0x70 ;oscilator register 819- 8MHZ
movwf OSCCON ;set to 0x60 if 4mhz
movlw b"10100011"
movwf TRISB
movlw 0x87 ;set pull-up resitors off and
movwf OPTION_REG ;preescaler 256 for TMR0,
movlw 0x06
movwf ADCON1 ;set analog port off
movlw b"00110000" ;RA0 red led. RA1 green led. RA3 yellow led
;RA5 mclr.
movwf TRISA
movlw 40h ; set the SPI port
movwf SSPSTAT
movlw period-1
movwf PR2
bcf STATUS,RP0 ;switch bank 0
movlw 11h ;Set TMR1 on & Timer prescaler for delays
movwf T1CON ;
bsf LEDV
call delay500 ;delay if user is still pressing the switch
bcf LEDV
bsf PSEL ;chip select deactivated
movlw 20h
movwf SSPCON ; set SPI port
call reset_1020
call power_down
movlw b"10110000" ;interrupts on RB0 falling edge (external turn off switch)
;and TMR0 overflow
movwf INTCON
bcf INTCON,GIE
sleep
;initialising SPI and Timers
movlw duty
movwf CCPR1L
bsf T2CON,1 ;Set TMR2 Timer prescaler for PWM alarm
call turnon ;turning on alarm subroutine
bsf INTCON,GIE
;bcf INTCON,TMR0IE
iniTMR0 movlw .0
movwf TMR0 ;initial TMR0 value
bsf INTCON,TMR0IE
clrf itemp2
clrf Flag ;clear all the flags
;Initializing the cc1020
setup1020
call reset_1020 ;Reset the cc1020 module
call configura ;Configurate
call wakeup_tx ; Wake up to TX mode
call calib ;Calibrate in TX
btfss Rxddato,7 ;show the cal done led
goto setup1020 ;cal failed, try again
call wakeup_rx ; Wake up to RX mode
call calib ;Calibrate in RX
btfss Rxddato,7 ;show the cal done led
goto setup1020 ;cal failed, try again
call power_down ;Power down the module
bcf RXOK
bcf timeoutRX
main
call wakeup_rx
bcf alarmduty ;alarm duty cycle 0= cycle off , 1= cycle on
bcf alarmflag ;alarm flag on=1 , off=0
bcf poweroutput ;output power flag: 1=low power 0= high power
bcf U2found ;second ID header found flag
bcf U1found ;first ID header found flag
bcf preamblefound ;preamble found flag
clrf itemp2
clrf shiftreg ;clear the shift register buffer
clrf bitcounter ;Clear the bitcounter
clrf preamblecount; clear the preamble ok counter
clrf preambleerror; clear the preamble error counter
call setup_rx
;comienzo rastreo señal por RX
BUCLE btfss RXOK ;Flag,6 recepcion ok
goto $+3
btfsc timeoutRX
goto dormir
btfsc DCLK ;testear si capturamos recepcion de byte
goto $-1 ;hubo recepcion
btfss DCLK
goto $-1
;Read bit from CC1020
bcf STATUS,C ;clear carry
rlf shiftreg,1 ;rotate the buffer (MSB)
bsf shiftreg,0 ;set bit 0 (default value)
btfss DIO ;check if a 1 was received
bcf shiftreg,0 ; clear bit 0 (MSB)
;Identified valid preamble before?
btfss preamblefound ; test if preamble was found before (FLAG)
goto testpreamble2 ;no, go to compare preamble routine
;Received SOF1?
btfsc U1found ;flag U1 found ?
goto testU2 ; yes, test U2
movlw U1code ;it´s first ID byte?
xorwf shiftreg,w
btfss STATUS,Z
goto testpreamble ;no, go to test if it´s a preamble
bsf U1found ;yes it´s U1. Set flag
goto BUCLE
;Received SOF2?
testU2 btfsc U2found ;was U2 found (flag)
goto testdata ;yes, go to test data (boya)
incf bitcounter,1
movlw 8
xorwf bitcounter,w
btfss STATUS,Z
goto BUCLE
clrf bitcounter
movlw U2code ;it´s first ID byte?
xorwf shiftreg,w
btfss STATUS,Z
goto failed
bsf U2found
goto BUCLE
testdata incf bitcounter,1
movlw 8
xorwf bitcounter,w
btfss STATUS,Z
goto BUCLE
clrf bitcounter
movlw boyaH ;it´s data boya byte?
xorwf shiftreg,w
btfsc STATUS,Z
goto perfetoH ;yes, go to OK boya led and exit
movlw boyaL ;it´s data boya byte?
xorwf shiftreg,w
btfsc STATUS,Z
goto perfetoL ;yes, go to OK boya led and exit
movlw sby ;it´s standby byte?
xorwf shiftreg,w
btfsc STATUS,Z
goto standbyte ;yes, go to standby led and exit
; movlw shutdown ;es orden de apagado por RF?
; xorwf shiftreg,w
; btfsc STATUS,Z
; goto apagado_remoto ;si, ir a subrutina apagado remoto para apagar por RF
goto failed ; none of the 3 allowed data bytes
failed bcf preamblefound
bcf U1found
bcf U2found
goto BUCLE
;If we are still receiving preamble do nothing
testpreamble movlw preamble55h
xorwf shiftreg,w
btfsc STATUS,Z
goto BUCLE
movlw preambleaah
xorwf shiftreg,w
btfsc STATUS,Z
goto BUCLE
movf preambleerror,1
btfsc STATUS,Z
incf preambleerror,1
incrpreambleerror movf preambleerror,1
btfss STATUS,Z
incf preambleerror,1
movlw 11
subwf preambleerror,w
btfss STATUS,C
goto BUCLE
bcf preamblefound
goto BUCLE
testpreamble2 movlw preamble55h
xorwf shiftreg,w
btfsc STATUS,Z
goto incrpreamble ;it´s 55h
movlw preambleaah
xorwf shiftreg,w
btfss STATUS,Z
goto preambleexit
incrpreamble incf preamblecount,1
movlw 4
subwf preamblecount,w ;Test if preamble required reached
btfss STATUS,C
goto BUCLE
bsf preamblefound ;set preamble found flag
clrf preambleerror ;clear the error flag
clrf preamblecount ;clear the preamble counter
goto BUCLE
standbyte
bcf alarmflag; clr the alarm flag
clrf itemp2 ;clear the 40s alarm timeout counter
call power_down
movlw .3 ;blink 3 veces de led
movwf cont4
batok1 bsf LEDV
call delay30
bcf LEDV
call delay30
decfsz cont4
goto batok1
call delay7s
goto main
perfetoL bsf poweroutput ;baja potencia
movlw boyaL
goto mando
perfetoH
bcf poweroutput ; alta potencia
movlw boyaH
mando movwf buffTx
bcf alarmflag; clr the alarm flag
clrf itemp2 ;clear the 40s alarm timeout counter
bsf RXOK ;flag RX ok
bcf timeoutRX; flag a 0
clrf itemp ;iniciar a 0 timer
batok
call setup_tx
bsf LEDV
call send_data
bcf LEDV
goto main+1;BUCLE
;Dormir
dormir
bcf RXOK
;call reset_1020 ;Reset the cc1020 module
;call configura ;Configurate
; call wakeup_tx ; Wake up to TX mode
; call calib ;Calibrate in TX
; btfss Rxddato,7 ;show the cal done led
; goto dormir+1 ;cal failed, try again
; call wakeup_rx ; Wake up to RX mode
; call calib ;Calibrate in RX
; btfss Rxddato,7 ;show the cal done led
; goto dormir+2 ;cal failed, try again
call power_down ;Power down the module
call delay7s
goto main
preambleexit clrf preamblecount
goto BUCLE
;Get the stream to transmit
send_data movlw .5
movwf cont0 ; send 55h 5 times
preamblesend movlw preamble55h
call TX ; send the byte to cc1020 DIO
decfsz cont0
goto preamblesend
movlw U1code ; send 1st header
call TX
movlw U2code ; send 2nd header
call TX
movf buffTx,w ; send the ONLY ONE BYTE DATA
call TX
movlw 0xff ; send the dummy to let PA shut down properly
call TX
return
;TX data bit-bashing RF data sending routine
TX movwf shiftreg ;store the outgoing data
movlw .8 ;init the bit counter for 8bits (1 byte)
movwf bitcounter
TXnext bcf STATUS,C ;clear carry
rlf shiftreg,f ;rotate data (MSB)
btfsc STATUS,C ;if C =1 then put a "1"
bsf DIO ;pin DIO high
btfss STATUS,C ;if C =0 then put a "0"
bcf DIO ;pin DIO low
btfss DCLK ;is sync high?
goto $-1
btfsc DCLK ;is sync low?
goto $-1
decfsz bitcounter,f ;loop untill all 8 bits are send
goto TXnext
return
;Reset the cc1020
reset_1020 clrf Txddir ;write MAIN reg
movlw 0x1e
movwf Txddato ;get into reset
call TxD
clrf Txddir ;write MAIN reg
movlw 0x1f ;get out of reset
movwf Txddato
call TxD
return
;Configurating all the cc1020 registers
configura movlw .1 ; +1 for jumping the MAIN data on the table
movwf contdata ;Set initial data counter of cc1020 registers
jump movf contdata,W
movwf Txddir ; set the address register
call tabladat ;call table for data registers
movwf Txddato ; put it on the data buffer
call TxD ; write register
incf contdata,1 ;increment address counter
movf contdata,W
sublw .32
btfss STATUS,Z; end of table?
goto jump
return
; calibrate cc1020
calib
movlw 0x1C ; write PA_POWER reg
movwf Txddir
clrf Txddato ; PA POWER off
call TxD
movlw 0x1B
movwf Txddir ;init calibration
movlw 0xB5
movwf Txddato
call TxD
;Monitor cal done STATUS bit (0x80)
movlw .127
movwf cont127
xx movlw .255
movwf cont255
call pollstatus ; poll STATUS reg
btfsc Rxddato,7 ; compare bit 7 for cal done
goto lock
decfsz cont255 ;Timeout
goto $-4
decfsz cont127
goto xx
;Monitor lock STATUS bit (0x10)
lock movlw .127
movwf cont127
xxx movlw .255
movwf cont255
call pollstatus ; poll STATUS reg
btfsc Rxddato,4 ; compare bit 4 for lock
goto PAon
decfsz cont255 ;Timeout
goto $-4
decfsz cont127
goto xxx
PAon movlw 0x1C
movwf Txddir ;write PA_POWER reg
movlw 0xff
movwf Txddato ;restore PA POWER
call TxD
return
;Lectura registro STATUS cc1020
pollstatus
movlw 40h ;write STATUS reg/address
movwf Txddir
call RxD ;read STATUS data
return
;wake up to RX
wakeup_rx
clrf Txddir ;write MAIN reg
movlw 0x1b ;xtal osc on
movwf Txddato
call TxD
call DEMORA2 ;wait untill xtal stabilise
clrf Txddir ;write MAIN reg
movlw 0x19 ;bias on
movwf Txddato
call TxD
call demora150us ;wait 150us
clrf Txddir ;write MAIN reg
movlw 0x11 ;frec syntetisser on
movwf Txddato
call TxD
return
;wake up to TX
wakeup_tx
clrf Txddir ;write MAIN reg
movlw 0xdb ;xtal osc on
movwf Txddato
call TxD
call DEMORA2 ;wait untill xtal stabilise
clrf Txddir ; write MAIN reg
movlw 0xd9 ;bias on
movwf Txddato
call TxD
call demora150us ;wait 150us
clrf Txddir ; write MAIN reg
movlw 0xd1 ;frec syntetisser on
movwf Txddato
call TxD
return
;puts cc1020 in rx mode (use wake up to rx first if coming from PD mode)
setup_rx
bsf STATUS,RP0
bsf TRISB,7 ;setup (DIO) as input
bcf STATUS,RP0
clrf Txddir ; write MAIN reg
movlw 0x11 ; switch to RX freq reg A
movwf Txddato
call TxD
movlw 17h ; write ANALOG reg
movwf Txddir
movlw 0x47 ; Rx analog
movwf Txddato
call TxD
; control lock status (status 0x10)
lockrx movlw .127
movwf cont127
x4 movlw .255
movwf cont255
call pollstatus ; read STATUS reg
btfsc Rxddato,4 ; search for lock bit
goto CSon
decfsz cont255 ;Timeout
goto $-4
decfsz cont127
goto x4
CSon ; movlw 0x01 ;write INTERFACE reg
; movwf Txddir
; movlw 0x1c ;DCLK carrier sense on
; movwf Txddato
; call TxD
clrf Txddir ; write MAIN reg
movlw 0x01 ; switch the RX part on
movwf Txddato
call TxD
return
;puts cc1020 in TX mode (use wake up to tx first if coming from PD mode)
setup_tx
bsf STATUS,RP0
bcf TRISB,7 ;setup (DIO) as output
bcf STATUS,RP0
movlw 0x1C ;write PA_POWER reg
movwf Txddir
clrf Txddato ;PA off
call TxD
movlw 17h ; write ANALOG reg
movwf Txddir
movlw 0x47 ; Rx analog
movwf Txddato
call TxD
clrf Txddir ;write MAIN reg
movlw 0xc1 ; switch to TX freq reg B
movwf Txddato
call TxD
; control lock status (status 0x10)
locktx movlw .127
movwf cont127
xxxx movlw .255
movwf cont255
call pollstatus ; read status register
btfsc Rxddato,4 ; search for LOCK bit
goto PAon2
decfsz cont255 ;Timeout
goto $-4
decfsz cont127
goto xxxx
PAon2 btfss poweroutput
goto highpow
movlw 0x1C ; write the PA_POWER reg
movwf Txddir
movlw lowpower
movwf Txddato ;restore PA POWER
call TxD
goto out
highpow movlw 0x1C ; write the PA_POWER reg
movwf Txddir
movlw highpower
movwf Txddato ;restore PA POWER
call TxD
out movlw 0x01 ;write the INTERFACE reg
movwf Txddir
movlw 0x0c ;DCLK carrier sense off
movwf Txddato
call TxD
return
power_down ;call DEMORA2 ;delay 2ms
call DEMORA2
clrf Txddir ;
movlw 0x1F ;power down write MAIN reg
movwf Txddato
call TxD
movlw 0x1C ;write PA_POWER reg
movwf Txddir
clrf Txddato ;turn off PA POWER
call TxD
return
;Delay 2ms or 150us
demora150us movlw .20 ;150us delay
movwf PDel0
goto PLoop0
DEMORA2 movlw .249 ; 2ms delay
movwf PDel0 ;
PLoop0 clrwdt ;
PDelL1 goto PDelL2 ;
PDelL2 goto PDelL3 ;
PDelL3 decfsz PDel0, 1 ;
goto PLoop0 ;
PDelL4 goto PDelL5 ;
PDelL5 clrwdt ;
return ;
;Universal Delay routine
delay500 movlw .100
movwf delaycont
goto loopint2
delay65 movlw .13
movwf delaycont ;65ms
goto loopint2
delay30 movlw .6
movwf delaycont; 30ms
goto loopint2
delay15 movlw .3
movwf delaycont
loopint2 movlw 0xEC
movwf TMR1H
movlw 78h
movwf TMR1L
bcf PIR1,0 ;init TMR1
loopint1 clrwdt
btfss PIR1,0 ;flag?
goto loopint1 ;still no, come back
decfsz delaycont,1 ;dec counter
goto loopint2 ;back
return ;end of delay
turnon
bsf T2CON,2 ;T2 on
movlw .3
movwf cont1
lupy movlw 0x0c
movwf CCP1CON
bsf LEDV
call delay30
bcf LEDV
clrf CCP1CON
call delay65
call delay65
decfsz cont1,1
goto lupy
bcf T2CON,2
return
interrupt btfss INTCON,TMR0IF
goto testpow
bcf INTCON,TMR0IF ;interrupts every 33ms
movlw .0
movwf TMR0 ;initial TMR0 value
;Timer 500ms
bcf T2CON,2
clrf CCP1CON
base500 incf itemp,1 ;count to 15x33ms
movlw .15
xorwf itemp,w ;equal 500ms
btfss STATUS,Z
goto salida
clrf itemp
bsf timeoutRX
btfss alarmflag ;test if there is alarm flag
goto base40s
bsf T2CON,2
movlw 0x0c
movwf CCP1CON
base40s incf itemp2,1
movlw .60 ; 60x500ms= 30 segundos
xorwf itemp2,w
btfss STATUS,Z
goto salida
clrf itemp2
bsf alarmflag
goto salida
testpow btfss INTCON,INTF ;testeamos si todavía hay peticón de apagado
goto salida ;no, falsa alarma, salir
batbaja bcf INTCON,INTF
bsf T2CON,2 ;T2 en ON
movlw 0x0C ;activar alarma apagado
movwf CCP1CON
call delay500 ;demora 1 seg antes de apagar el equipo
call delay500
off bcf T2CON,2 ;T2 en OFF
clrf CCP1CON ;apgamos alarma apagado
bcf PWM
call delay500
goto 00h
salida bcf INTCON,INTF ;limpiamos flag interrupción
retfie ;y volvemos
delay7s call delay500
call delay500
call delay500
call delay500
call delay500
call delay500
call delay500
call delay500
call delay500
call delay500
call delay500
call delay500
call delay500
call delay500
call delay500
call delay500
call delay500
return
apagado_remoto
goto 00h
bateriabaja
bcf INTCON,GIE
bsf T2CON,2 ;T2 en ON
movlw 0x0C ;activar alarma apagado
movwf CCP1CON
call delay500 ;demora 1 seg antes de apagar el equipo
call delay500
bcf T2CON,2 ;T2 en OFF
clrf CCP1CON ;apgamos alarma apagado
bcf PWM
goto 00h
END