Hello everyone,
I am a newbie to MSP430 and SPI protocol. I read a lot about SPI online and also in the User Guide of MCU. The problem I am facing is described below;
I am interfacing ADXL345 Accelerometer with MSP430F2274 via 3 wire SPI. I think I am doing all the initializing correctly, but I am not getting any useful data back.
I am trying to read the device ID of ADXL345 but I keep on getting 0xF2 instead of 0xE5.
This is the data sheet for ADXL345; http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf
ADXL345 takes 16 bits of data at one time but the MCU can only clock out 8 bits in one transmission, I think this creates a timing difference in the communication and hence
I don’t get what I want. Below is the code I am trying to use:
#include "msp430x22x4.h"
volatile unsigned char Data;
volatile int i;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P3SEL |= 0x31; // P3.0,P3.4,P3.5 USCI_A0 option select
P3DIR |= 0x40; // P3.6 output direction
P4DIR |= BIT3; // Used to provide power to the Accelerometer
P4OUT &= ~BIT3;
// SPI initialization
UCA0CTL0 |= UCCKPH + UCCKPL + UCMSB + UCMST + UCSYNC; // 3-pin, 8-bit SPI master
UCA0CTL1 |= UCSSEL_2; // Select SMCLK as CLK
UCA0BR0 |= 0x02;
UCA0BR1 = 0;
UCA0MCTL = 0;
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
P4OUT |= BIT3; // Turn the Accelerometer on
while(1)
{
P3OUT &= ~0x40; // Chip select active low
for(i = 0xFFFF; i > 0; i--); // Delay
IFG2 &= ~UCA0RXIFG; // Clear int flag
while ((IFG2 & UCA0TXIFG) == 0);
UCA0TXBUF = 0xC0; // Read Dev ID
IFG2 &= ~UCA0RXIFG;
//while ((IFG2 & UCA0TXIFG) == 0); // TXBUF ready?
UCA0TXBUF = 0x45;
while ((IFG2 & UCA0TXIFG) == 0); // TXBUF ready?
P3OUT |= 0x40; // End Transmission
while ((IFG2 & UCA0RXIFG) == 0); // RXBUF ready?
Data = UCA0RXBUF; // Move value
}
}
Somebody please help me on this.
Thanks.