Bit re-mapping / re-arranging
SPI write actually supports any number of bits in the format.
see for http://mbed.org/forum/mbed/topic/1630/ for ref to my original query.
For my own sake, here is some hashing out for a remapping routine, might come in useful at some point or another.
#include "mbed.h"
short InputArray [23]; //24 elements
char OutputArray [35]; //3 elements
char swapnibbles(char myChar) {
char swapped = ((myChar >> 4) & 0x0f) | ((myChar << 4) & 0xf0);
return swapped;
}
///can these be refactored in to a single routine which accepts
//input value
//number of bits to mask (as a value)
//offset (to push to maskup or down against the value)
//and still return a byte
//use the offset to calculate how much to push resulting masked off section back down to fit into a char?
char botnibble(short Input) {
char OutputByte = 0;
short BitMask = 0x000F; //Mask off the bottom nibble only
short Temp = 0;
Temp = (Input & BitMask); //Logical AND Mask and Input to get the lower nibble only
OutputByte = OutputByte + Temp;
return OutputByte;
}
char midnibble(short Input) {
char OutputByte = 0;
short BitMask = 0x00F0; //Mask off the middle nibble only
short Temp = 0;
Temp = (Input & BitMask); //Logical AND Mask and Input to get the lower nibble only
OutputByte = OutputByte + (Temp >> 4); //shift all the bits in the result right 4 places such that value is in the bottom of the byte
return OutputByte;
}
char topnibble(short Input) {
char OutputByte = 0;
short BitMask = 0x0F00; //Mask off the top nibble only
short Temp = 0;
Temp = (Input & BitMask); //Logical AND Mask and Input to get the lower nibble only
OutputByte = OutputByte + (Temp >> 8); //shift all the bits in the result right 8 places such that value is in the bottom of the byte
return OutputByte;
}
main() {
int i = 0 ;
char a,b,c,d,e,f = 0;
for (i=0; i<=23;i++) {
a = botnibble(InputArray[(2*i)]);
b = midnibble(InputArray[(2*i)]);
c = topnibble(InputArray[(2*i)]);
d = botnibble(InputArray[((2*i)+1)]);
e = midnibble(InputArray[((2*i)+1)]);
f = topnibble(InputArray[((2*i)+1)]);
OutputArray[(3*i)] = a + swapnibbles(b);
OutputArray[((3*i)+1)] = c + swapnibbles(d);
OutputArray[((3*i)+2)] = e + swapnibbles(f);
};
}
0 comments
You need to log in to post a comment
