View previous topic :: View next topic |
Author |
Message |
pilar
Joined: 30 Jan 2008 Posts: 197
|
Rotate 2 byte |
Posted: Thu Jun 09, 2011 3:33 pm |
|
|
Hi someone can tell me how I can rotate 2 bytes?
Code: | Example: I hace 0xFD45 I need to have 0x45FD |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jun 09, 2011 4:20 pm |
|
|
You can use a swap_bytes() macro. It displays this result:
Quote: |
initial value = 1234
result = 3412 |
Here's the test program:
Code: |
#include <16F877.H>
#fuses XT, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#define swap_bytes(x) make16(x, x >> 8)
//==========================================
void main()
{
int16 result;
int16 value;
value = 0x1234;
printf("initial value = %lx \n\r", value);
result = swap_bytes(value);
printf("result = %lx \n\r", result);
while(1);
} |
|
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Thu Jun 09, 2011 4:33 pm |
|
|
A byte swap can be effectively performed with built-in functions
Code: | ival16 = make16(make8(ival16,0),make8(ival16,1)); |
|
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Thu Jun 09, 2011 5:07 pm |
|
|
Thank you... |
|
|
|