Measurement with ADXL345 and MSP430F5438A per 4 wire SPI

Discussions on how to get your MSP JTAG programmer up and running.

Moderator: phalanx

Post Reply
roc Sa
Posts: 1
Joined: Fri May 10, 2013 2:21 am

Measurement with ADXL345 and MSP430F5438A per 4 wire SPI

Post by roc Sa » Fri May 10, 2013 6:55 am

Hi!

I found the Basic example Code from Sparkfun based on Arduino and I am trying to adjust the Code for my MSP430, but I have got some understandment problems. I defined the Buffer, but I am getting confused with the data of 10 bits from the accelerator.

I added a line for every value in the Interruptroutine, after putting the received data to the measurement variable.
Is this correct? The idea was to follw the Basic Code, especially the lines below.

//This buffer will hold values read from the ADXL345 registers.

char values[10];

//These variables will be used to hold the x,y and z axis accelerometer values.

int x,y,z;

(Copied from the Basis Code)



I do not how I can built the stuff down below into my code. Can somebody give me some help?



//The ADXL345 gives 10-bit acceleration values, but they are stored as bytes (8-bits). To get the full value, two bytes must be combined for each axis.

//The X value is stored in values[0] and values[1].

x = ((int)values[1]<<8)|(int)values[0];

//The Y value is stored in values[2] and values[3].

y = ((int)values[3]<<8)|(int)values[2];

//The Z value is stored in values[4] and values[5].

z = ((int)values[5]<<8)|(int)values[4];



I want to know how to implement the shift operations and afterwards the reading operation for my MSP430 in my existing code. Can I put the

shift operation in the interrupt routine?



Thanks and greetings

#include "msp430x54xA.h"
#include "hal_MSP-EXP430F5438.h"
#include "hal_lcd.h"
#include <stdint.h>
#include "hal_pmm.h"

#define SYSTEM_CLOCK 23986176

#define USB_PORT_OUT P5OUT
#define USB_PORT_SEL P5SEL
#define USB_PORT_DIR P5DIR
#define USB_PORT_REN P5REN
#define USB_PIN_TXD BIT6
#define USB_PIN_RXD BIT7
#define USB_PORT_BAUDRATE 57600

#define UART_A3_PSEL P10SEL
#define UART_A3_PDIR P10DIR
#define UART_A3_TXD_MASK BIT4
#define UART_A3_RXD_MASK BIT5

#define UART_A3_BAUDRATE 57600

char rxBuffer[256]; //Buffer to keep received messages/responses/events from module
char rxReadIndex = 0;
char rxWriteIndex = 0;

volatile char dataReady = 0; //variable to declare is message is completely received (to be implemented)

volatile unsigned int i; // volatile to prevent optimization
unsigned int SetVCore (unsigned char level);
void initSystemClock();
//unsigned char contrast = 0x66;
//unsigned char backlight = 8;
void lcdStart();
void portsInit();

unsigned char MST_Data = 0x80; // Initialize data values
unsigned char MST_Data2 = 0xFF;
unsigned char SLV_Data = 0xE5; // Slave-Data, Roko
unsigned char tempvar;
int j = 0;

// SPI-Configurationfunction
void spi_init ();

// Code added by Roko
uint8_t ADRESS_DATA_FORMAT=0x31; // 0x31; DATA_FORMAT, wake up from standby
uint8_t ADRESS_BW_RATE=0x2C; // Address
uint8_t ADRESS_FIFO_CTL=0x80; // Address
uint8_t ADRESS_POWER_CTRL=0x2D; // Address


// Code added by Roko
uint8_t DATA_FORMAT=0x08; // 100 Hz output rate // 0x0A //SELF_TEST | SPI | INT_INVERT | 0 | FULL_RES | Justify | Range
uint8_t BW_RATE=0x0A; // 0 | 0 | 0 | LOW_POWER | Rate | Rate | Rate Rate
uint8_t FIFO_CTL=0x80; // FIFO_MODE | FIFO_MODE | Tigger | Samples | Samples | Samples | Samples | Samples
uint8_t POWER_CTRL=0x08; // 0 | 0 | Link | AUTO_SLEEP | Measure | Sleep | Wakeup
unint8_t POWER_CTRLstop=0x00; // setting Measurement Bit to 0 to stop measurement


// Variables for measurement
uint8_t DATAX0=0x32; // adress
uint8_t DATAX1=0x33; // adress
uint8_t DATAY0=0x34; // adress
uint8_t DATAY1=0x35; // adress
uint8_t DATAZ0=0x36; // adress
uint8_t DATAZ1=0x37; // adress

// Results measurements
uint8_t X0;
uint8_t X1;
uint8_t Y0;
uint8_t Y1;
uint8_t Z0;
uint8_t Z1;



void main(void)
{

WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer

//initSystemClock();

// It's 4 wires, but in reality 3-wire mode for MSP430, since the CS is manually controlled. Still full duplex (and not half)
P10DIR |= 0x40; // CS pin.
P10SEL |= 0x0E; // or BIT1 + BIT2 + BIT3;

UCB3CTL1 |= UCSWRST; // **Put state machine in reset** !!!
UCB3CTL0 |= UCMST + UCSYNC + UCCKPL + UCMSB; // + UCCKPH; // 3-pin, 8-bit SPI master

//UCB3CTL1 |= UCSSEL_1; // ACLK = ~32.768kHz,
UCB3CTL1 |= UCSSEL_2; // SMCLK

//UCB3BR0 = 0; //
UCB3BR0 = 0x02; //
UCB3BR1 = 0; //

UCB3CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCB3IE |= UCRXIE; // Enable USCI_B3 RX interrupt

P10OUT |= 0x40; //set high (CS active low)

__delay_cycles(100); // Wait for slave to initialize


//******* ROUTINE TO SEND //TEST_ID
// P10OUT &= ~0x40; //select slave
//
// while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
// UCB3TXBUF = MST_Data; // Transmit first character
//
//

spi_init();



__bis_SR_register(GIE); // enable global interrupts


while(1)
{ }

}


#pragma vector=USCI_B3_VECTOR
__interrupt void USCI_B3_ISR(void)
{
switch(__even_in_range(UCB3IV,12))
{
case 0: break; // Vector 0 - no interrupt
case 2: // Vector 2 - RXIFG

P10OUT&=~0x40;


// sending the adress to start conversation with POWER_CTL


while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
UCB3TXBUF=ADRESS_POWER_CTRL; // Transmit first character, Address(ROKO)
while (!(UCB3STAT&UCBUSY)==0);

P10OUT|=0x40;


__delay_cycles(40);
//---------------------------------------
// sending 0x08 to the POWER_CTL Register to start the measurement
P10OUT&=~0x40;

while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
UCB3TXBUF=POWER_CTRL; // Transmit first character, Address(ROKO)
while (!(UCB3STAT&UCBUSY)==0);


P10OUT|=0x40;

__delay_cycles(40);


//****Measurements***************

UCB3TXBUF=DATAX0; // USCI_B0 TX buffer ready?
while (!(UCB3IFG&UCTXIFG));
UCB3TXBUF=0xFF;
while (!(UCB3IFG&UCRXIFG)); // USCI_B0 RX buffer ready?
X0=UCB3RXBUF;
buffer[1]=X1; // Putting Data from Variable X1 into the buffer
while (!(UCB3STAT&UCBUSY)==0);


// P10OUT|=0x40;
// // __delay_cycles(40);
// P10OUT&=~0x40;


// UCB3TXBUF = MST_Data2;
// Transmit first character, Address(ROKO)

UCB3TXBUF=DATAX1; // Transmit first character, Address(ROKO)
while (!(UCB3IFG&UCTXIFG));
UCB3TXBUF=0xFF; // Dummy-Byte
while (!(UCB3IFG&UCRXIFG)); // USCI_B0 RX buffer ready??
X1=UCB3RXBUF;
while (!(UCB3STAT&UCBUSY)==0);
//
// P10OUT&=~0x40;
// // __delay_cycles(40);
// P10OUT|=0x40;

while (!(UCB3IFG&UCTXIFG));
UCB3TXBUF=DATAY0; // Transmit first character, Address(ROKO)
while (!(UCB3IFG&UCTXIFG));
UCB3TXBUF=0xFF; // Dummy-Byte
while (!(UCB3IFG&UCRXIFG)); // USCI_B0 TX buffer ready?
Y0=UCB3RXBUF;
buffer[2]=Y0; // Putting Data from Variable Y0 into the buffer
while (!(UCB3STAT&UCBUSY)==0);

//
// P10OUT&=~0x40;
// // __delay_cycles(40);
// P10OUT|=0x40;

while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
UCB3TXBUF=DATAY1;
while (!(UCB3IFG&UCTXIFG));
UCB3TXBUF=0xFF; // Dummy-Byte
while (!(UCB3IFG&UCRXIFG)); // USCI_B0 RX buffer ready?
Y1=UCB3RXBUF;
buffer[3]=Y1; // Putting Data from Variable Y1 into the buffer
while (!(UCB3STAT&UCBUSY)==0);

// Transmit first character, Address(ROKO)
//
// P10OUT&=~0x40;
// // __delay_cycles(40);
// P10OUT|=0x40;

while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
UCB3TXBUF=DATAZ0; // Transmit first character, Address(ROKO)
while (!(UCB3IFG&UCTXIFG));
UCB3TXBUF=0xFF; // Dummy-Byte
while (!(UCB3IFG&UCRXIFG)); // USCI_B0 RX buffer ready?
Z0=UCB3RXBUF;
buffer[4]=Z0; // Putting Data from Variable Z0 into the buffer
while (!(UCB3STAT&UCBUSY)==0);

// P10OUT|=0x40;
// P10OUT&=~0x40;
// __delay_cycles(40);


while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
UCB3TXBUF=DATAZ1; // Transmit first character, Address(ROKO)
while (!(UCB3IFG&UCTXIFG));
UCB3TXBUF=0xFF; // Dummy-Byte
while (!(UCB3IFG&UCRXIFG)); // USCI_B0 RX buffer ready?
Z1=UCB3RXBUF;
buffer[5]=Z1; // Putting Data from Variable Z1 into the buffer
while (!(UCB3STAT&UCBUSY)==0);

P10OUT|=0x40;


// while (!(UCB3IFG&UCTXIFG)); // USCI_A0 TX buffer ready?
// tempvar = UCB3RXBUF;
// if (tempvar == SLV_Data) // Test for correct character RX'd
// tempvar = 0xAA; //tempvar=0xAA;
// else
// UCB3TXBUF = MST_Data2; // Send next value (Roko, sending Dummy byte)
// j++; // Zeile zum Debuggen


// sending the adress to start conversation with POWER_CTL


while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
UCB3TXBUF=ADRESS_POWER_CTRL; // Transmit first character, Address(ROKO)
while (!(UCB3STAT&UCBUSY)==0);

P10OUT|=0x40;


__delay_cycles(40);

//---------------------------------------


// sending 0x08 to the POWER_CTL Register to start the measurement

P10OUT&=~0x40;

while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
UCB3TXBUF=POWER_CTRLstop; // Transmit first character, Address(ROKO)
while (!(UCB3STAT&UCBUSY)==0);


__delay_cycles(40); // Add time between transmissions to
// make sure slave can process information (seems to avoid putting CS to high)
// if(j==2)
// tempvar++;
break;
case 4: break; // Vector 4 - TXIFG
default: break;
}
}



void spi_init ()
{

//******** Configuration ***** Roko

while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
UCB3TXBUF=ADRESS_DATA_FORMAT; // Transmit first character, Address(ROKO)
while (!(UCB3STAT&UCBUSY)==0);


while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
UCB3TXBUF=DATA_FORMAT; // Transmit second character, Address(ROKO)
while (!(UCB3STAT&UCBUSY)==0);

P10OUT|=0x40;

__delay_cycles(40);
//-------------------------------

P10OUT&=~0x40;

while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
UCB3TXBUF=ADRESS_BW_RATE; // Transmit first character, Address(ROKO)
while (!(UCB3STAT&UCBUSY)==0);

while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
UCB3TXBUF=BW_RATE; // Transmit second character, Address(ROKO)
while (!(UCB3STAT&UCBUSY)==0);


P10OUT|=0x40;

__delay_cycles(40);
//--------------------------------
P10OUT&=~0x40;

while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
UCB3TXBUF=ADRESS_FIFO_CTL; // Transmit first character, Address(ROKO)
while (!(UCB3STAT&UCBUSY)==0);


while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
UCB3TXBUF=FIFO_CTL; // Transmit second character, Address(ROKO)
while (!(UCB3STAT&UCBUSY)==0);


P10OUT|=0x40;

__delay_cycles(40);
//---------------------------------

P10OUT&=~0x40;

while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
UCB3TXBUF=ADRESS_POWER_CTRL; // Transmit first character, Address(ROKO)
while (!(UCB3STAT&UCBUSY)==0);

P10OUT|=0x40;


__delay_cycles(40);
//---------------------------------------

P10OUT&=~0x40;

while (!(UCB3IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
UCB3TXBUF=POWER_CTRL; // Transmit first character, Address(ROKO)
while (!(UCB3STAT&UCBUSY)==0);


P10OUT|=0x40;

__delay_cycles(40);

//----------------------------------------


};

Post Reply