|
|
View previous topic :: View next topic |
Author |
Message |
Guille Guest
|
How can I implement something like that in CCS C compiler |
Posted: Wed Sep 05, 2007 2:25 pm |
|
|
Code: | typedef union{
unsigned int WORD[1];
unsigned char BYTE[2];
tData8bits BYTEbits[2];
}tData2bytes;
typedef union{
unsigned long DWORD[1];
unsigned int WORD[2];
unsigned char BYTE[4];
tData8bits BYTEbits[4];
}tData4bytes; |
I need to implement a code like that. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Sep 05, 2007 3:40 pm |
|
|
The variable name BYTE is not allowed in CCS as it is a reserved keyword, you will have to choose another name. Then, if you have to change the name of BYTE, it is a good idea to change the name of Word as well.
Code: | typedef int8 tData8bits;
typedef union{
unsigned int myWORD[1];
unsigned char myBYTE[2];
tData8bits BYTEbits[2];
}tData2bytes;
typedef union{
unsigned long myDWORD[1];
unsigned int myWORD[2];
unsigned char myBYTE[4];
tData8bits BYTEbits[4];
}tData4bytes; |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Sep 05, 2007 3:40 pm |
|
|
You can do it, but it requires vs. 4 of the compiler to do the array of bits.
The program shown below displays the following output. It was tested
with vs. 4.053.
Quote: |
12345678
12345678
12345678
12345678
|
Here is the demo program:
Code: |
#include <16F877.h>
#fuses XT, NOWDT, PUT, BROWNOUT, NOLVP
#use Delay(Clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
typedef union
{
int32 dword[1];
int16 words[2];
int8 bytes[4];
int1 bits[32];
}t_32bits;
//==================================
void main(void)
{
int8 i;
int32 temp;
t_32bits data;
data.dword[0] = 0x12345678;
printf("%lx\n\r", data.dword[0]);
printf("%lx%lx\n\r", data.words[1], data.words[0]);
printf("%x%x%x%x\n\r", data.bytes[3], data.bytes[2],
data.bytes[1], data.bytes[0]);
// Shift the bit array into a 32-bit variable and display it.
temp = 0;
for(i=0; i <32; i++)
{
shift_right(&temp, 4, data.bits[i]);
}
printf("%lx\n\r", temp);
while(1);
} |
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|