ON 20020414@2:10:20 PM at page: http://www.piclist.org/techref/mem/eeproms.htm CM-mcmanis-J95 Chuck McManis added 'Code: This code will write the EEPROM on the 16F628 in chunks. ; ; Written: 14-Apr-2002, Chuck McManis ; ; Uses the temporary variable EE_LEN, EE_SRC, and EE_DST ; During write Source is RAM and Destination is EEPROM ; ; Note: Due to a bug in the 16F628 you can "overrun" the ability ; of the chip to write the EEPROM if you just monitor the WR bit ; in EECON1 (if you write immediately after it goes low the EEPROM ; may not be written. ; ; Thus this macro assumes that when it starts there is not a write ; currently in progress! ; ; Usage: EEWRITE , , ; ; On the 16F628, EE_LEN, EE_SRC, and EE_DST should be in the range ; 0x70 - 0x7F (mirrored on all ram banks) ; ; EEWRITE MACRO SRC_ADDR, DST_ADDR, LEN LOCAL WLOOP MOVLW LEN ; Get length MOVWF EE_LEN ; Store it in count MOVLW SRC_ADDR ; Get the source address MOVWF EE_SRC ; Store in the source addr MOVLW DST_ADDR ; EEPROM Destination address MOVWF EE_DST ; Store it too WLOOP: BCF PIR1, EEIF ; Clear this flag... MOVF EE_SRC,W ; Source Address MOVWF FSR ; Store in FSR MOVF INDF,W ; Get a data byte BSF STATUS, RP0 ; Switch to Bank 1 MOVWF EEDATA ; Store the data byte in EE Data MOVF EE_DST,W ; Get the destination address MOVWF EEADR ; .. this byte goes here BSF EECON1, WREN ; Enable Writes to EEPROM ; BTFSS EECON1, WR ; This should work but doesn't (see ; GOTO $-1 ; below) it only writes 2 of n bytes. BCF INTCON, GIE ; Interrupts off. MOVLW H'55' ; Magic Sequence (Sequence Start) MOVWF EECON2 ; ... MOVLW H'AA' ; ... MOVWF EECON2 ; ... BSF EECON1, WR ; and Write it! (Sequence End) BSF INTCON, GIE ; Interrupts are allowed again. BCF EECON1, WREN ; Disable future writes. BCF STATUS, RP0 ; back to bank 0 BTFSS PIR1, EEIF ; Wait for it to finish, this slows GOTO $-1 ; ... us down! (bug! see above) INCF EE_SRC, F ; Add 1 to source and destination INCF EE_DST, F ; ... addresses (no overlap) DECFSZ EE_LEN,F ; Decrement the length and ... GOTO WLOOP ; ... loop if non-zero. ENDM '553 Null recipient not accepted