AVR internal EEPROM write function is not working properly

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

those are the two functions of read and write

void IEEPROM_Write(u16 A_u16Adress, u8 A_u8Data)
{
    /* Wait for completion of previous write */
    while(GET_BIT(EECR,EECR_EEWE)==1);
    /* Set up address and data registers */
    EEAR = A_u16Adress;
    EEDR = A_u8Data;
    /* Write logical one to EEMWE */
    SET_BIT(EECR,EECR_EEMWE);
    /* Start eeprom write by setting EEWE */
    SET_BIT(EECR,EECR_EEWE);
}

u8 IEEPROM_Read(u16 A_u16Adress)
{
    /* Wait for completion of previous write */
    while(GET_BIT(EECR,EECR_EEWE)==1);
    /* Set up address and data registers */
    EEAR = A_u16Adress;
    /* Start eeprom read by setting EERE */
    SET_BIT(EECR,EECR_EERE);

    return EEDR;
}
////////////EEPROM.h//////////////

#define EEARH (*(volatile u8 *)0x3F)
#define EEARL (*(volatile u8 *)0x3E)
#define EEAR (*(volatile u16 *)0x3E)
#define EECR (*(volatile u8 *)0x3C)
#define EECR_EERE 0
#define EECR_EEWE 1
#define EECR_EEMWE 2
#define EECR_EERIE 3

#define EEDR (*(volatile u8 *)0x3D)
void IEEPROM_Write(u16 A_u16Adress, u8 A_u8Data);
u8 IEEPROM_Read(u16 A_u16Adress);
//////main.c////////////
#include <util/delay.h>

#define F_CPU 8000000UL

void main(void)
{
    // local variable to return data in
    u8 local_u8Data=0;

    // Initialize DIO --> PORTD LCD Data, PORTB LCD Control, PORTC TWI
    MDIO_voidInit();

    // Initialize LCD
    HLCD_voidInit();

    IEEPROM_Write(2, 25);
    local_u8Data = IEEPROM_Read(2);

    if(local_u8Data != 20)
    {
        MDIO_voidSetPinValue(PORTA, 0, PIN_HIGH);
        HLCD_voidSendString("Exp Value: ");
        HLCD_voidDisplayNumber(80);
        HLCD_voidGoToPosition(2, 0);
        HLCD_voidSendString("Ret Value: ");
        HLCD_voidDisplayNumber(local_u8Data);

    }

    while (1)
    {

    }
}

 

I followed the instructions of the ATMEGA32 Datasheet but it the code doesn't work I write a number on a byte then I read it and display it on LCD , but it prints 255,

Embedded software developer

Last Edited: Fri. Sep 23, 2022 - 03:22 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What if you 

return 0x37

 

Your display should show a "7"   (or something else if you are doing a conversion)...then you know your display portion is working 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

avrcandies wrote:

What if you 

return 0x37

 

Your display should show a "7"   (or something else if you are doing a conversion)...then you know your display portion is working 

 

my display works well , i tested it many times to make sure that the problem isn't from it

Embedded software developer

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

what is your getbit?   Does it return a right justified result (0 or 1)

similar for setbit

#define EECR_EEMWE 2

would this return 0b00000001    or  would it return 0b00000100 (which is never ==1)

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

it return 0b000000001 , i wrote many drivers using these macros and they work perfectly , but this one isn't working for some reason , although the library <avr/eeprom.h> works well , 

Embedded software developer

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


Is this the correct address (ex $1D vs $3D)  ...I didn't think much about it

When using the I/O specific commands IN and OUT, the I/O addresses $00 - $3F must be used. When addressing I/O Registers as data space using LD and ST instructions, $20 must be added to these addresses.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

If you are using Studio, why not make your life soooooo much easier and simply use teh EEPROM.h header and the functions it contains? 

 

It does all the work for you.

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

I write all the drivers and libraries by myself when i used the eeprom.h library it worked fine , but i want to write the library from scratch 
 

Embedded software developer

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

omarahmed22 wrote:
but i want to write the library from scratch 
 

 

Fair enough.

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

omarahmed22 wrote:

I write all the drivers and libraries by myself when i used the eeprom.h library it worked fine , but i want to write the library from scratch 
 

does that include printf(), malloc(), strcpy(), sin(),... ?

 

Just wondering. 

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

 

does that include printf(), malloc(), strcpy(), sin(),... ?

 

Well, when we were in school, we wrote our own 2-pass linking assembler (It was quite a stack of punch cards)

Maybe time for a new C flavor--enough to keep one hopping for quite a while 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

omarahmed22 wrote:
I write a number on a byte then I read it and display it on LCD , but it prints 255,

Surely we need to see that invoking code as well.

 

Here is the CodeVisionAVR code for writing a byte:

__EEPROMWRB:
	SBIC EECR,EEWE
	RJMP __EEPROMWRB
s>	IN   R25,SREG
s>	CLI
	OUT  EEARL,R26
	OUT  EEARH,R27
r>	SBI  EECR,EERE
r>	IN   R24,EEDR
r>	CP   R30,R24
r>	BREQ __EEPROMWRB0
	OUT  EEDR,R30
	SBI  EECR,EEMWE
	SBI  EECR,EEWE
__EEPROMWRB0:
s>	OUT  SREG,R25
	RET

The lines that I marked with s> are to save-restore the I bit in SREG.  You will need interrupt protection in a real app.

 

The lines that I marked with r> are to do a read-before-write, and skip if the same value.  As this sequence takes a microsecond or several and the erase-write takes a millisecond or several, it ends up saving a lot of wasted time, especially when you consider the case of e.g. incrementing a multi-byte counter in EEPROM.

 

But other than that it looks about the same as your sequence.  Perhaps show the total [generated] code of a test program.  Be sure to tell processor model.

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.

Last Edited: Fri. Sep 23, 2022 - 12:36 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

omarahmed22 wrote:

I write all the drivers and libraries by myself when i used the eeprom.h library it worked fine , but i want to write the library from scratch 
 

does that include printf(), malloc(), strcpy(), sin(),... ?

 

Just wondering. 

No , i meant avr libraries 

Embedded software developer

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

theusch wrote:

omarahmed22 wrote:
I write a number on a byte then I read it and display it on LCD , but it prints 255,

Surely we need to see that invoking code as well.

 

Here is the CodeVisionAVR code for writing a byte:

__EEPROMWRB:
	SBIC EECR,EEWE
	RJMP __EEPROMWRB
s>	IN   R25,SREG
s>	CLI
	OUT  EEARL,R26
	OUT  EEARH,R27
r>	SBI  EECR,EERE
r>	IN   R24,EEDR
r>	CP   R30,R24
r>	BREQ __EEPROMWRB0
	OUT  EEDR,R30
	SBI  EECR,EEMWE
	SBI  EECR,EEWE
__EEPROMWRB0:
s>	OUT  SREG,R25
	RET

The lines that I marked with s> are to save-restore the I bit in SREG.  You will need interrupt protection in a real app.

 

The lines that I marked with r> are to do a read-before-write, and skip if the same value.  As this sequence takes a microsecond or several and the erase-write takes a millisecond or several, it ends up saving a lot of wasted time, especially when you consider the case of e.g. incrementing a multi-byte counter in EEPROM.

 

But other than that it looks about the same as your sequence.  Perhaps show the total [generated] code of a test program.  Be sure to tell processor model.

I added the main function to the post , 
do you mean the processor model of the PC or the avr

Embedded software developer

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

The datasheet says that EEWE must be set within 4 clock-cycles after setting EEMWE