PDA

View Full Version : Optimize eeprom capacity



mombasa
- 2nd December 2014, 14:28
Hi, I have some eeprom locations where I store information as possible . For example, I read the contents of four memory locations , I transfer it on four variables for a total of 32 bits. Now I have to memorize series of numbers ranging from 0 to 59 and then for this needs only six bits each so I can store 5 numbers and I advance two locations where I can store a variable that arrive up to 3 and so on . Or I could enter numbers of up to 10 occupying only 4 bits and I could insert 8. In short, I would like to optimize memory I have available The problem is how to put in the byte , and then remove only the bits that interest me ? There is an elegant way to manipulate these variables and then transfer them in eeprom? I hope I was clear . thanks

towlerg
- 2nd December 2014, 17:17
External EEPROM is so cheap and easy to use, is it really worth it?

mombasa
- 2nd December 2014, 17:58
Unfortunately I have to change the program of a Pic with EEPROM and I have to add more data . I could insert another EEPROM but I should change almost one hundred printed circuit boards. I think it is more convenient to optimize what I have now . Thanks ;)

HenrikOlsson
- 2nd December 2014, 19:51
Hi,
Sounds a lot like BASE64 encoding to me...
Anyway, here's one way of doing what you ask:

InValues VAR BYTE[4]
OutValues VAR BYTE[3]


' Max assignable value is 63
InValues[0] = 50
InValues[1] = 51
InValues[2] = 52
InValues[3] = 53

Main:
HSEROUT["Program start",13]
OutValues[0] = ((InValues[0] & %00111111) << 2) + ((InValues[1] & %00110000) >> 4)
OutValues[1] = ((InValues[1] & %00001111) << 4) + ((InValues[2] & %00111100) >> 2)
OutValues[2] = ((InValues[2] & %00000011) << 6) + ((InValues[3] & %00111111))

' Print input values, only the 6 least significant bits of each value
HSEROUT[BIN6 InValues[0], BIN6 InValues[1], BIN6 InValues[2], BIN6 InValues[3],13]

' Print output values, now encoded into 3 bytes.
HSEROUT[BIN8 OutValues[0],BIN8 OutValues[1], BIN8 OutValues[2],13]

Pause 100
END

It produsces the followin output

Program start
110010110011110100110101
110010110011110100110101

/Henrik.

mombasa
- 8th December 2014, 10:27
Thanks Henrik, your solution seems like a good starting point .

Amoque
- 8th December 2014, 12:42
I think, as Henrik demonstrates, allotting 6 bits to each value is the most efficient or supportable or likely to be upgradable later (perhaps at some future date a pin-compatible processor with more EEPROM) method and so this is my advice as well. And, as he demonstrates the details, I will only suggest that a bit array of 24 elements would store 4 of your 6 bit numbers and write to 3 bytes of EEPROM without waste (well, except the two final bytes). For myself, it seems easiest to read and write to this array - shifting values in prior to a write and out to retrieve the stored values. Simpler in my mind to calculate retrieval positions with integer math. Henrick has done the "heavy lifting" for the subroutines to read and write...

You may also look at BCD encoding. This is typically done with 4 or 8 bits, but there are 6 bit examples out there. Perhaps there is something that we have not thought of...