Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Reading from EEPROM not working

Status
Not open for further replies.

Suraj143

Active Member
Hi Guys I have write to eeprom but when I power up it wont starts from last saved value its starting from "0000".I cannot solve the problem please take a look.
Thanks

Code:
		list		p=16f628a
		#include	<P16F628A.inc>
		errorlevel	-302
		__config	0x3D18
		

D0		equ		21h
D1		equ		22h
D10		equ		23h
D100		equ		24h
SCcount 	equ		25h				
Eadd		equ		70h	;common to all four banks
Edat		equ		71h	;common to all four banks	
		
		org	0x00
		movlw	0x07
		movwf	CMCON		
		goto	Main
	
;**********************************************
;multiplexing & increment stuff in the four SSD
;**********************************************

Scan		----
		----
		return


Incr		----
		----
		return	

;**********************
;eeprom write routine
;**********************	
	
Ewrite		bsf	STATUS,RP1	;B1	
		movf	Edat,W
		movwf	EEDATA
		movf	Eadd,W
		movwf	EEADR				
		bsf	EECON1,WREN
		movlw	55h
		movwf	EECON2
		movlw	0AAh
		movwf	EECON2
		bsf	EECON1,WR
		btfsc	EECON1,WR
		goto	$-1
		clrf	STATUS		;B0
		return
		
;**********************
;eeprom read routine
;**********************	

EEread		bsf	STATUS,RP1	;B1	
		movwf	EEADR		
		bsf	EECON1,RD
		btfsc	EECON1,RD
		goto	$-1	
		movf	EEDATA,W
		clrf	STATUS		;B0
		return

;******************************
;main program startd from here
;******************************
						
Main		bsf	STATUS,RP0	;B1
		movlw	01h		;make RB0 input button
		movwf	TRISB	
		clrf	TRISA
		bcf	OPTION_REG,NOT_RBPU
		bcf	STATUS,RP0	;B0		
		clrf	Eadd
		clrf	Edat		
		clrf	D0
		clrf	D1
		clrf	D10
		clrf	D100
	
;*************************************************************
;read the saved value first & put into four digits in the SSD
;*************************************************************
	
		clrw		
		call	EEread
		movwf	D0
		movlw	01h		
		call	EEread
		movwf	D1
		movlw	02h		
		call	EEread
		movwf	D10
		movlw	03h	
		call	EEread
		movwf	D100

;****************************
;has the save button pressed?
;****************************
				
Main1		movlw	.100		;100 scans
		movwf	SCcount
		call	Scan
		decfsz	SCcount,f
		goto	$-2
		call	Incr			
		btfsc	PORTB,0		;check the save button
		goto	Main1

;****************************************
;yes then write the four digits to EEPROM
;****************************************	

		clrw
		movwf	Eadd
		movf	D0,W
		movwf	Edat
		call	Ewrite		
		movlw	01h
		movwf	Eadd
		movf	D1,W
		movwf	Edat
		call	Ewrite			
		movlw	02h
		movwf	Eadd
		movf	D10,W
		movwf	Edat
		call	Ewrite			
		movlw	03h
		movwf	Eadd
		movf	D100,W
		movwf	Edat
		call	Ewrite	
		call	Scan
		btfss	PORTB,0
		goto	$-2
		goto	Main1			
		
		end
 
Hi,
Does it start at 0000?
Your 7-segment displays show the numbers of D0, D1, D10 and D100 am I right?
In the beginning, the value of the eeprom is not set, and you read from it. The value in the eeprom is unknown, according to the datasheet (POR reset). I've tried and it is 0xff.
In my program, I first check the content of the EEPROM. If it is not 0xff, read from it. While if it is 0xff, write into it.
POR is disabled in your program, so the initial value in the EEPROM is unchanged, but what is 'unchanged' if you never write anything into it in the beginning?
 
Hi banansiong
bananasiong said:
Hi,
Does it start at 0000?
Yes.
Your 7-segment displays show the numbers of D0, D1, D10 and D100 am I right?
Yes
In the beginning, the value of the eeprom is not set, and you read from it. The value in the eeprom is unknown, according to the datasheet (POR reset). I've tried and it is 0xff.
In my program, I first check the content of the EEPROM. If it is not 0xff, read from it. While if it is 0xff, write into it.
POR is disabled in your program, so the initial value in the EEPROM is unchanged, but what is 'unchanged' if you never write anything into it in the beginning?

So what I have to do now?
 
What do you want your program to do? Save the data into the EEPROM when the button is pressed?
But in your program, I can see that the same data is written in and read from the EEPROM, no increment or any changes. Or you didn't show it here?
I don't know whether there is any other method out there, maybe you can try mine.
First check whether the first memory location of the EEPROM whether is 0xff or not. If it is not 0xff, read from it.
Then when the button is pressed, write into it and continue counting or whatever.
When the next time you turn on the PIC, the value you got will be the last saved data.

Or there is any other way to do that?
 
You could write values in the source code.

Code:
		org	0x2100
		Data	1,2,3,4,5,6

The area at 2100 is where you put data that you want in EEPROM.

Mike.
 
Oh I see I really missed that part it should really work.

Then those are the data. Can I put any values to that? So when I save the data through my button
this data will be ovewrite.I have only four digits so I need to write only four data in the ORG 2100 location If I have more data to write I need to fill up with more values in the ORG 2100.

Thanks a lot mike.
 
Last edited:
Hi Pommie,
How do you know that the location of EEPROM is at 0x2100? The datasheet of PIC16F628A shows only the PROGRAM MEMORY MAP AND STACK which is not more than 0x2000, never mention what is more than this location. I've tried reading PIC16F627A/628A/648A EEPROM Memory Programming Specification too, the note shows 'not implemented for the memory location after 0x2008. Or I've missed out any other application notes?
 
No, different PICs have different EEPROM addresses. The code templates included with MPLAB at;
C:\Program Files\Microchip\MPASM Suite\Template\Code
usually have these values already filled in for you as an example. Or you can browse the data sheet for the device in question.
 
:eek:
I didn't know that the EEPROM can be written in this way. Usually I do according to the datasheet.
So I can straight away write into the EEPROM without using 'EEPROM write routine', i.e. 0x55 and 0xaa to EECON2? Or this is just assigning the initial value to the EEPROM?
 
It is just for the initial values for the EEPROM when the device is programmed. You still need to write the 0x55 and 0xAA bytes etc from within your code to modify eeprom.
 
I see some small mistakes in OP coding.Edited ones highlighted in red.Try this.

Code:
		list		p=16f628a
		#include	<P16F628A.inc>
		errorlevel	-302
		__config	0x3D18
		

D0		equ		21h
D1		equ		22h
D10		equ		23h
D100		equ		24h
SCcount 	equ		25h				
Eadd		equ		70h	;common to all four banks
Edat		equ		71h	;common to all four banks	
		
		org	0x00
		movlw	0x07
		movwf	CMCON		
		goto	Main
	
;**********************************************
;multiplexing & increment stuff in the four SSD
;**********************************************

Scan		----
		----
		return


Incr		----
		----
		return	

;**********************
;eeprom write routine
;**********************	
	
Ewrite		[COLOR="Red"][B]bsf	STATUS,RP0	;B1[/B][/COLOR]	
		movf	Edat,W
		movwf	EEDATA
		movf	Eadd,W
		movwf	EEADR				
		bsf	EECON1,WREN
		movlw	55h
		movwf	EECON2
		movlw	0AAh
		movwf	EECON2
		bsf	EECON1,WR
		btfsc	EECON1,WR
		goto	$-1
		clrf	STATUS		;B0
		return
		
;**********************
;eeprom read routine
;**********************	

EEread		[B][COLOR="Red"]bsf	STATUS,RP0	;B1	[/COLOR][/B]
		movwf	EEADR		
		bsf	EECON1,RD
		btfsc	EECON1,RD
		goto	$-1	
		movf	EEDATA,W
		clrf	STATUS		;B0
		return

;******************************
;main program startd from here
;******************************
						
Main		bsf	STATUS,RP0	;B1
		movlw	01h		;make RB0 input button
		movwf	TRISB	
		clrf	TRISA
		bcf	OPTION_REG,NOT_RBPU
		bcf	STATUS,RP0	;B0		
		clrf	Eadd
		clrf	Edat		
		clrf	D0
		clrf	D1
		clrf	D10
		clrf	D100
	
;*************************************************************
;read the saved value first & put into four digits in the SSD
;*************************************************************
	
		clrw		
		call	EEread
		movwf	D0
		movlw	01h		
		call	EEread
		movwf	D1
		movlw	02h		
		call	EEread
		movwf	D10
		movlw	03h	
		call	EEread
		movwf	D100

;****************************
;has the save button pressed?
;****************************
				
Main1		movlw	.100		;100 scans
		movwf	SCcount
		call	Scan
		decfsz	SCcount,f
		goto	$-2
		call	Incr			
		btfsc	PORTB,0		;check the save button
		goto	Main1

;****************************************
;yes then write the four digits to EEPROM
;****************************************	

		clrw
		movwf	Eadd
		movf	D0,W
		movwf	Edat
		call	Ewrite		
		movlw	01h
		movwf	Eadd
		movf	D1,W
		movwf	Edat
		call	Ewrite			
		movlw	02h
		movwf	Eadd
		movf	D10,W
		movwf	Edat
		call	Ewrite			
		movlw	03h
		movwf	Eadd
		movf	D100,W
		movwf	Edat
		call	Ewrite	
		call	Scan
		btfss	PORTB,0
		goto	$-2
		goto	Main1	

		[COLOR="Red"][B]org	2100h
		data	.1
		data	.2
		data	.3
		data	.4	[/B][/COLOR]	
		
		end
 
bananasiong said:
:eek:
I didn't know that the EEPROM can be written in this way. Usually I do according to the datasheet.
So I can straight away write into the EEPROM without using 'EEPROM write routine', i.e. 0x55 and 0xaa to EECON2? Or this is just assigning the initial value to the EEPROM?

Hi bananasiong without writing the org 0x2100 did your eeprom code work well?

I think its there to store data. When new data comes the previous values will overwrite.

Earlier I tried without it but it doesn’t work. After I applied org 0x2100. & also after changing bank bits it really worked well.
 
Suraj143 said:
Hi bananasiong without writing the org 0x2100 did your eeprom code work well?

I think its there to store data. When new data comes the previous values will overwrite.

Earlier I tried without it but it doesn’t work. After I applied org 0x2100. & also after changing bank bits it really worked well.
Yes, it worked well.
As kchriste said, the initial value of EEPROM can be written at 0x2100. For further modifying the data EEPROM, the EEPROM write routine as suggested in the datasheet is required.

In my program, I didn't have any initial value in the EEPROM, I read the first address of EEPROM. If there is nothing (0xff), the program will write data into it; or else the program will take data from the EEPROM.
 
Oh I see my only problem is, if do not have any initial value do I still have to add the org 0x2100 codings? Earlier without that it didn’t work.
 
In your program, you first get data from EEPROM. Without any initial value in it, what will you get?
Are you doing something like this: Save the present value of the 7-segment displays into the EEPROM when the button is pressed? Then the 7-segment displays will display the last saved values when the device is turned off and on again.

Or you just want to store the initial values of the 7-segment displays?
 
bananasiong said:
In your program, you first get data from EEPROM. Without any initial value in it, what will you get?
org 2100h contains 1,2,3,4 values so I get this values when I power up.

Are you doing something like this: Save the present value of the 7-segment displays into the EEPROM when the button is pressed? Then the 7-segment displays will display the last saved values when the device is turned off and on again.
Exactly what I'm doing.So my previous 1,2,3,4 values will be overwrite when I press the save button after & turn off & off.

Or you just want to store the initial values of the 7-segment displays?
For this no need to write to EEPROM I can load the GP registers which has already loaded the initial values.
 
Yes, so you're doing what I've done. I didn't get the values from the EEPROM, but from the general purpose register for the first time turn on. Then second time onwards turning it on, it reads from the EEPROM for the last saved data.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top