Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 20th August 2007, 02:47 AM   (permalink)
Question Reading from EEPROM not working

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
Suraj143 is offline   Reply With Quote
Old 20th August 2007, 05:03 AM   (permalink)
Default

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?
__________________
Superman returns..
bananasiong is offline   Reply With Quote
Old 20th August 2007, 05:12 AM   (permalink)
Default

Hi banansiong
Quote:
Originally Posted by bananasiong
Hi,
Does it start at 0000?
Yes.
Quote:
Your 7-segment displays show the numbers of D0, D1, D10 and D100 am I right?
Yes
Quote:
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?
Suraj143 is offline   Reply With Quote
Old 20th August 2007, 05:28 AM   (permalink)
Default

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?
__________________
Superman returns..
bananasiong is offline   Reply With Quote
Old 20th August 2007, 06:51 AM   (permalink)
Default

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.
Pommie is offline   Reply With Quote
Old 20th August 2007, 07:22 AM   (permalink)
Default

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 by Suraj143; 20th August 2007 at 11:03 AM.
Suraj143 is offline   Reply With Quote
Old 20th August 2007, 02:45 PM   (permalink)
Default

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?
__________________
Superman returns..
bananasiong is offline   Reply With Quote
Old 20th August 2007, 02:56 PM   (permalink)
Default

It's section 3.9 in the EEPROM Memory Programming Specification.

Mike.
Pommie is offline   Reply With Quote
Old 20th August 2007, 04:30 PM   (permalink)
Default

Quote:
Originally Posted by Pommie
It's section 3.9 in the EEPROM Memory Programming Specification.

Mike.
Hey this is really awesome! I read only the program memory mapping. Seems I didn't read them properly.
Is this the same for other PIC?

Thanks.
__________________
Superman returns..
bananasiong is offline   Reply With Quote
Old 20th August 2007, 11:41 PM   (permalink)
Default

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.
__________________
--- The days of the digital watch are numbered. ---
kchriste is offline   Reply With Quote
Old 21st August 2007, 12:14 AM   (permalink)
Default


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?
__________________
Superman returns..
bananasiong is offline   Reply With Quote
Old 21st August 2007, 12:29 AM   (permalink)
Default

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.
__________________
--- The days of the digital watch are numbered. ---
kchriste is offline   Reply With Quote
Old 21st August 2007, 02:33 AM   (permalink)
Default

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		bsf	STATUS,RP0	;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,RP0	;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	

		org	2100h
		data	.1
		data	.2
		data	.3
		data	.4		
		
		end
__________________
Gayan

My Website
http://gsmicro.blogspot.com/
Gayan Soyza is offline   Reply With Quote
Old 21st August 2007, 04:34 AM   (permalink)
Default

It really worked well.Thanks a lot Gayan. I really didn't notice that bank bits.

Thanks for your help.
Suraj143 is offline   Reply With Quote
Old 21st August 2007, 05:13 AM   (permalink)
Default

Quote:
Originally Posted by bananasiong

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 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Latest
working with eeprom pages? justDIY Micro Controllers 1 7th October 2005 09:21 AM
Writing Data to the EEPROM Electrix Micro Controllers 2 31st August 2005 06:39 AM
faulty digital components testing,checking walters General Electronics Chat 5 22nd August 2005 07:10 PM
sequential read to AT24C01A serial EEPROM giaracam Micro Controllers 2 26th October 2004 03:19 AM
Ext EEPROM read delay? brodin Micro Controllers 2 22nd February 2004 01:43 PM



All times are GMT. The time now is 12:17 AM.


Electronic Circuits  |  Electronics Wiki
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.