Reverse Bit order in Byte in MikroC

General discussion on mikroC.
Post Reply
Author
Message
lkmurdoch
Posts: 8
Joined: 07 Nov 2015 23:11

Reverse Bit order in Byte in MikroC

#1 Post by lkmurdoch » 14 Jan 2016 19:39

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

User avatar
lana.arsic
mikroElektronika team
Posts: 1715
Joined: 15 Jan 2016 12:50

Re: Reverse Bit order in Byte in MikroC

#2 Post by lana.arsic » 15 Jan 2016 17:11

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

lkmurdoch
Posts: 8
Joined: 07 Nov 2015 23:11

Re: Reverse Bit order in Byte in MikroC

#3 Post by lkmurdoch » 16 Jan 2016 15:17

Hi Lana,

Thank you for your reply.

I did try this but the term "reverse" not recognized by the compiler.

Any suggestions?

Mark

Sparky1039
Posts: 1179
Joined: 24 Nov 2005 20:07
Location: Colorado, USA

Re: Reverse Bit order in Byte in MikroC

#4 Post by Sparky1039 » 17 Jan 2016 08:27

"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.

lkmurdoch
Posts: 8
Joined: 07 Nov 2015 23:11

Re: Reverse Bit order in Byte in MikroC

#5 Post by lkmurdoch » 17 Jan 2016 15:41

Thank you!

I''ll do that.

Mark

lkmurdoch
Posts: 8
Joined: 07 Nov 2015 23:11

Re: Reverse Bit order in Byte in MikroC

#6 Post by lkmurdoch » 18 Jan 2016 16:24

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

Sparky1039
Posts: 1179
Joined: 24 Nov 2005 20:07
Location: Colorado, USA

Re: Reverse Bit order in Byte in MikroC

#7 Post by Sparky1039 » 20 Jan 2016 02:49

Code: Select all

unsigned char reverse (unsigned char*);
Semicolon after the function? Look again at Lana's code example.

lkmurdoch
Posts: 8
Joined: 07 Nov 2015 23:11

Re: Reverse Bit order in Byte in MikroC

#8 Post by lkmurdoch » 21 Jan 2016 00:17

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
}
}

Post Reply

Return to “mikroC General”