Using SPI but backwards? MSB/LSB first?

14 Nov 2010 . Edited: 14 Nov 2010

Hi all,

 

I am using a 7 segment display that uses a SPI like input.

I have a set of character codes as hex. To display a P I would write 0xCE (11001110) - where each bit refers to a segment (the 8th being a decimal point)

That works on an Arduino where I wrote a function to output the codes, however I thought I'd use SPI on the mbed to achieve the same result.

It kind of half works - I can get the display to show characters - but it seems to be that mbed is outputting backwards to what the display wants (or the display wants the characters backwards :) )

So instead of sending 0001 it sends 1000

Is there a setting for SPI to achieve this or am I going to have to work out all the character codes again? :)

Alternatively a function that takes hex, converts to binary, reverses it and then converts back to hex.


James


14 Nov 2010 . Edited: 14 Nov 2010

Hi James,

The best approach (at least to test it works) may be to use a function to reverse the bits.

For example, you could even use the __rbit() instruction to reverse the bits of a register, so it'd look something like this on a Cortex-M3:

uint8_t reverse_byte(uint8_t byte) {
    return (__rbit(byte) >> 24) & 0xFF; // reverse a byte in a 32-bit value, and extract the byte 
}

At least until you've convinced yourself it is worth reversing the constants!

Simon

14 Nov 2010

Hi Simon - thanks for that!

 

Yup, it works for sure if they are backwards.

I Googled and found this to reverse a byte:

 

result = (hexToReverse * 0x0202020202ULL & 0x010884422010ULL) % 1023;

 

That seems to work fine.

Not sure if yours is quicker/slower/more efficient?

 

James

14 Nov 2010

you could write am mbedprogram, that takes your current file, rotates it arround, and with a little printf majic,

you can regenerate your file.

this metod is also very good for performing 90degree rotations of charictor LCD  Displays,

esspesialyforfar less powerfull micros.

 

Regards

    Ceri