where : ibrtses embedded

AVR EEPROM as datastorage

Some projects require data as configuration information be saved in the EEPROM.
This approach consists of

Access the EEPROM

config.asm
;
; reads and writes the configuration 
;  to/from the EEPROM
;
; polled version - read p47 on EEWE about interrupts
;
;
;
;EEPROM(Z^) to RAM(X^), length:=U1  
MoveETR:
eew2:	mov	temp,ZL
	out	EEARL,temp
	mov	temp,ZH
	out	EEARH,temp
	sbi	EECR,EERE	; read enable
_eee1:	in	temp,EECR
	andi	temp,EERE
	tst	temp
	brne	_eee1
	in	temp,EEDR
	nop
	st	X+,temp
	dec	U1
	breq	eee1
	ld	temp,Z+		; inc Z
	rjmp	eew2
eee1:	ret
;
;
;RAM(X^) to EEPROM(Z^) length:=U1  
MoveRTE:
	cli
eew5:	sbic	EECR,EEWE
	rjmp	eew5
eew4:	mov	temp,ZL
	out	EEARL,temp
	mov	temp,ZH
	out	EEARH,temp
	sbi	EECR,EERE	; read enable
_eee2:	in	temp,EECR
	andi	temp,EERE
	tst	temp
	brne	_eee2
	in	temp,EEDR
	nop
	ld	U2,X+
	cp	temp,U2
	breq	een1
; not equal - program
	out	EEDR,U2
	
;	cli
	sbi	EECR,EEMWE
	sbi	EECR,EEWE
;	sei
;eew5:	sbic	EECR,EEWE
;	rjmp	eew5
een1:	dec	U1
	breq	eee2
	ld	temp,Z+
	rjmp	eew5 	; war eew4
eee2:	sei
	ret


; BYTE(X^) to EEPROM(Z^)
;ByteToEEPROM:
	push	U2
	mov	temp,ZL
	out	EEARL,temp
	mov	temp,ZH
	out	EEARH,temp
	sbi	EECR,EERE	; read enable
	in	temp,EEDR
	nop
	ld	U2,X
	cp	temp,U2
	breq	b2e1
; not equal - program
	out	EEDR,U2
	cli
	sbi	EECR,EEMWE
	sbi	EECR,EEWE
	sei
b2e2:	sbic	EECR,EEWE
	rjmp	b2e2
b2e1:	pop	U2
	ret


; WORD(X^) to EEPROM(Z^)
;WordToEEPROM:
	push	U1
	push	U2
	ldi	U1,2
w2e4:	mov	temp,ZL
	out	EEARL,temp
	mov	temp,ZH
	out	EEARH,temp
	sbi	EECR,EERE	; read enable
	in	temp,EEDR
	nop
	ld	U2,X+
	cp	temp,U2
	breq	w2e1
; not equal - program
	out	EEDR,U2
	cli
	sbi	EECR,EEMWE
	sbi	EECR,EEWE
	sei
w2e2:	sbic	EECR,EEWE
	rjmp	w2e2
w2e1:	dec	U1
	breq	w2e3
	ld	temp,Z+
	rjmp	w2e4
w2e3:	pop	u2
	pop	u1
	ret


;EEPROM(Z^) to RAM(X^)
EEPROMtoByte:
	cli
	mov	temp,ZL
	out	EEARL,temp
	mov	temp,ZH
	out	EEARH,temp
	sbi	EECR,EERE	; read enable
_eee:	in	temp,EECR
	nop
	andi	temp,EERE
	tst	temp
	brne	_eee
	in	temp,EEDR
	nop
	st	X,temp
	sei
	ret


;EEPROM(Z^) to RAM(X^)
;EEPROMtoWord:
e2w2:	push	u1
	ldi	u1,2
	mov	temp,ZL
	out	EEARL,temp
	mov	temp,ZH
	out	EEARH,temp
	sbi	EECR,EERE	; read enable
	in	temp,EEDR
	nop
	st	X+,temp
	dec	U1
	breq	e2w1
	ld	temp,Z+		; inc Z
	rjmp	e2w2
e2w1:	pop	u1
	ret

;string from EEPROM(Z^) to RAM(X^)
EStr2RAM:
	pushX
	push	u1
	push	temp
	rcall	EEPROMtoByte
	ld	u1,X
_es2r:	tst	u1
	breq	_es2re	
	ld	temp,X+		; dummy for inc
	ld	temp,Z+		; dummy for inc
	rcall	EEPROMtoByte
	dec	u1
	rjmp	_es2r
_es2re:
	pop	temp
	pop	u1
	popX
	ret

Definitions

in main.asm :

.equ CurrentConfigSize  =2	; size of configuration
.equ defaultID		=0x01	;
.equ defaultmode	=0	;

.DSEG

; start EEPROM buffer
ConfigSize:	.byte 1		; size not including this
DeviceID:	.byte 1
Mode:		.byte 1

; end EEPROM buffer


.ESEG		; little endian : low byte on low adress
.DB	CurrentConfigSize,defaultID,defaultmode

.CSEG

.include "CONFIG.ASM"	; EEPROM stuff

.MACRO	SaveVar(VAR,SIZE)
	push	ZL
	push	ZH
	push	XL
	push	XH
	push	U1
	push	U2
	ldi	ZL,low(@0-ConfigSize)
	ldi	ZH,high(@0-ConfigSize)
	ldi	XL,low(@0)
	ldi	XH,high(@0)
	ldi	U1,@1
	rcall	MoveRTE
	pop	U2
	pop	U1
	pop	XH
	pop	XL
	pop	ZH
	pop	ZL
.ENDMACRO

the code

; initialization section 

;init vars
;  set config size
	ldi	temp,CurrentConfigSize
	setYPtr	ConfigSize
	st	Y,temp
; set ID
	ldi	temp,defaultID
	SetYPtr	DeviceID
	st	Y,temp
; set mode
	ldi	temp,defaultmode
	SetYPtr	Mode
	st	Y,temp
..

; get config from EEPROM
	ldi	ZL,0x0
	ldi	ZH,0x0
	SetXPtr	ConfigSize
	ld	U1,X
	inc	u1
	rcall	MoveETR


running the loop

	ldd	temp,Z+5	; eg....
	SetYPtr	Mode
	st	Y,temp		; move data to RAM
	SaveVar	Mode,1		; move data to EEPROM


Questions ?
Suggestions?
Feedback ?






disclaimer



sponsored links




AVR index
embedded software pages
home

last updated: 10.dec.00



Copyright (99,2001) Ing.Büro R.Tschaggelar