#include <p16F690.inc>
__config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
cblock 0x20
Delay1 ; Assign an address to label Delay1
Delay2
OnDelayLoop
OffDelayLoop
WRITE_ERR
WRITE_OK
Dcycle_add
Dcycle_data
endc
org 0
Start
bsf STATUS, RP0 ; select Register Page 1
movlw 0xFF
movwf TRISA ; Make PortA all input
movlw 0x00
movwf TRISC
bcf STATUS, RP0 ; back to Register Page 0
clrf PORTC
MainLoop
movlw 0x10
movwf Dcycle_add
movlw 0x0A
movwf Dcycle_data
WRITE
BANKSEL EEADR ; [color="RED"]Switch to Bank 2[/color]
MOVF Dcycle_add, W; [color="RED"]But DCycle_add is in Bank 0[/color]
MOVWF EEADR ;Data Memory Address to write
MOVF Dcycle_data, W; [color="RED"]Same error again[/color]
MOVWF EEDAT ;Data Memory Value to write
BANKSEL EECON1 ;
BCF EECON1, EEPGD ;Point to DATA memory
BSF EECON1, WREN ;Enable writes
BCF INTCON, GIE ;Disable INTs.
BTFSC INTCON, GIE ;SEE AN576 [color="RED"]???[/color]
GOTO $-2
MOVLW 0x55 ;
MOVWF EECON2 ;Write 55h
MOVLW 0xAA ;
MOVWF EECON2 ;Write AAh
BSF EECON1, WR ;Set WR bit to begin write
BSF INTCON, GIE ;Enable INTs.
;SLEEP ;Wait for interrupt to signal write complete (optional)
BCF EECON1, WREN ;Disable writes
[color="RED"];You need to poll for PIR2<EEIF> to become set indicating the write is complete, before continueing...[/color]
[color="RED"];...Then clear PIR2<EEIF> yourself[/color]
BANKSEL 0x00 ;Bank 0 [color="RED"]questionable use of banksel, better use a variable name... Furthermore...[/color]
;verify
BANKSEL EEDAT ; [color="RED"]...2 banksel's in a row is useless[/color]
MOVF EEDAT, W ;EEDAT not changed [color="RED"]I can't see the 16f690 datasheet specifically say EEDAT is unchanged after a write - Better not to assume things[/color]
[color="RED"]; You need to reload EEADR - Datasheet clearly states EEADR contents are unreliable after a write operation[/color]
BANKSEL EECON1 ;
[color="RED"]; BCF EECON1, EEPGD ? [/color]
BSF EECON1, RD ;YES, Read the
;value written
BANKSEL EEDAT ;
XORWF EEDAT, W ;
BTFSS STATUS, Z ;Is data the same
goto $ +4
movlw 0x02
movwf PORTC
goto $-2
BANKSEL 0x00 ;Bank 0 [color="RED"]Same as before, use a label[/color]
READ
BANKSEL EEADR ; [color="RED"]Bank 2...[/color]
MOVF Dcycle_add, W; [color="RED"]Dcycle_add is in Bank 0[/color]
MOVWF EEADR ;Data Memory
;Address to read
BANKSEL EECON1 ;
BCF EECON1, EEPGD ;Point to DATA memory
BSF EECON1, RD ;EE Read
BANKSEL EEDAT ;
MOVF EEDAT, W ;W = EEDAT
BCF STATUS, RP1 ;Bank 0
movwf PORTC
btfsc PORTA,3
goto $ -1
clrf PORTC
movlw 0x05
movwf PORTC
btfss PORTA,3
goto $ -1
goto READ
end