View previous topic :: View next topic |
Author |
Message |
BoydNielsen
Joined: 20 Aug 2006 Posts: 12
|
Byte to Nibble |
Posted: Sun Oct 05, 2008 4:08 pm |
|
|
Does anyone have a quick and efficient way of converting a byte into two separate nibbles in CCS? Years ago on a different platform, we used to be able to:
Code: |
int8 a, MSnibble, LSnibble;
a = 174; // arbitrary number
MSnibble = a / 16; // 'MSnibble' is the upper nibble
LSnibble = a mod 16; // 'mod' for modulus
// 'LSnibble' is the lower nibble
|
However, this does not seem to work in CCS. A code snipet would be extremely helpful.
Boyd |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Oct 05, 2008 4:21 pm |
|
|
Look at the lcd_send_byte() function in the CCS lcd.c driver.
It passes nibbles to another function and it shows how to do it. |
|
|
BoydNielsen
Joined: 20 Aug 2006 Posts: 12
|
|
Posted: Sun Oct 05, 2008 4:33 pm |
|
|
Thanks. Sometimes one forgets the basics (shift right and bit masking)!
Code: |
a = 174;
MSnibble = a >> 4; // right shift to remove lower nibble
LSnibble = a & 0xF; // bit mask to remove upper nibble
|
Boyd |
|
|
Ttelmah Guest
|
|
Posted: Mon Oct 06, 2008 3:12 am |
|
|
As a comment, the original would have worked, if you had used the C modulus function (%, not 'mod'). Shifts are much quicker though. However the compiler is 'smart' enough, to perform division by 16, using shifts, so it would not make too much difference.
Best Wishes |
|
|
|