I read data from interrupt 1 or 0 and load a char array that length is 24. After that I converted that array to unsigned long value. Now I wanna write this data to eeprom with byte to byte.
Anyone help to me how to do converting byte to byte
Reverse that procedure.
If you feel it's difficault to do that, then probably the initial procedure to convert byte array to unsigned long was neighter efficient, nor simple.
You could post this code here if you need help.
We do not even know what language you are programming in.
I use mikroC compiler. Assume that I have char array that size is 24 and values are {'1','0','1', ...}
I convert this array to long value (14811184). Now I have to write this data to eeprom. Now I can write it as three byte. Because size is 24 bit. How can I write this data to eeprom as 3 byte
typedef union {
unsigned long l;
unsigned char b[sizeof l];
} ul_bytes;
void do_something(unsigned long v){
char i;
ul_bytes u;
u.l = v;
for(i=0;i<3;i++){//assuming little endian
write_eeprom(u.b[i]);
}
}
or
Code:
//portable code
unsigned char i,x;
unsigned long l = 0x1234;
for(i=0;i<3;i++){
x = (unsigned char)l;
write_eeprom(x);
l >>= 8;
}
or
Code:
//assuming little endian
#define GETBYTE(v,i) (((unsigned char *)&v)[i])
char i;
//to write long to eeprom
for(i=0;i<3;i++)
write_eeprom(GETBYTE(v,i));
//to read from eeprom into long
for(i=0;i<3;i++)
GETBYTE(v,i) = read_eeprom();
}
I use mikroC compiler. Assume that I have char array that size is 24 and values are {'1','0','1', ...}
I convert this array to long value (14811184). Now I have to write this data to eeprom. Now I can write it as three byte. Because size is 24 bit. How can I write this data to eeprom as 3 byte
The '(unsigned char)' is tells the compiler that the number flowing it is to be treated as though it is an unsigned character value rather than it's original size. In this instance it makes it ignore the first 16 bits and only use the last 8. Later in the loop, the 24 bit value is shifted 8 positions to the right which means what was originally bits 15:8 are then in positions 7:0 where they can then be treated as an unsigned char again.
When I write and read eeprom data, have some problems. I am trying to write eeprom. After that I check it programmer software that is ok. But When I read data that address, it doesnt send truth data.
unsigned short addr;
unsigned char deneme[2];
EEPROM_Write(0x10,'C'); // Write some data at address 2
EEPROM_Write(0x11,'E');
EEPROM_Write(0x12,'L');
EEPROM_Write(0x13,'A');
EEPROM_Write(0x14,'L');
I assume that he talks about omiting the type casting of [unsigned long] to [unsigned char], because it will be automaticaly done by the compiler (if the definition of function...).
This is why I replied:
I assume that he talks about omiting the type casting of [unsigned long] to [unsigned char], because it will be automaticaly done by the compiler (if the definition of function...).
O.K., so it's a suggestion to omit the superfluous x variable. You can in fact do this without generating a warning by pulling the type cast into the function call.