Continue to Site

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.

[SOLVED] XC8 Compiler How to split data

Status
Not open for further replies.

kommanche

Member level 4
Joined
Mar 18, 2007
Messages
70
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,766
Hi,

I am newbie in PIC programming. I am using PIC12F1822 and XC8 compiler. Can you please help me with the following:

What I want to do is write a function for breaking 30 bit data into pieces and write them to target IC.

How to do such a thing:

char x=2DC6B2B3
asd(x)


void asd (char xx)

{

x1= "an expression to take MSB 8 bits of xx"
x2= "an expression to take next 8 bits of xx"
x3= "an expression to take next 8 bits of xx"
x4= "an expression to take last 6 bits of xx"

}

Thanks
 

Code:
void asd ([B]long [/B]xx)

{

x1= xx >> 22;
x2= (xx & 0x3FFFC0) >> 14;
x3= (xx & 0x3FC0)) >> 6;
x4= xx & 0x3F;

}
 
It is impossible but I think I know what you are trying to do.

A char is only 8 bits wide so it's numeric range can only be from 0x00 to 0xFF. You need an unsigned long type instead of a char.

To split it into unsigned chars you can use:
x4 = (xx & 0x0000003F); // last 6 bits
x3 = (xx & 0x00003FC0) >> 6; // next 8 bits
x2 = (xx & 0x003FC000) >> 14; // next 8 bits
x1 = (xx & 0x3FC00000) >> 22; // most significant bits

I think I got that right - I'm using a cell phone so I can't check for you.

An alternative method is to create a union of unsigned long and four unsigned chars, write xx to it then read it back as individual bytes.

Brian.
 
It is impossible but I think I know what you are trying to do.

A char is only 8 bits wide so it's numeric range can only be from 0x00 to 0xFF. You need an unsigned long type instead of a char.

To split it into unsigned chars you can use:
x4 = (xx & 0x0000003F); // last 6 bits
x3 = (xx & 0x00003FC0) >> 6; // next 8 bits
x2 = (xx & 0x003FC000) >> 14; // next 8 bits
x1 = (xx & 0x3FC00000) >> 22; // most significant bits

I think I got that right - I'm using a cell phone so I can't check for you.

An alternative method is to create a union of unsigned long and four unsigned chars, write xx to it then read it back as individual bytes.

Brian.

Thank you for your help.

Actually "char" is not critical. I may use long.

I will be happy if you help me with one more question:

Let's say I define A= 0x4B2F;
I want to split it 4 pieces which have 8 bits each (total 32 bits) by adding 2 zeros(binary) in LSB; and required amount of 0's in MSB (binary) like this:

A= 0x4B2F;

A1= 00000000;
A2= 00000001;
A3= 00101100;
A4 =10111100;

Thanks in advance,
 

The easiest method is to start with a 32 bit variable (preferably an unsigned long), copy the value to it then shift it two places to the left. That gives you a 32 bit number with the bits in the right place. Next, divide it into 4 bytes by masking the bits with 0xFF000000, 0xFF0000, 0xFF00 and 0xFF to isolate the 8 bit groups and shift the results the appropriate number of places so they align to the lowest byte positions.

Again you can use a union. A union is a data structure where the variable stored in it share the same addresses, they are 'overlayed' on each other. If you write the value to a 32 bit variable in the union you can then read it out as a different kind of variable, in your case as 4 8-bit variables.

Brian.
 
unsigned char x1, x2, x3, x4;
unsigned long int xx;

x4 = xx & 0x3F;
x3 = xx >> 6;
x2 = xx >> 14;
x1 = xx >> 22


2.

A= 0x4B2F;

A1= 00000000;
A2= 00000001;
A3= 00101100;
A4 =10111100;

If 0x00 is added to LSB then it becomes 0x4B2F00

and to make it 32 bits if you add another 0x00 to MSB it becomes 0x004B2F00 which is same as 0x4B2F00.

So,

A1 = 0x00 = 0b00000000
A2 = 0x2F = 0b00101111
A3 = 0x4B = 0b01001011
A4 = 0x00 = 0b00000000


unsigned char A1, A2, A3, A4
unsigned long int y = 0x4B2F;


y <<= 8;

A1 = y;
A2 = y >> 8;
A3 = y >> 16;
A4 = y >> 24;

Oops. I got it wrong.

Here how it should be done


unsigned char A1, A2, A3, A4
unsigned long int y = 0x4B2F;

y <<= 2;

A4 = y;
A3 = y >> 8;
A2 = y >> 16;
A1 = y >> 24;

- - - Updated - - -

Try this. This might not work for all values.


Code C - [expand]
1
2
3
4
5
6
7
8
9
unsigned char A1, A2, A3, A4
unsigned long int y = 0x4B2F;
 
     y <<= 2;
 
A4 = y;
A3 = (y >> 8);
A2 = (y >> 16);
A1 = (y >> 24);

 
Last edited:
Thank you for your help friends,

I haven't tried it yet but I hope this will work :

unsigned long dummy;
dummy = XX;
unsigned long x1,x2,x3,x4;


dummy <<= 1;
dummy <<= 1;

x1 = (dummy & 0xFF000000) >> 24; // MSB
x2 = (dummy & 0x00FF0000) >> 16; // Next 8
x3 = (dummy & 0x0000FF00) >> 8; // Next 8
x4 = (dummy & 0x000000FF); // last 8

Regards,
 

If you use unsigned char type for x1, x2, x3 and x4 then there is no need for ANDing.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top