View previous topic :: View next topic |
Author |
Message |
blo
Joined: 11 May 2009 Posts: 22
|
from long int to 2 int |
Posted: Thu May 21, 2009 6:22 am |
|
|
As title say, I need to split a long int variable into 2 different int variable after have shifted left the long variable.
I use this code:
Code: | long int data =0x03ff; //bit from 0 to 9 set to 1
unsigned int datah, datal;
datah = data>>2;
datal=data>>10; |
unfortunatly this code doesn't work.
Can you help me?
Thanks |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu May 21, 2009 6:42 am |
|
|
Starting with 0x03FF your shift and split: Code: | datah = data>>2;
datal=data>>10; | will result in datah=0xFF and datal=0x00
What effect do you want to achieve? |
|
|
blo
Joined: 11 May 2009 Posts: 22
|
|
Posted: Thu May 21, 2009 6:48 am |
|
|
i'm starting with
data= 0000001111111111 bit from 9 to 0 set 1
then i need to shift left data by 2 bit
data= 0000111111111100
now bit from 15 to 8 must be written into datah and bit from 7 to 0 mast be written into datal
datah=00001111
datal =11111100
P.S.
I'm writing this code beacause i'm doing the library for the 10bit DAC ad5315.
If anyone know where i can find a library done tell me please. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu May 21, 2009 7:05 am |
|
|
'>>' shifts to the right, not to the left...
Code: | long int data =0x03FF; //bit from 0 to 9 set to 1
unsigned int datah, datal;
// Solution 1: straight forward
datal = (int8)(data << 2);
datah = (int8)(data >> 6);
// Solution 2: easier to understand and shorter code
data <<= 2;
datal = make8(data,0);
datah = make8(data,1); | Solution 2 results in shorter code because there are fewer shifts on the int16. The PIC processors are very efficient in int8 shifting, but for an int16 every single bit shift requires two instructions. |
|
|
blo
Joined: 11 May 2009 Posts: 22
|
|
Posted: Thu May 21, 2009 7:21 am |
|
|
I don't know why if I use this code:
Code: | long int data = 0000001111111111;
unsigned int datah,datal;
data <<= 2;
datal = make8(data,0);
datah = make8(data,1);
lcd_cloc(0,0); //clear lcd
printf(lcd_o, "datah= %u" datah);
lcd_loc(1,0);
printf(lcd_o, "datal= %u" datal); |
datah is 73 -->01001001
datal is 36 -->00100100
for the other code the results are the same.
EDIT:
I'm stupid, when I declare data i've forgot to write 0b befor the value.
now it seems work. |
|
|
|