Page 1 of 1

EEPROM Problem & Switch problem (Solved)

Posted: Sun Jul 29, 2012 9:20 pm
by Jordy101091
Hello everyone,

I've trouble reading and writing to the eeprom of my PIC18F8722.
I wan to write a value of 296 to the eeprom, this is a value of a calibration process.
but when I read the same address the result is different.
Also I have tryed to write to address 1 instead of address 0 but same result.

Can some body look into this, because this setting cannot be lost after power-down.

And then there is a switch problem this is a snap-shot of a macro that i'm using for my calibration menĂ¼:

Image

as you can see here that I can use my rotary encoder to select the different sensors when I press the rotary encoder""ROT_SW" then its should enter that sub menu of sensor CO2 bt it never does, When I hold down so ROT_SW remains High end then rotate the encoder to select sensor CO2 then it enters the submenu, I dont know how where this behavior comes from because I used the same system in every menu and there it works perfectly.

Hope somebody has suggestion for this solve,

Regards Jordy

Re: EEPROM Problem & Switch problem

Posted: Sun Jul 29, 2012 11:07 pm
by Spanish_dude
Hi,
296 is a 9 bit value. EEPROM can save 8 bit values.
That's why when you read the value back, it's different.

What you need to do is split the value in 2 bytes and save those bytes to the EEPROM.
You can then read those 2 bytes back and put them together to make your 2 bytes value.

Code: Select all

byte = (value >> 8) & 0xFF;
write_to_eeprom(address, byte); // write high byte
byte = value & 0xFF;
write_to_eeprom(address+1, byte); // write low byte

--------------------

byte = read_from_eeprom(address); // read high byte
value = byte << 8;
byte = read_from_eeprom(address+1); // read low byte
value = value | byte;
Don't know about your switch problem.

Re: EEPROM Problem & Switch problem

Posted: Sun Jul 29, 2012 11:09 pm
by kersing
Jordy101091 wrote:Hello everyone,

I've trouble reading and writing to the eeprom of my PIC18F8722.
I wan to write a value of 296 to the eeprom, this is a value of a calibration process.
but when I read the same address the result is different.
Also I have tryed to write to address 1 instead of address 0 but same result.
You can only store values 0 to 255 (byte) in a single location. You will have to split 296 over two locations. Store the value / 256 in one and the remainder in the other location.

Re: EEPROM Problem & Switch problem

Posted: Mon Jul 30, 2012 12:48 am
by Enamul
Hi,
Although Nicolus clearly describes the idea..I thought it would be better to say bit more in FC..
For example, you want to write EE_DATA whose value is more than 255...declare EE_DATA as INT and DATA_H and DATA_L as Byte.
Write:
To write data in EEPROM, in calculation box...

Code: Select all

DATA_H = EE_DATA>>8
DATA_L = EE_DATA AND 0x00FF
1. EEPROM write macro...to write DATA_H
2. EEPROM write macro...to write DATA_L

Read:
1. EEPROM read macro..to read DATA_H
2. EEPROM read macro..to read DATA_L
To combine two 8-bit data into 16-bit data...in calculation box..

Code: Select all

EE_DATA = DATA_H
EE_DATA = (EE_DATA<<8) OR DATA_L
you will get EE_DATA as INT..
To deal with your Switch problem..can you post the program or the modified program with switch portion?
Enamul

Re: EEPROM Problem & Switch problem

Posted: Sun Aug 05, 2012 12:37 pm
by Jordy101091
Hi guys,

Sorry for this late response, I'm very bussy with work these days, this weekend I've got my switch problem working properly, I dont know what the cuase of the problem was, but i deleted all and builded it up again and now it works the way it supposed to.

as for the EEPROM this works perfect thanks for that.

Regards Jordy