Tool/software: Code Composer Studio
I am using the ADXL345 with msp430g2553 due to I2C, but it not working, I put the pull up resistor of 10 K and remove the jumper of the green led, but not working yet. I was studyn user guide for I2C and i was review some examples and i think that the code must work but not. Help me please.
The code:
#include <msp430g2553.h>
#define NUM_BYTES_TX 2
#define NUM_BYTES_RX 6
#define ADXL_345 0x53
int RXByteCtr, x1,y1,z1; // enables repeated start when 1
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];
void Setup_TX(unsigned char);
void Setup_RX(unsigned char);
void Transmit(unsigned char,unsigned char);
void TransmitOne(unsigned char);
void Receive(void);
void Setup_UART();
void UARTSendArray(unsigned char *TxArray, unsigned char ArrayLength);
void UARTSendInt(unsigned int x);
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
// LED
P1DIR |= BIT0;
P1OUT &= ~BIT0;
// UART
BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1MHz
DCOCTL = CALDCO_1MHZ; // Set DCO to 1MHz
// ADXL345
P1SEL |= BIT6 + BIT7; // Assign I2C pins to USCI_B0
P1SEL2 |= BIT6 + BIT7; // Assign I2C pins to USCI_B0
// Init sequence for ADXL345
//Transmit process
Setup_TX(ADXL_345);
Transmit(0x2D,0x00); // STUCK
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
//Transmit process
Setup_TX(ADXL_345);
Transmit(0x2D,0x10);
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
//Transmit process
Setup_TX(ADXL_345);
Transmit(0x2D,0x08);
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
Setup_TX(ADXL_345);
RPT_Flag = 1;
Transmit(0x31,0x00); // Range Select at add 0x31 write 0x00 for 2g(default)/ 0x01 for 4g/ 0x02 for 8g/ 0x03 for 16g
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
long long i;
while(1){
// loop to measure completion time
for(i=0;i<1000;i++) {
//Transmit process
Setup_TX(ADXL_345);
TransmitOne(0x32); // Request Data from ADXL345 in 2g Range 10Bit resolution
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
//Receive process
Setup_RX(ADXL_345);
Receive();
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
x1 = (((int)RxBuffer[1]) << 8) | RxBuffer[0];
y1 = (((int)RxBuffer[3]) << 8) | RxBuffer[2];
z1 = (((int)RxBuffer[5]) << 8) | RxBuffer[4];
if ((x1 > 128) || (y1 > 128) || (x1 < -128) || (y1 < -128)) {
P1OUT |= BIT0; // red led on
}
else {
P1OUT &= ~BIT0; // red led off
}
}
}
__bis_SR_register( LPM3_bits + GIE );
}
#pragma vector = USCIAB0TX_VECTOR
__interrupt void USCIAB0TX_ISR(void)
{
if(RX == 1){ // Master Recieve?
RXByteCtr--; // Decrement RX byte counter
if (RXByteCtr)
{
*PRxData++ = UCB0RXBUF; // Move RX data to address PRxData
}
else
{
UCB0CTL1 |= UCTXSTP; // No Repeated Start: stop condition
*PRxData++ = UCB0RXBUF; // Move final RX data to PRxData
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
}}
else{ // Master Transmit
if (TXByteCtr) // Check TX byte counter
{
TXByteCtr--; // Decrement TX byte counter
UCB0TXBUF = MSData[TXByteCtr]; // Load TX buffer
IFG2|= UCB0TXIFG; // Clear USCI_B0 TX int flag
}
else
{
UCB0CTL1 |= UCTXSTP; // I2C stop condition
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
}
}
}
void Setup_TX(unsigned char Dev_ID){
_DINT();
RX = 0;
IE2 &= ~UCA0RXIE; // Disable USCI_A0 RX interrupt
IE2 &= ~UCB0RXIE;
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent// Disable RX interrupt
UCB0CTL1 |= UCSWRST; // Enable SW reset
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
UCB0BR0 = 10; // fSCL = SMCLK/12 = ~100kHz
UCB0BR1 = 0;
UCB0I2CSA = Dev_ID; // Slave Address is 048h
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
IE2 |= UCB0TXIE; // Enable TX interrupt
}
void Setup_RX(unsigned char Dev_ID){
_DINT();
RX = 1;
IE2 &= ~UCA0RXIE; // Disable USCI_A0 RX interrupt
IE2 &= ~UCB0TXIE;
UCB0CTL1 |= UCSWRST; // Enable SW reset
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
UCB0BR0 = 10; // fSCL = SMCLK/12 = ~100kHz
UCB0BR1 = 0;
UCB0I2CSA = Dev_ID; // Slave Address is 048h
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
IE2 |= UCB0RXIE; // 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 (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
__bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
}
void TransmitOne(unsigned char Reg_ADD){
MSData[0]= Reg_ADD;
TXByteCtr = 1; // Load TX byte counter
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
__bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
}
void Receive(void){
PRxData = (unsigned char *)RxBuffer; // Start of RX buffer
RXByteCtr = NUM_BYTES_RX; // Load RX byte counter
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
UCB0CTL1 |= UCTXSTT; // I2C start condition
__bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
}
THANKS!!