# MSP430 SPI 12 bit to 8 bit ?

Status
Not open for further replies.

#### GTS_DK

##### New Member
Hi, I have a MSP430f149 and want to use the SPI port to communicate with a gyroscope. But I am not sure if the Gyroscope can communicate with my MSP430. The gyroscope sends out a 12 bit data length. and the MSP430 can only recieve 7- or 8-bit data length.
So does anyone know if it is possible to use the gyroscope on my msp430 ? and how do I do that? Furthermore i am not sure how to read from SPI port.

The datasheet for the gyroscope:
and the MSP430 Userguide
https://www.ti.com/litv/pdf/slau049f

I hope you guys can help me.

#### DirtyLude

##### Well-Known Member
I don't have experience with the 1xx series, I've been using the 2xx series, but you can't do 12 bit with the hardware SPI. I've managed to do 16bit, but that's 2 8bit transfers linked together.

SPI is exceedingly simple to bit-bang. Here's a function for you from a Code Composer program I wrote a while ago. It's for 16bit, but you can see how to change it to 12bit pretty easily. It also may need to be modified based on the slave device you are talking to. SPI doesn't have a hard standard and there can be some differences.

Code:
#define RFM_REN      P3REN
#define RFM_DIR      P3DIR
#define RFM_IN       P3IN
#define RFM_OUT      P3OUT

#define RFM_CS_PIN   (1<<7)
#define RFM_SCL_PIN  (1<<3)
#define RFM_MISO_PIN (1<<2)
#define RFM_MOSI_PIN (1<<1)
#define RFM_NIRQ_PIN (1<<6)

unsigned int spi_shift16(unsigned int data)
{
unsigned int result = 0;
unsigned char width;
RFM_OUT &= ~(RFM_SCL_PIN);             //init clock low
RFM_OUT &= ~(RFM_CS_PIN);             //begin transmit - cs low
for (width=0; width<16; width++)
{
if (data & 0x8000)
{   //check bit and set data line accordingly
RFM_OUT |= RFM_MOSI_PIN;
}
else
{
RFM_OUT &= ~(RFM_MOSI_PIN);
}
RFM_OUT |= RFM_SCL_PIN;          //end bit - clock high
result <<= 1;
if((RFM_IN & RFM_MISO_PIN) == RFM_MISO_PIN)
result |= 0x00001;
RFM_OUT &= ~(RFM_SCL_PIN);        //begin bit - clock low
data <<= 1;
}
RFM_OUT |= RFM_CS_PIN;               //end transmit
return result;
}

Last edited:
Status
Not open for further replies.

Replies
1
Views
1K
Replies
2
Views
1K
Replies
3
Views
3K
Replies
13
Views
2K
Replies
1
Views
2K