CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Quick Bitwise question

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
logical



Joined: 21 Dec 2009
Posts: 57
Location: SouthPort, UK

View user's profile Send private message Visit poster's website

Quick Bitwise question
PostPosted: Sat Mar 27, 2010 12:28 pm     Reply with quote

Hi All,

Is there a quick Bitwise operation to rotate all the bits in a byte ie MSB to LSB

Don't need the code, just a hint as to what function I should be studying.

Basically convert 1st to 8th, 2nd to seventh bits etc in one command if possible.

11001010 to

01010011

Reason being due to optimising a 24bit colour OLED which is working pretty sweet but as I didn't bother to use the PMP, I am wanting to manipulate the bits and write directly to the port rather than currently using bittest which is costing me 58 cycles for each RGB byte.

Sweet Densitron 24Bit Colour OLED with a 24HJ128GP502.
Thanks in advance.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat Mar 27, 2010 12:45 pm     Reply with quote

Your apparently asking about bit reversal. Rotation is something different. Fast bit reversal can be best done with a look-up table.
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Sat Mar 27, 2010 12:50 pm     Reply with quote

Are you sending the data to the device in serial fashion?

If so, how are you sending the bits out to the device?

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
logical



Joined: 21 Dec 2009
Posts: 57
Location: SouthPort, UK

View user's profile Send private message Visit poster's website

PostPosted: Sat Mar 27, 2010 12:59 pm     Reply with quote

Thanks for the prompt replies, am using parallel data but... probably easier to see with these functions:

Code:
void write_d(int out_data)
{
OUTPUT_BIT(RS,1);
OUTPUT_BIT(CSB,0);
OUTPUT_BIT(WRB,0);

 output_bit(D17,bit_test(out_data,7));
 output_bit(D16,bit_test(out_data,6));
 output_bit(D15,bit_test(out_data,5));
 output_bit(D14,bit_test(out_data,4));
 output_bit(D13,bit_test(out_data,3));
 output_bit(D12,bit_test(out_data,2));
 output_bit(D11,bit_test(out_data,1));
 output_bit(D10,bit_test(out_data,0));

OUTPUT_BIT(WRB,1);
OUTPUT_BIT(CSB,1);

}




   #define resetb pin_a0
   #define wrb pin_a1
   #define sck pin_a2
   #define sdo pin_a3
   #define sdi pin_a4
   #define rdb pin_b0
   #define csb pin_b1
   #define rs  pin_b2
   #define d9  pin_b3
   #define usd_cs pin_b4
   #define data pin_b5
   #define clock pin_b6
   #define oled_dc pin_b7
   #define led pin_b7
   #define d17 pin_b8
   #define d16 pin_b9
   #define d15 pin_b10
   #define d14 pin_b11
   #define d13 pin_b12
   #define d12 pin_b13
   #define d11 pin_b14
   #define d10 pin_b15


As I now know, I should have either used the PMP or totally connected the pins backwards for the OLED Data, did it the way it is as the OLED is very fine pitch and basically the data lines go totally straight to the PIC Chip making the protoboard easier to etch (single sided no vias)

Don't mind redoing the PCB as the above solution works much better than I had expected but trying to get the max speed out of the above, if not then PMP time.

As always, thanks everyone for the support.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat Mar 27, 2010 4:20 pm     Reply with quote

A benchmark shows this results:
- eightfold output_bit() (as above) with fast_io in effect 40 Cycles
- look-up table (in ROM) LATBH = bitrev[out_data]; 15 Cycles
- look-up table in RAM 7 Cycles

Code:
// rom table
const int8 bitrev[] = {0,0x80,0x40,0xc0,0x20 ...};
// ram table
int8 bitrev[] = {0,0x80,0x40,0xc0,0x20 ...};
logical



Joined: 21 Dec 2009
Posts: 57
Location: SouthPort, UK

View user's profile Send private message Visit poster's website

PostPosted: Sat Mar 27, 2010 5:37 pm     Reply with quote

Thanks FVM,

Will certainly look in to that as it would do nicely, have had a quick search on the forum for bitrev but can only find one post, nothing in the help manual on that, is it a command?

The post I found was this:
Code:

// this is a macro shortcut (this code will replace bitrev(c) at compile time) for reversing the bit order//
#define bitrev(c)   c = (c & 0x0F) << 4 | (c & 0xF0) >> 4; \
         c = (c & 0x33) << 2 | (c & 0xCC) >> 2; \
         c = (c & 0x55) << 1 | (c & 0xAA) >> 1;
 

Thanks again

Chris
logical



Joined: 21 Dec 2009
Posts: 57
Location: SouthPort, UK

View user's profile Send private message Visit poster's website

PostPosted: Sat Mar 27, 2010 5:55 pm     Reply with quote

Actually I missed the Look Up Table part.

Basically put all 255 values in RAM, so for the sake of it I will just use binary here for ease of use.

if data out was 0x01 then match it against the first memory location which would have the hex equiv of 10000000

if data out was 0x02 then the second value of the lookup would be
01000000
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Sat Mar 27, 2010 8:00 pm     Reply with quote

logical wrote:
Actually I missed the Look Up Table part.

Basically put all 255 values in RAM, so for the sake of it I will just use binary here for ease of use.

if data out was 0x01 then match it against the first memory location which would have the hex equiv of 10000000

if data out was 0x02 then the second value of the lookup would be
01000000


Right.
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
logical



Joined: 21 Dec 2009
Posts: 57
Location: SouthPort, UK

View user's profile Send private message Visit poster's website

PostPosted: Sat Mar 27, 2010 8:11 pm     Reply with quote

Sweet, thanks everyone for your help in this, genuinely appreciated to all concerned.

Going to go write a snippet to fill the Ram with the inverted values upon boot up, will save me converting 255 values manually with Calc ;o)
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun Mar 28, 2010 9:25 am     Reply with quote

A comparison of 6 bit reverse routines was posted in 2007: http://www.ccsinfo.com/forum/viewtopic.php?t=23364
You would have found it with keywords "bit order" or "reverse order".

This comparison includes a lookup table in ROM, so you don't have to do the calculations again. Too bad I didn't include the lookup table test in RAM, but this was before the PIC24 with the larger memory existed and was not considered an option.
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Sun Mar 28, 2010 10:18 am     Reply with quote

As an extra alternative, 'compromise' way of doing this:
Code:

const int8 nibble_tab[] = {0,8,4,0xC,2,0xA,6,0xE,1,9,5,0xD,3,0xB,7,0xF};
int8 swap_bits(int8 val){
   int8 temp;
   temp=swap(nibble_tab[val & 0xF]);
   temp=temp+nibble_tab[val/16];
   return temp;
}

This is nearly as fast as Neutones solution, and has the 'elegance' in terms of code appearance. I'm sure with some playing, this could be tidied.

Best Wishes
logical



Joined: 21 Dec 2009
Posts: 57
Location: SouthPort, UK

View user's profile Send private message Visit poster's website

PostPosted: Sun Mar 28, 2010 4:59 pm     Reply with quote

Thanks again, typically after my last post, hunting around in google for byte reversal gave me a link to the lookup table on this forum, had genuinely searched for it to save wasting anyones time, anyway thanks all and it works very well, was just not sure what to search for initially.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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