Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3600682
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:31:34+00:00 2026-05-18T20:31:34+00:00

I am trying to write and read data from EEPROM (microcontroller ATmega2560 ), which

  • 0

I am trying to write and read data from EEPROM (microcontroller ATmega2560), which gives me the wrong answer. When I debug it, I see that only the last character is being read though I see that data is being written on different addresses.

At uiAddress = 1, the data is A, at uiAddress = 2 the data is B, uiAddress=3 data=67’C’, and so on. So when you read from uiAddress = 0 to the last address, you should get ABCDE. I am reading one bit, one character at a time.

EESAVE is enabled.

Why is that happening? (I’ve tried to include as much code as possible, the original file is too big. But this is the concerned area).

#include<avr/io.h>
#include<avr/eeprom.h>
#include<avr/interrupt.h>

volatile UINT intrs, i = 1, count, switch_pressed = 0, uiAdd, uiAddEnd, flag_led_intr;
volatile UINT record, play_recorded_keys, flag_serial_receiver;

volatile unsigned char get_switch=0, data, TX_complete, TX, RX;

extern void __vector_25 (void) __attribute__ ((signal)); //Interrupt vector

#define LED_DELAY 10

#define F_CPU 2000000L

#define BAUDRATE 9600

#define BAUD_PRESCALER (((F_CPU/(BAUDRATE * 16UL)))-1)

void ReadWriteSerialPort(void)
{
    while(((UCSR0A) & (1<<UDRE0)) == 0)
        ;

    UDR0 = RX;

    if(RX == 0x1A) //CRTL-z
    {
        record = !record;
        play_recorded_keys = 0;
    }
    else
        if(RX == 0x19) //CRTL-y
        {
            record = 0;
            uiAdd = 0;
            play_recorded_keys = !play_recorded_keys;
        }

    if(record == 1)
    {
        EEPROM_write(uiAdd++, RX);
    }

    if(uiAdd == 4096)
    {
        record = 0;
        uiAddEnd = 4096;
    }
    else
        uiAddEnd = uiAdd;
}

void initialize(void)
{
    cli();        //Stop all interrupts

    flag_led_intr = 0;
    record = 0;
    play_recorded_keys = 0;
    RX = 0;
    TX = 0;
    flag_serial_receiver = 0;

    uiAdd = 0;
    uiAddEnd = 0;

    enable_ports();
    usart_init();

    sei();
}

void enable_ports() //Enables PORTB, PORTD
{
    DDRB = 0xff;  //PORTB as output for leds

    PORTB = 0xff; //Initialize PORTB

    DDRD = 0x00;  //PORTD as input for switches
}

void usart_init(void) //Enables USART
{
   /* Set baud rate */

   UBRR0L = BAUD_PRESCALER);
   UBRR0H = (BAUD_PRESCALER>>8);

   /* Set frame format: 8 bit data + start bit + stop bit */

   UCSR0C = 0x06;

   /* Enable reciever and transmitter */

   UCSR0B = 0x98;
}

void EEPROM_write(unsigned int uiAddress, unsigned char ucData)
{
    while(EECR & (1<<EEPE));    /* Wait for completion of previous write */

        EEARH = (uiAddress>>8); /* Set up address and Data Registers */
        EEARL = uiAddress;

        EEDR = ucData;
        cli();
        EECR |= (1<<EEMPE);     /* Write logical one to EEMPE */

        EECR |= (1<<EEPE);      /* Start eeprom write by setting EEPE */
        sei();
}
unsigned char EEPROM_read(unsigned int uiAddress)
{
        while(EECR & (1<<EEPE)); /* Wait for completion of previous write */

        EEARH = (uiAddress>>8);  /* Set up address register */
        EEARL = uiAddress;

        EECR |= (1<<EERE);       /* Start eeprom read by writing EERE */

        return EEDR;             /* Return data from Data Register */
}

void __vector_25 (void)
{
    RX = UDR0;
    flag_serial_receiver = 1;
    sei();
}

int main(void)
{
   initialize();

   while(1)
   {
        if(flag_serial_receiver == 1)
        {
            ReadWriteSerialPort();
            flag_serial_receiver = 0;
        }

        if(play_recorded_keys)
        {
            TX = EEPROM_read(uiAdd);
            uiAdd++;

            if(uiAdd == 4096 || uiAdd >= uiAddEnd)
            {
                play_recorded_keys = 0;
                uiAdd = 0;
            }
            while(((UCSR0A) & (1<<UDRE0)) == 0)
                ;
            UDR0 = TX;
        }
    }
    return(0);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:31:35+00:00Added an answer on May 18, 2026 at 8:31 pm

    I would bet that either your write or read or both is not happening. Please verify that you are constructing the flag bits correctly as ruslik hinted in comments by using a debugger or debug output to manually check the configuration register contents against the chip data sheet.

    A useful test would be to set EEDR to an unexpected test value before calling the read. If the read returns this unexpected value, you know you didn’t actually read but just got a stale value of EEDR. That could be due to either not setting the right flags, or perhaps needing to wait for the read to complete but not doing so.

    You could also try playing with the order or writes and reads – for example, writing in increasing order but reading out in decreasing.

    Experiment around with constructing such tests that would reveal different sorts of errors, and I’m sure you will figure it out quickly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write and read data from EEPROM (microcontroller ATmega2560 ), which
I'm trying to read binary data from a specific offset. I write the data
I am trying to write a simple program in C# that will read data
I am trying to write a metro style application to read data from a
I was trying to read data from some huge file and write them back,
I'm trying to read data from a text file, clear it, and then write
I am trying to write a basic app that will read data off a
I am trying to write an android app to read and write data from
I'm trying to read and write the data to the server using java program.
I am trying to write some simple code which will read a text file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.