Part Number:MSP430FR5969
Tool/software: Code Composer Studio
Hello,
I'm having difficulty configuring the ADXL345 accelerometer with the MSP430FR5969 Launchpad.
I've been following along with a similar solution posted here. I believe the hook-up is identical:
GND -> GND
VCC -> VCC
CS -> VCC
SDO -> GND
SDA -> P1.7 (with 4.7k pullup resistor)
SCL -> P1.6 (with 4.7k pullup resistor)
My buffer never seems to fill after running Debug. So I imagine I'm initializing something incorrectly.
One of my peers mentioned that I may need to add a clock initialization procedure?
I've tested the ADXL345 on an Arduino and it works fine, so I don't believe it's a hardware problem.
Any advice it welcomed, thank you.
I've attached my modification of the code here:
#include <msp430.h>
#define NUM_BYTES_TX 2
#define NUM_BYTES_RX 6
#define ADXL_345 0x53
int RXByteCtr, x, y, z;
volatile unsigned char RxBuffer[6]; // Allocate 6 byte of RAM
unsigned char *PRxData; // Pointer to RX data
unsigned char TXByteCtr, RX = 0;
unsigned char MSData[2];
// Functions for I2C
void Setup_TX(unsigned char);
void Setup_RX(unsigned char);
void Transmit(unsigned char,unsigned char);
void TransmitOne(unsigned char);
void Receive(void);
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop WDT
// LED
P1OUT &= ~BIT0; // Clear P1.0 output latch
P1DIR |= BIT0; // P1.0 = red LED
P1OUT |= BIT0; // P1.0 = red LED
P1SEL1 |= BIT6 + BIT7; // I2C pins
// Disable the GPIO power-on default high-impedance mode to activate
// previously configured port settings
PM5CTL0 &= ~LOCKLPM5;
// Init sequence for ADXL345
Setup_TX(ADXL_345);
Transmit(0x2D,0x00);
while (UCB0CTLW1 & UCTXSTP); // Ensure stop condition got sent
Setup_TX(ADXL_345);
Transmit(0x2D,0x10);
while (UCB0CTLW1 & UCTXSTP); // Ensure stop condition got sent
Setup_TX(ADXL_345);
Transmit(0x2D,0x08);
while (UCB0CTLW1 & UCTXSTP); // Ensure stop condition got sent
while(1){
// Transmit process
Setup_TX(ADXL_345);
TransmitOne(0x32); // Request Data from ADXL345 in 2g Range 10Bit resolution
while (UCB0CTLW1 & UCTXSTP); // Ensure stop condition got sent
// Receive process
Setup_RX(ADXL_345);
Receive();
while (UCB0CTLW1 & UCTXSTP); // Ensure stop condition got sent
x = (((int)RxBuffer[1]) << 8) | RxBuffer[0];
y = (((int)RxBuffer[3]) << 8) | RxBuffer[2];
z = (((int)RxBuffer[5]) << 8) | RxBuffer[4];
// Now we have x,y,z reading.
// Below red LED is on, if x or y angle is more then 45 or less then -45 degree.
if ((x > 128) || (y > 128) || (x < -128) || (y < -128)) {
P1OUT |= BIT0; // red LED on
}
else {
P1OUT &= ~BIT0; // red LED off
}
__delay_cycles(1000000); // delay 1 sec
}
}
//-------------------------------------------------------------------------------
// I2C
//-------------------------------------------------------------------------------
#pragma vector = USCI_B0_VECTOR
__interrupt void USCI_B0_ISR(void)
{
if(RX == 1){ // Master Recieve?
RXByteCtr--; // Decrement RX byte counter
if (RXByteCtr)
{
*PRxData++ = UCB0RXBUF; // Move RX data to address PRxData
}
else
{
UCB0CTLW1 |= UCTXSTP; // No Repeated Start: stop condition
*PRxData++ = UCB0RXBUF; // Move final RX data to PRxData
__bic_SR_register_on_exit(LPM0_bits | GIE); // Exit LPM0
}}
else{ // Master Transmit
if (TXByteCtr) // Check TX byte counter
{
TXByteCtr--; // Decrement TX byte counter
UCB0TXBUF = MSData[TXByteCtr]; // Load TX buffer
}
else
{
UCB0CTLW1 |= UCTXSTP; // I2C stop condition
UCB0IFG &= ~UCTXIFG; // Clear USCI_B0 TX int flag
__bic_SR_register_on_exit(LPM0_bits | GIE); // Exit LPM0
}
}
}
void Setup_TX(unsigned char Dev_ID){
_DINT();
RX = 0;
UCB0IE &= ~UCRXIE; // Disable USCI_B0 RX interrupt
while (UCB0CTLW1 & UCTXSTP); // Ensure stop condition got sent// Disable RX interrupt
UCB0CTLW1 |= UCSWRST; // Enable SW reset
UCB0CTLW0 = UCMODE_3 | UCMST | UCSYNC; // I2C Master, synchronous mode
UCB0CTLW1 = UCSSEL_2; // Use SMCLK, keep SW reset
UCB0BR0 = 0x0008; // baudrate = SMCLK / 8
UCB0TBCNT = 0x0006; // number of bytes to be received
UCB0I2CSA = Dev_ID; // Slave Address is 048h
UCB0CTLW1 &= ~UCSWRST; // Clear SW reset, resume operation
UCB0IE |= UCTXIE | UCNACKIE | UCBCNTIE; // Enable TX interrupt
}
void Setup_RX(unsigned char Dev_ID){
_DINT();
RX = 1;
UCB0IE &= ~UCRXIE; // Disable USCI_B0 RX interrupt
UCB0CTLW1 |= UCSWRST; // Enable SW reset
UCB0CTLW0 = UCMODE_3 | UCMST | UCSYNC; // I2C Master, synchronous mode
UCB0CTLW1 = UCSSEL_2; // Use SMCLK, keep SW reset
UCB0BR0 = 0x0008; // baudrate = SMCLK / 8
UCB0TBCNT = 0x0006;
UCB0I2CSA = Dev_ID; // Slave Address is 048h
UCB0CTLW1 &= ~UCSWRST; // Clear SW reset, resume operation
UCB0IE |= UCRXIE | UCNACKIE | UCBCNTIE; // Enable RX interrupt
}
void Transmit(unsigned char Reg_ADD,unsigned char Reg_DAT){
MSData[1]= Reg_ADD;
MSData[0]= Reg_DAT;
TXByteCtr = NUM_BYTES_TX; // Load TX byte counter
while (UCB0CTLW1 & UCTXSTP); // Ensure stop condition got sent
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
while (1)
{
__delay_cycles(2000);
while (UCB0CTLW1 & UCTXSTP); // Ensure stop condition got sent
UCB0CTLW1 |= UCTXSTT; // I2C start condition
}
}
void TransmitOne(unsigned char Reg_ADD){
MSData[0]= Reg_ADD;
TXByteCtr = 1; // Load TX byte counter
while (UCB0CTLW1 & UCTXSTP); // Ensure stop condition got sent
UCB0CTLW1 |= UCTR + UCTXSTT; // I2C TX, start condition
while (1)
{
__delay_cycles(2000);
while (UCB0CTLW1 & UCTXSTP); // Ensure stop condition got sent
UCB0CTLW1 |= UCTXSTT; // I2C start condition
}
}
void Receive(void){
PRxData = (unsigned char *)RxBuffer; // Start of RX buffer
RXByteCtr = NUM_BYTES_RX; // Load RX byte counter
while (UCB0CTLW1 & UCTXSTP); // Ensure stop condition got sent
UCB0CTLW1 |= UCTXSTT; // I2C start condition
while (1)
{
__delay_cycles(2000);
while (UCB0CTLW1 & UCTXSTP); // Ensure stop condition got sent
UCB0CTLW1 |= UCTXSTT; // I2C start condition
}
}