list p=16f628A ; list directive to define processor #include ; processor specific variable definitions errorlevel -302 ;hide banking message __config _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _LVP_OFF ;setup some user test regs test1: equ 0x20 ;eeprom test reg test2: equ 0x21 test3: equ 0x22 ;******* MACRO Defines ********* ;write data to a EEPROM location WRD2_EE macro eedata, eeaddr movlw eedata banksel EECON1 movwf EEDATA movlw eeaddr movwf EEADR call EP_Write endm ;write contents of a register to a EEPROM location WRR2_EE macro eereg, eeaddr movf eereg,W banksel EECON1 movwf EEDATA movlw eeaddr movwf EEADR call EP_Write endm ;read EEProm location and save in regaddr, Bank0 RDD2_EE macro eeaddr, regaddr banksel EECON1 movlw eeaddr movwf EEADR call EP_Read movwf regaddr endm ;********************************************************************** org 0x000 ; processor reset vector goto Start ; go to beginning of program org 0x004 ; interrupt vector location nop retfie ; return from interrupt Start: ;initialise PIC clrf PORTA clrf PORTB movlw 0x07 movwf CMCON ; Turn off comparator bsf STATUS,RP0 ; bank one movlw 0xFF movwf TRISA ; porta all Input movlw 0x00 movwf TRISB ; portb all Output movlw 0x00 ;See datasheet for prefered movwf OPTION_REG ;settings of OPTION_REG bcf STATUS,RP0 ; return to bank 0 Dummy: movlw 0x57 ;load some test data into registers movwf test1 ;user reg movlw 0x12 movwf TMR1L ;PIC reg movlw 0x13 movwf TMR1H ;PIC reg Main1: ;NOTE: set correct BANK to suit register data source, before MACRO command WRD2_EE 0x41,0x00 ;write data to any eeprom address WRD2_EE 0x42, 0x01 WRD2_EE 0x43, 0x02 WRD2_EE 0x44, 0x03 WRD2_EE 0x41,0x00 ;write data to any eeprom address WRD2_EE 0x42,0x01 ;write a reg to a specified address WRR2_EE 0x20,0x40 ;write user reg content to a address in eeprom WRR2_EE TMR1L,0x50 ;write PIC reg content to a address in eeprom WRR2_EE TMR1H,0x60 ;write PIC content to a address in eeprom Loop1: RDD2_EE 0x00, 0x7F ;Read EEP, EEProm addr to register addr ;loop for ever,,, Oshonsoft ONLY goto Loop1 ;enter using a MACRO command, that has data/reg and EEPROM address ;write one Byte to one EEPROM address and then return EP_Write bsf EECON1,WREN ; Enable write ;required write sequence movlw 0x55 movwf EECON2 ; Write 55h movlw 0xAA movwf EECON2 ; Write AAh bsf EECON1,WR ; Set WR bit ;write to EEP bcf STATUS,RP0 ; Bank 0 btfss PIR1,EEIF ; wait for write to complete. goto $-1 bcf PIR1,EEIF ; and clear the 'write complete' flag bsf STATUS,RP0 ; Bank 1 bcf EECON1,WREN ; Disable write bcf STATUS,RP0 ; Bank 0 return ;enter using Macro EP_Read bsf EECON1,RD ; EE Read movf EEDATA,W ; W = EEDATA bcf STATUS,RP0 ; Bank 0 return end