Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

16F877A EEPROM Write Problem. Help Please

Status
Not open for further replies.

shimanto

Newbie level 4
Joined
Aug 21, 2009
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Bangladesh
Activity points
1,325
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
         banksel EEADR
         movwf EEADR
         call wrt
         call read
         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
Joined
May 15, 2009
Messages
3,056
Helped
883
Reputation
1,781
Reaction score
736
Trophy points
113
Location
Prime Meridian
Activity points
0
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
Joined
Jul 4, 2009
Messages
14,376
Helped
4,727
Reputation
9,469
Reaction score
4,497
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
122,668
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
Joined
Aug 21, 2009
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Bangladesh
Activity points
1,325
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
Joined
Aug 24, 2009
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,284
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
Joined
Jul 4, 2009
Messages
14,376
Helped
4,727
Reputation
9,469
Reaction score
4,497
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
122,668
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.

For example, your code:
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
    Helpful Answer Positive Rating

shimanto

Newbie level 4
Joined
Aug 21, 2009
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Bangladesh
Activity points
1,325
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

Advanced Member level 2
Joined
Jun 10, 2006
Messages
671
Helped
159
Reputation
316
Reaction score
99
Trophy points
1,308
Location
Bolivia
Activity points
5,913
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
Joined
Aug 21, 2009
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Bangladesh
Activity points
1,325
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.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top