Re: Optimize eeprom capacity
External EEPROM is so cheap and easy to use, is it really worth it?
Re: Optimize eeprom capacity
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 ;)
Re: Optimize eeprom capacity
Hi,
Sounds a lot like BASE64 encoding to me...
Anyway, here's one way of doing what you ask:
Code:
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
Code:
Program start
110010110011110100110101
110010110011110100110101
/Henrik.
Re: Optimize eeprom capacity
Thanks Henrik, your solution seems like a good starting point .
Re: Optimize eeprom capacity
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...