Hello all,
I have been playing around with this but not so much in mikroC.
I want to reverse the bit order in a byte so that MSB to LSB becomes LSB to MSB.
Any ideas?
Mark
Reverse Bit order in Byte in MikroC
- lana.arsic
- mikroElektronika team
- Posts: 1715
- Joined: 15 Jan 2016 12:50
Re: Reverse Bit order in Byte in MikroC
Hi,
Here is one suggestion:
unsigned char reverse(unsigned char b) {
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
return b;
}
Best regards
Lana
Here is one suggestion:
unsigned char reverse(unsigned char b) {
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
return b;
}
Best regards
Lana
Re: Reverse Bit order in Byte in MikroC
Hi Lana,
Thank you for your reply.
I did try this but the term "reverse" not recognized by the compiler.
Any suggestions?
Mark
Thank you for your reply.
I did try this but the term "reverse" not recognized by the compiler.
Any suggestions?
Mark
-
- Posts: 1179
- Joined: 24 Nov 2005 20:07
- Location: Colorado, USA
Re: Reverse Bit order in Byte in MikroC
"reverse" is the name Lana gave to the example function. Make sure you have this function declared as a prototype in your source code if it's structured in the way that requires one. Or make sure this function is listed in the source code above the place where it is first called.
Re: Reverse Bit order in Byte in MikroC
Thank you!
I''ll do that.
Mark
I''ll do that.
Mark
Re: Reverse Bit order in Byte in MikroC
Hello to all,
I tried:
unsigned char reverse (unsigned char*);
therefore declaring a function prototype and I still get "invalid expression"
Please refer to my previous posts.
Any idea what could be the issue?
I'm using mikoC v6.6 now.
Regards,
Mark
I tried:
unsigned char reverse (unsigned char*);
therefore declaring a function prototype and I still get "invalid expression"
Please refer to my previous posts.
Any idea what could be the issue?
I'm using mikoC v6.6 now.
Regards,
Mark
-
- Posts: 1179
- Joined: 24 Nov 2005 20:07
- Location: Colorado, USA
Re: Reverse Bit order in Byte in MikroC
Code: Select all
unsigned char reverse (unsigned char*);
Re: Reverse Bit order in Byte in MikroC
Here is my code thus far.
Thanks for your help!
//#################################################
//Serial data converter
//CS3310 to DS1801
//invert CS
//invert bit order in data byte
//external clock (from master)
//*************************************************
//Define all Serial Pins
//*************************************************
//SDATA_OUT = GPIO.b0
//SDATA_IN = GPIO.b1
//CS_IN = GPIO.b4
//CS_OUT = GPIO.b2
//CLK_IN = GPIO.b5 //clock continues to 2 x DS1801
//#################################################
unsigned char i = 0;
unsigned char b = 0;
unsigned char rev_byte(unsigned char*); //Function declared
void main ()
{
CMCON = 7; //all inputs digital
INTCON = 0; //interrupts off
TRISIO = 0b00100110;
while (1)
{
i = GPIO.B4;
if(i == 0){GPIO.B2 == 1;} //CS invert
if(i == 1){GPIO.B2 == 0;}
Soft_UART_Init(&GPIO, 1, 0, 19200, 0); // Serial data bus initialization (RX=GPIO.B1 TX=GPIO.B0)
unsigned char rev_byte(char b) //serial data bit reversal in byte // This line compiled as "invalid expression"
{
return ((b & 0x1) << 7) | ((b & 0x2) << 5) |
((b & 0x4) << 3) | ((b & 0x8) << 1) |
((b & 0x10) >> 1) | ((b & 0x20) >> 3) |
((b & 0x40) >> 5) | ((b & 0x80) >> 7);
}
void Soft_UART_Write(unsigned char b); //write byte to tx pin
}
}
Thanks for your help!
//#################################################
//Serial data converter
//CS3310 to DS1801
//invert CS
//invert bit order in data byte
//external clock (from master)
//*************************************************
//Define all Serial Pins
//*************************************************
//SDATA_OUT = GPIO.b0
//SDATA_IN = GPIO.b1
//CS_IN = GPIO.b4
//CS_OUT = GPIO.b2
//CLK_IN = GPIO.b5 //clock continues to 2 x DS1801
//#################################################
unsigned char i = 0;
unsigned char b = 0;
unsigned char rev_byte(unsigned char*); //Function declared
void main ()
{
CMCON = 7; //all inputs digital
INTCON = 0; //interrupts off
TRISIO = 0b00100110;
while (1)
{
i = GPIO.B4;
if(i == 0){GPIO.B2 == 1;} //CS invert
if(i == 1){GPIO.B2 == 0;}
Soft_UART_Init(&GPIO, 1, 0, 19200, 0); // Serial data bus initialization (RX=GPIO.B1 TX=GPIO.B0)
unsigned char rev_byte(char b) //serial data bit reversal in byte // This line compiled as "invalid expression"
{
return ((b & 0x1) << 7) | ((b & 0x2) << 5) |
((b & 0x4) << 3) | ((b & 0x8) << 1) |
((b & 0x10) >> 1) | ((b & 0x20) >> 3) |
((b & 0x40) >> 5) | ((b & 0x80) >> 7);
}
void Soft_UART_Write(unsigned char b); //write byte to tx pin
}
}