Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to convert long data to byte to write eeprom

Status
Not open for further replies.

cllunlu

Member level 4
Joined
Nov 21, 2006
Messages
76
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,900
Hi Everyone,

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

Thank you
 

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
19,486
Helped
4,308
Reputation
8,621
Reaction score
4,262
Trophy points
1,393
Activity points
129,197
Hi,

Why did you use the char (=byte) array to long?

Why not save the chars into the eeprom?

Klaus
 

xenos

Full Member level 4
Joined
May 9, 2015
Messages
212
Helped
82
Reputation
164
Reaction score
81
Trophy points
28
Location
127.0.0.1
Activity points
1,182
...load a char array that length is 24. After that I converted that array to unsigned long value....
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.
 

cllunlu

Member level 4
Joined
Nov 21, 2006
Messages
76
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,900
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
 

xenos

Full Member level 4
Joined
May 9, 2015
Messages
212
Helped
82
Reputation
164
Reaction score
81
Trophy points
28
Location
127.0.0.1
Activity points
1,182
Code:
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
 
Last edited:

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
19,486
Helped
4,308
Reputation
8,621
Reaction score
4,262
Trophy points
1,393
Activity points
129,197
Hi,

Why not bit shift the incoming data in a 3 byte array?

I don't see the benefit of using 24 bytes using only a single bit each...


Klaus
 

cllunlu

Member level 4
Joined
Nov 21, 2006
Messages
76
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,900
I couldnt understand that code

Especially x = (unsigned char)l;

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;
}

- - - Updated - - -

I wanna read wiegand26 data. Device sends 24+2 bit to me. I wanna record this data to eeprom
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
14,878
Helped
4,862
Reputation
9,742
Reaction score
4,643
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
126,641
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.

Brian.
 

horace1

Advanced Member level 5
Joined
Nov 18, 2008
Messages
2,123
Helped
596
Reputation
1,188
Reaction score
573
Trophy points
1,393
Location
Norwich, UK
Activity points
13,071
so long the write_eeprom() function prototype was declared as
Code:
write_eeprom(unsigned char x);
the long would be truncated to unsigned char when the function is called
 

xenos

Full Member level 4
Joined
May 9, 2015
Messages
212
Helped
82
Reputation
164
Reaction score
81
Trophy points
28
Location
127.0.0.1
Activity points
1,182
so long the write_eeprom() function prototype was declared as
Code:
write_eeprom(unsigned char x);
the long would be truncated to unsigned char when the function is called

with an annoying warning produced by the compiler...
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,301
Helped
14,233
Reputation
28,727
Reaction score
12,925
Trophy points
1,393
Location
Bochum, Germany
Activity points
279,678
the long would be truncated to unsigned char when the function is called
Are you talking about the code in post #5? Nothing is truncated therein.
 

cllunlu

Member level 4
Joined
Nov 21, 2006
Messages
76
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,900
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');

addr= EEPROM_Read(0x13);
shortToStr(addr,deneme);
UART1_Write_Text(deneme);
 

xenos

Full Member level 4
Joined
May 9, 2015
Messages
212
Helped
82
Reputation
164
Reaction score
81
Trophy points
28
Location
127.0.0.1
Activity points
1,182
When I write and read eeprom data,...

// Write some data at address 2???
Do you know that 0x10 is equal to 16 and 0b10 is equal to 2?

Try this
Code:
    unsigned char deneme[2];
    deneme[1] = 0;

    EEPROM_Write(0x10,'C');
    EEPROM_Write(0x11,'E');
    EEPROM_Write(0x12,'L');
    EEPROM_Write(0x13,'A');
    EEPROM_Write(0x14,'L');
    
    deneme[0] = EEPROM_Read(0x13);
    UART1_Write_Text(deneme);
you should read 'A' in the rs232 terminal

ps. I do not know the prototypes of EEPROM_Write and shortToString, I assume what they do by their names

- - - Updated - - -

Are you talking about the code in post #5? Nothing is truncated therein.
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:
with an annoying warning produced by the compiler...
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,301
Helped
14,233
Reputation
28,727
Reaction score
12,925
Trophy points
1,393
Location
Bochum, Germany
Activity points
279,678
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.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top