# 16F877A EEPROM Write Problem. Help Please

Status
Not open for further replies.

#### shimanto

##### Newbie level 4
bcf status,rp1

Please read the code and tell me where did I make the mistake. Or help me by writing a code for the EEPROM.

Code:
         list    p=16F877a
include "P16F877a.inc"
__CONFIG   0x3E79
cblock 0x20
count
count1
count2
endc

org 0x0005

banksel TRISB
clrf    TRISB
banksel PORTB
clrf    PORTB

movlw 0xAA
banksel EEDATA
movwf EEDATA
call wrt
banksel PORTB
movwf PORTB

wrt      bcf      INTCON,GIE
bsf      STATUS,RP1
bsf      STATUS,RP0

bsf     EECON1,WREN
movlw   0x55
movwf   EECON2
movlw   0xaa
movwf   EECON2
bsf     EECON1,WR

wait4write banksel PIR2
btfss     PIR2,EEIF
goto      wait4write
banksel   EECON1
bcf       EECON1,WREN
banksel PIR2
bcf      PIR2,EEIF
return

delay    movlw   .16
movwf   count2
nn       movlw   .50
movwf   count1
time1    movlw   .250
movwf   count
decfsz   count,f
goto   $-1 decfsz count1,f goto time1 decfsz count2,f goto nn return read banksel EEADR movwf EEADR banksel EECON1 bsf EECON1,RD banksel EEDATA movf EEDATA,w return #### wp100 ##### Advanced Member level 5 16f877a eeprom Hi, Its a while since I coded that chip but the ORG 005 looks wrong to me, try ORG 000 Cannot see an obvious error, but its so easy to get a bank wrong or not to follow Microchips eeprom examples to the letter #### betwixt ##### Super Moderator Staff member eeprom-write Several problems here: 1. you are correct to start your code at address 5 but to get there you need to put a "goto" at the reset address which is 0. 2. never use constructs like this: Code: bcf STATUS,RP1 bsf STATUS,RP0 clrf TRISB use "banksel TRISB" instead. It makes it much easier to follow which register bank you are using. The banksel directive will generate the appropriate RP1 and RP0 settings for the register you name. 3. It makes it much easier to follow the code if you put labels on a line of their own too. For example your code: delay movlw .16 movwf count2 nn movlw .50 movwf count1 time1 movlw .250 is far more readable as: Code: delay movlw .16 movwf count2 nn movlw .50 movwf count1 time1 movlw .250 4. Never use code like "goto$-1". It isn't technically incorrect but it will cause you problems later in life, particularly if the jump is more than one address. Instead place a lablel where you want to jump to and then jump to the label. Doing that will let you insert extras instructions and edit the code without risk of the jump taking you to the wrong location.

Finally,
when posting code on this board, click the box with the word "Code" in it at the top of the entry box. At the end of the code, click it again to return to normal mode. It will prevent the board software removing tabs and formatting.

Brian.

#### shimanto

##### Newbie level 4
banksel produces error when next to label

Thanks Brian.. I have made the necessary corrections that u told about the program. Still the EEPROM is not writing the data. I think its the wait4write loop, may be its becoming an infinite loop. Will anybody check and find the mistake for me plz.

#### Pommie

##### Newbie level 1
16f877a examples

I had a quick look at your code and the bit that stands out is EECON1,EEIF. EEIF is not in EECON1 but in INTCON. Try changing all EECON1,EEIF to INTCON,EEIF.

Mike.

#### betwixt

##### Super Moderator
Staff member
16f877a

Thanks for editing the code tags, it looks much more readable now.

I agree with Pommie, you are looking at the wrong bit to detect the end of the EE write sequence.

I would still advise you to make the other changes I suggested though. It will ultimately produce the same machine instructions but using 'banksel' makes the program far more readable and takes away the risk of setting the RP0 and RP1 bits wrongly.

Code:
         movlw 0xAA
bsf   STATUS,RP1
bcf   STATUS,RP0
movwf EEDATA
bsf   STATUS,RP1
bcf   STATUS,RP0
movwf EEADR

can be rewritten as:
Code:
         movlw 0xAA
banksel EEDATA
movwf EEDATA
movwf EEADR

Brian.

### shimanto

Points: 2

#### shimanto

##### Newbie level 4
movlw 0x55 movwf eecon2 movlw 0xaa movwf eecon2

EEIF is in PIR2 register. I have corrected it and for selecting bank I have used banksel opcode. Its not working now... What should I do?

#### Kurenai_ryu

16f877a eeprom write

could you post your final code? also have you changed the bit? EEIF isn't in INTCON it's in PIE2 register!!! also, I would read the WR bit in EECON1 reg... as noted in the datasheet!!!! why didn't you used it???

#### shimanto

##### Newbie level 4
16f877 eeprom write

@ Kurenai_ryu, I have already edited my post and wrote the final code. EEIF is in the PIR2 register, not in PIE2 register. I have used the WR bit in write sequece, I think u didnt see it. Any way it worked at last.

I have another problem, I have used the ADC of the 16F877A. I want to save the ADC outputs in EEPROM after a short delay. Can anyone give me the program code for this?

Status
Not open for further replies.