This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Tool/software: Code Composer Studio
I wrote a code for I2C communication between MSP430FR5969 the accelerometer ADXL345.
When I run the code it works one time. I mean I receive only one data one time from the ADXL345. After that It does not works any more. Someone could give any idea about that?
Below the entire code:
#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];
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;
P1DIR |= BIT0;
//P1OUT |= BIT0;
P1OUT &= ~BIT0;
//P1SEL |= BIT6 | BIT7; // ADXL345
P1SEL1 |= BIT6 | BIT7; // ADXL345
//P1SEL0 &= ~(BIT6 | BIT 7); // ADXL345
//P1SEL2 |= BIT6 | BIT7; // ADXL3450
PM5CTL0 &= ~LOCKLPM5;
// Init sequence for ADXL345
Setup_TX(ADXL_345);
Transmit(0x2D,0x00);//00
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
Setup_TX(ADXL_345);
Transmit(0x2D,0x10);//16
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
Setup_TX(ADXL_345);
Transmit(0x2D,0x08);//08
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
//P1OUT ^=BIT0;
while(1){
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
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(10); // delay 1 sec
//UCB0CTLW1 &= ~UCTXSTP;
}//Fin While(1)
}//FIN MAIN
#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{
UCB0CTL1 |= UCTXSTP; // No Repeated Start: stop condition
*PRxData++ = UCB0RXBUF; // Move final RX data to PRxData
__bic_SR_register_on_exit(LPM0_bits); // Exit LPM0
}}
else{ // Master Transmit
if (TXByteCtr){ // Check TX byte counter
TXByteCtr--; // Decrement TX byte counter
UCB0TXBUF = MSData[TXByteCtr]; // Load TX buffer
}else{
UCB0CTL1 |= UCTXSTP; // I2C stop condition
UCB0IFG &= ~UCTXIFG; // Clear USCI_B0 TX int flag //UCTXIFG0 is set when UCBxTXBUF is empty in master or in slave mode
__bic_SR_register_on_exit(LPM0_bits); // Exit LPM0
}
}//fin if else
}
/////////////////////////
void Setup_TX(unsigned char Dev_ID){
_DINT();
RX=0;
UCB0IE &=~UCRXIE;
UCB0CTLW1 &= ~UCTXSTP;//MOIIIIIIIII
while(UCB0CTL1 & UCTXSTP);
//UCB0CTLW0 |= UCSWRST;
//UCB0CTLW0 |= UCMODE_3 | UCMST | UCSSEL_2 | UCSYNC;
//UCB0CTLW1 = UCASTP_2 | UCSWRST; // Automatic stop generated
UCB0CTLW0 |= UCSWRST;
UCB0CTLW0 |= UCMODE_3 | UCMST | UCSYNC |UCSSEL_2 ;
UCB0CTLW1 = UCASTP_2; // Automatic stop generated
UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz
UCB0BR1 =0;
UCB0TBCNT=0x0006;
UCB0I2CSA = Dev_ID;
UCB0CTL1 &= ~UCSWRST;
UCB0IE |= UCTXIE;// | UCNACKIE | UCBCNTIE;
}
void Setup_RX(unsigned char Dev_ID){
_DINT();
RX = 1;
UCB0IE &=~UCTXIE;
//UCB0CTLW0 |= UCSWRST; // Enable SW reset
//UCB0CTLW0 |= UCMODE_3 | UCMST | UCSSEL_2 | UCSYNC;
//UCB0CTLW1 = UCASTP_2 | UCSWRST; // Use SMCLK, keep SW reset
UCB0CTLW0 |= UCSWRST;
UCB0CTLW0 |= UCMODE_3 | UCMST | UCSYNC | UCSSEL_2;
UCB0CTLW1 = UCASTP_2; // Automatic stop generated
UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz
UCB0BR1 = 0;
//UCB0TBCNT=0x0006;
UCB0I2CSA = Dev_ID; // Slave Address is 048h
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
UCB0IE |= UCRXIE; // | UCNACKIE | UCBCNTIE;
}
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
UCB0CTLW0 |= UCTR | UCTXSTT; // I2C TX, start condition
__bis_SR_register(LPM0_bits | GIE);
/*while(1){
__delay_cycles(2000);
while (UCB0CTL1 & UCTXSTP);
UCB0CTL1 |= UCTXSTT;
__bis_SR_register(LPM0_bits | GIE);
}*/
}
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
UCB0CTLW0 |= UCTR | UCTXSTT; // I2C TX, start condition
__bis_SR_register(LPM0_bits | GIE);
/*while(1){
__delay_cycles(2000);
while (UCB0CTL1 & UCTXSTP);
UCB0CTL1 |= UCTXSTT;
}*/
}
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
UCB0CTLW0 |= UCTXSTT;
UCB0CTLW0 &= ~UCTR;
//while(UCB0IFG & UCNACKIFG);
__bis_SR_register(LPM0_bits | GIE);
}
Hello Darwin,
A great resource to use when attempting to find the bug inside of a project is to compare it to our published example code to see where the differences are. Here is a link to MSP430FR5969 example code ( http://dev.ti.com/tirex/#/?link=Software%2FMSP430Ware%2FDevices%2FMSP430FR5XX_6XX%2FMSP430FR5969%2FPeripheral%20Examples%2FRegister%20Level ) where you can find multiple I2C example projects. Although the example projects have to be general and can't directly be a plug-in and fit for all implementations, they are a good tool to see code flow, proper initialization, and use cases.
Another good resource is an Application Report that focuses on solutions to common serial communication issues on the MSP430 MCUs ( http://www.ti.com/lit/an/slaa734/slaa734.pdf ).
Hopefully this gives you a solid starting point to finding your issue. Please reach out with any updates as they arise!
Best regards,
Matt Calvo
**Attention** This is a public forum