hi i need to split a 16 bit variabile in 2 variabile of one byte on c language.. and viceversa.. the MSB in the first var and the LSB on the second var...
can hel me???
Moderator: ericb
short var; // 16 bit variable
char hi, lo; // 8 bit variables
hi = (var>>8)&255; // set hi to the upper 8 bits of var
lo = (var&255); // set lo to the lower 8 bits of var
var = (hi<<8) + lo; // set var to 16 bit value from hi and lo
typedef union {
unsigned int Word;
struct {
unsigned char Low;
unsigned char High;
} Byte;
} WordByteType;
WordByteType Value, AnotherValue;
Value. Word = 0x1234;
UART_PutChar(Value.Byte.Low); //Send 0x34
UART_PutChar(Value.Byte.High); //Send 0x12
AnotherValue.Byte.Low = 0x78;
AnotherValue.Byte.High = 0x65;
PWM16_WritePeriod(AnotherValue.Word); // Write 0x6578
unsigned char low_byte;
unsigned char high_byte;
unsigned int the_value;
unsigned char *the_ptr;
...
the_value = 1234;
the_ptr = * ((unsigned char *)&the_value);
low_byte = *the_ptr;
the_ptr++;
high_byte = *the_ptr;
Return to “%s” HiTech C User-to-User Support
Users browsing this forum: No registered users and 1 guest