Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] How can I save a 16bit data in 8bit EEPROM?

Status
Not open for further replies.
I would recommend writing two routines, a 16-bit write routine and 16-bit read routine.

The 16-bit write routine simply writes the LSB and MSB into consecutive EEPROM addresses starting at the address passed to the routine.

The 16-bit read routine simply reads the LSB and MSB from consecutive EEPROM addresses starting at the address passed to the routine.

Of course, you must decide on either a little endian or big endian method of storage.

For which microcontroller are you implementing these routines?

Also is the C Compiler, MikroC?

BigDog
 

Hi, I tried the code with modification. But it shows error that Lo, Hi ... are undeclared. here is the screed shot. Untitled.png
 

Add these lines at the top of the code.

Code:
#define Lo(param) ((char *)&param)[0]
#define Hi(param) ((char *)&param)[1]
#define Higher(param) ((char *)&param)[2]
#define Highest(param) ((char *)&param)[3]
 

Thank you for your help. I've modified the program as follows:

Code:
#define Lo(param) ((char *)&param)[0]
#define Hi(param) ((char *)&param)[1]
#define Higher(param) ((char *)&param)[2]
#define Highest(param) ((char *)&param)[3]

unsigned short eeprom_intenal_address, eeprom_external_address, lowByte, highByte, higherByte, highestByte;
unsigned long int myInt, eeprom_integer_value, askvalue = 10000;
char StrEEPROM[23], StrmyInt[23];

void EEPROM_Write(unsigned short eeprom_internal_address, unsigned long int eeprom_integer_value);
void EEPROM_Read(unsigned short eeprom_internal_address);

void EEPROMWriteInt(unsigned short eeprom_internal_address, unsigned long int eeprom_integer_value) {
        lowByte = Lo(eeprom_integer_value);
        highByte = Hi(eeprom_integer_value);
        higherByte = Higher(eeprom_integer_value);
        highestByte = Highest(eeprom_integer_value);

        EEPROM_write(eeprom_internal_address, lowByte);
        eeprom_internal_address++;
        delay_ms(20);
        EEPROM_write(eeprom_internal_address, highByte);
        eeprom_internal_address++;
        delay_ms(20);
        EEPROM_write(eeprom_internal_address, higherByte);
        eeprom_internal_address++;
        delay_ms(20);
        EEPROM_write(eeprom_internal_address, highestByte);
        eeprom_internal_address++;
        delay_ms(20);

        lowByte = 0x00;
        highByte = 0x00;
        higherByte = 0x00;
        highestByte = 0x00;

}

unsigned long int EEPROMReadInt(unsigned short eeprom_internal_address)
        {
        Lo(eeprom_integer_value) = EEPROM_read(eeprom_internal_address);
        eeprom_internal_address++;
        delay_ms(20);
        Hi(eeprom_integer_value) = EEPROM_read(eeprom_internal_address);
        eeprom_internal_address++;
        delay_ms(20);
        Higher(eeprom_integer_value) = EEPROM_read(eeprom_internal_address);
        eeprom_internal_address++;
        delay_ms(20);
        Highest(eeprom_integer_value) = EEPROM_read(eeprom_internal_address);
        eeprom_internal_address++;
        delay_ms(20);

        return eeprom_integer_value;

}


void display()
{
  ch = (tlong / 10000) % 10;
  //Lcd_Out(1,3,"Tmp");
  Lcd_Out(1,3,ROMtext_to_RAMtext(text2));
  Lcd_Chr_Cp(48+ch);          // Write result in ASCII format
  ch = (tlong / 1000) % 10;
  Lcd_Chr_Cp(48+ch);          // Write result in ASCII format
  ch = (tlong / 100) % 10;
  Lcd_Chr_Cp(48+ch);          // Write result in ASCII format
  Lcd_Chr_CP('.');
  ch = (tlong / 10) % 10;     // Extract hundreds of millivolts
  Lcd_Chr_CP(48+ch);           // Write result in ASCII format
  ch = (tlong / 1) % 10;      // Extract tens of millivolts
  Lcd_Chr_CP(48+ch);           // Write result in ASCII format
  Lcd_Chr_Cp(223);
  Lcd_Chr_CP('F');
  Lcd_Chr_CP(' ');
  Delay_ms(20);
}




while(RD0_bit == 0 || RD1_bit == 0)  // key function for asking value
  {
   RA5_bit = 1;
   Delay_ms(300);
   RA5_bit = 0;
   top:
       while(RD0_bit == 0)
       {
        askvalue++;
        
        eeprom_external_address = 0x00;
        eeprom_integer_value = 0;
        EEPROMWriteInt(eeprom_external_address, askvalue);
        eeprom_external_address = eeprom_external_address + 4;
        
        RA5_bit = 1;
        Delay_ms(50);
        RA5_bit = 0;
        break;
       }
       while(RD1_bit == 0)
       {
        askvalue++;

        eeprom_external_address = 0x00;
        eeprom_integer_value = 0;
        EEPROMWriteInt(eeprom_external_address, askvalue);
        eeprom_external_address = eeprom_external_address + 4;
        
        
        RA5_bit = 1;
        Delay_ms(50);
        RA5_bit = 0;
        break;
       }
        
        eeprom_external_address = 0x00;
        askvalue = EEPROMReadInt(eeprom_external_address);
        eeprom_external_address = eeprom_external_address + 4;
       value = askvalue;
       tlong =  value;
       setvalue = value; // getting the asking value here
       display();
  }

Here, I want to show the value of "askvalue". Thank you again. Solved
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top