Silver Pearl
Silver Pearl

Reputation: 5

EEPROM read and write for 5 bits

I have this code that write and write from EEPROM for 4 digit number. For Ex: 2356

Code;

void WritePassToEEPROM(uint16_t pass)
{
   EEPROMWrite(0000,(pass%100));
   EEPROMWrite(0001,(pass/100));
}

uint16_t ReadPassFromEEPROM()
{
   return (EEPROMRead(0001)*100  +  EEPROMRead(0000));
}

The Write_Pass_To_EEPROM() function writes to 2 addresses 0000 and 0001. for 2356, 2356%100 is 56 and 2356/100 is 23. So, at address 0000 it will be 56 and at address 0001 it will be 23. While reading EEPROM_Read(0000) will return 34 and EEPROM_Read(0001)*100 will return 2300. 2300 + 56 is 2356.

But if i need to write 5 digit number like 65238 what should i do.

Upvotes: 0

Views: 553

Answers (1)

Rok Jarc
Rok Jarc

Reputation: 18875

This will go up to 0xffff (65535).

void WritePassToEEPROM(uint16_t pass)
{
   EEPROMWrite(0000,(pass & 0x00ff));
   EEPROMWrite(0001,(pass & 0xff00) >> 8);
}

uint16_t ReadPassFromEEPROM()
{
   return ((uint16_t)(EEPROMRead(0001) << 8)  +  (uint16_t)EEPROMRead(0000));
}

Upvotes: 0

Related Questions