ASM: EEPROM always reads 0xFF

Go To Last Post
15 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi again!

I tried reading stored values from EEPROM and putting them into SRAM on my ATmega16. SRAM isn't a problem, but EEPROM is. I followed a tutorial about reading from EEPROM stored values and got this:

ldi acc, low(EaddrL); get address for reading
ldi acc2, high(EaddrL)
out EEARL, acc		; set address for reading
out EEARH, acc2
sbi EECR, EERE		; activate reading
in acc, EEDR		; get result
sts addrL, acc		; store to SRAM
; (marked position)

.dseg
addrL: .byte 1

.eseg
EaddrL:.db 0x00

When testing in AVR Studio 4.13, the line

in acc, EEDR

always reads 0xFF while it should read 0x00 which is stored in EEPROM.

The funny thing: they (the tutorial) are talking about a loop that waits for data being read from EEPROM, but added a simple

loop:rjmp loop

at the marked position. This can't be right because the line reading EEDR isn't being recalled and the loop is endless, i.e. the program will never get out of that loop again. Their program has an out to a port instead of the sts I have put there...

In the tutorials section here, I only found C-Tutorials related to EEPROM and a search for EEPROM in this forum returned about 500 results, the first page didn't do any good.

The ATmega16 datasheet has almost the same example except it uses sbic instead of sbi. But as this is the only EEPROM access (yet), it should be working.

So how do you suggest creating that loop so it actually does wait and continue after data is loaded?

Thanks for any advice or comment!

Markus

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

To read several EEPROM bytes in a row you need a loop like this

sram_init:
	ldi	zh,0x01		;msb SRAM
	ldi	zl,0x00		;lsb SRAM
	ldi	xh,0x00		;msb EEPROM
	ldi	xl,0x00		;lsb EEPROM
read_prom:
	rcall	prom_read		;read EEPROM
	st	z+,data		;save data to SRAM
	adiw	xl,1			;inc EEPROM adress
	cpi	xl,0xcc		;until beyond last lsb
	brne	read_prom		;read EEPROM
	cpi	xh,0x01		;also if EEPROM msb = 0x00	;
	brlo	read_prom		;read EEPROM
        ;All done when you get here

prom_read:
	sbic	eecr,eewe		;wait EEPROM
	rjmp	prom_read		;not busy
	out	eearh,xh		;load msb EEPROM
	out	eearl,xl		;load lsb EEPROM
	sbi	eecr,eere		;enable EEPROM read
	in	data,eedr		;collect EEPROM data
	ret				;return

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your loop waits for the eeprom WRITE to clear, but it's supposed to be reading. Typo or does it have to be this way?

Markus

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Interesting question... :wink:
From m16 datasheet p.20

Quote:
The user should poll the EEWE bit before starting the read operation. If a write operation
is in progress, it is neither possible to read the EEPROM, nor to change the EEAR
Register.

Also the EEPROM read example on p.22 poll EEWE before reading.
I have used this scheme succesfully to move a large number of bytes from EEPROM to SRAM and never had any problem.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Also, from m16 datasheet p.20

Quote:
The EEPROM Read Enable Signal – EERE – is the read strobe to the EEPROM. When
the correct address is set up in the EEAR Register, the EERE bit must be written to a
logic one to trigger the EEPROM read. The EEPROM read access takes one instruction,
and the requested data is available immediately. When the EEPROM is read, the CPU
is halted for four cycles before the next instruction is executed.

If my brain still works it does not seem possible to start a new read while a read is in progress since data is available after one clock cycle and CPU halts for 4 cycles after this. Maybe polling EEWE can be omitted but I'd rather follow datasheet recommendations...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Have you loaded the contents of the EEPROM from the generated .eep file? How did you WRITE the data to EEPROM in the first place? I rarely use Studio so I'm not sure what option to use to load the .eep file.

Randy

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Like Randy says, have you >>really downloaded<< .eep file from AVR Studio. You need to either add this option to the "Auto" function or explicitly download .eep file using "Write EEPROM".
Have you tried to read EEPROM after download to reassure that it contains what you believe?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just simulated the program (for now), I'll build the module on a breadboard and test it in the real µC once the simulator runs it smoothly.

The eep-file is loaded automatically by the simulator. (there is no option to manually load it, so...)

The data is written to EEPROM with these instructions:

.eseg
EaddrL:.db 0x00

(an eep file is created while compiling)

Markus

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I can't test in real surrounding yet because of the other (interrupts repeatedly firing) problem. I'd have tried it with the µC if it was possible yet :-(

Markus

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Was not aware that all this was done in "virtual reality".
My advice is to not trust simulator, especially doing ISR stuff...
BTW, turn off external interrupts, do the testing.
You should still be able to read EEPROM on real chip once you have done a >>real download<< of .eep file.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Remeber that when using the simulator the debug menu does have an "upload/download memory" option and one of the memories you can read froma .hex/.eep file is the contents of the simulated EEPROM

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The problem was the simulator. It does run on the µC but behaves strange in the simulator. Again something learned: never ever rely on a simulator, even not if the company that built the µC gives it to you.

Seems like the AVR Studio assembler simulator has some strange problems...

Thanks for all your efforts!

Markus

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I still think the .eep file wasn't loaded. I may be wrong, but I've always had to manually load it. I didn't know you could set it to automatically load.

Randy

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What Randy said. AVRStudio will happily do EEPROM writes & reads. But you DO have to do the up/download memories step. [I can't remember whether you need to do it every rebuild or not--IIRC yes.]

But the other notes about "proper" sequences with checking for write-in-progress, and interrupt-protection, apply for the "real" app.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have not used the option to autoprogram via Studio 4 but if you click
"Tools" -> "Program AVR" -> "Connect" -> "Disconnected mode" -> "Auto"
you should be able to select what will happen when you choose
"Tools" -> "Program AVR -> "Start Auto" once you have connected to chip.