I am trying to write to the eeprom in a 16F876, but although using the procedure detailed in the datasheet (DS30292C page 43) my routine will not write. I am attempting to have a 10 bit number in two adjacent locations, and have default values specified in my source code, which get written by the programmer, and I can read ok. However any attempt at writing to these locations fails in my code, and seems to set the corresponding locations to all ones. Source code below for the write routine, using Olin's macros ;* Subroutine "a2d_write_default" ;* Write a default value into the correct EEPROM location for ;* the a2d channel and upper or lower limit selected. ;* ;* Inputs ;* Reg1 - 0 for low limit, non-zero for high limit value ;* Req3(high byte)/Reg2(low byte) - value to save ;* Reg4 - a2d channel address (becomes eeprom offset) ;* ;* Outputs ;* Reg1 - 0 good save, non-zero save failed or invalid channel ;* Req4 - corrupted a2d_write ; start by ensuring a write is not in progress dbankif EECON1 a2d_write_check btfsc EECON1,WR goto a2d_write_check ; get channel address *4 into REG4 ; This will overflow if warning above is not heeded bcf status,c ; clear carry before rotate rlf Reg4,F ; move up by three bits (*2) rlf Reg4,F ; (*4 ready here for eeprom access) dbankif EEADR getf Reg4 ; mov offset into EEADR movwf EEADR movlw a2d0_high ; now allow for address to be adjusted testfz Reg1 ; check to see which it is skip_z addlw 2 ; adjust address for low limit to be set addwf EEADR,F dbankif EEADRH clrf EEADRH ; clear high address for safety. dbankif EEDATA getf Reg3 ; load high byte movwf EEDATA ; into the data register dbankif EECON1 clrf EECON1 bsf EECON1,WREN ; allow eeprom write intr_off ; disallow interrupts through critical portion movlw 0x55 ; see data sheet for machinations performed here movwf EECON2 movlw 0xAA movwf EECON2 bsf EECON1,WR intr_on ; and allow interrupts again a2d_wr_wait1 btfsc EECON1,WR ; allow write to complete goto a2d_wr_wait1 bcf EECON1,WREN ; remove write enable to stop corruption dbankif EEADR incf EEADR,F ; now write other half dbankif EEDATA getf Reg2 movwf EEDATA dbankif EECON1 clrf EECON1 bsf EECON1,WREN ; allow eeprom write dbankif EECON2 intr_off ; disallow interrupts through critical portion movlw 0x55 ; see data sheet for machinations performed here movwf EECON2 movlw 0xAA movwf EECON2 bsf EECON1,WR intr_on ; and allow interrupts again a2d_wr_wait2 btfsc EECON1,WR ; allow write to complete goto a2d_wr_wait2 bcf EECON1,WREN ; remove write enable to stop corruption ; Now we check that the write happened OK. ; first check that the second byte wrote clrf EECON1 bsf EECON1,RD ; start read operation dbankif EEDATA getf EEDATA ; get the byte subwf Reg2,W ; compare with what should have been written movwf Reg0 ; and save result as a flag dbankif EEADR decf EEADR,F ; now do other byte dbankif EECON1 clrf EECON1 bsf EECON1,RD ; start the read dbankif EEDATA getf EEDATA ; get this byte subwf Reg3,W ; compare with what should have been written iorwf Reg0,F ; and OR it with previous result a2d_wr_exit ; and exit -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads