MSP430FR5969: Poblem with SPI MSP430FR5969

Part Number: MSP430FR5969

In this case i work with spi in msp430fr5969 as the master and there are two slaves adxl362 as slave and em4325 rfid chip as second slave in this case i use the code below. When i debug step by step it is working but when i run the program continously the programme does not work i did not receive anything and other case, i also try only with one slave adxl362 where the code is working without while with step by step debugging but when there is while loop it is not working i did not receive anything i really need help please. 

#include <msp430.h>
#include <stdio.h>

void clockInit(void);
void spiInit(void);
void UARTInit(void);
void delay(unsigned int ms);
unsigned char Get_register_value(unsigned char address);
void Get_register_Burst(unsigned char address, unsigned char Rxbuf[], unsigned int BytesNum);
void Set_register_value(unsigned char address, unsigned char regValue);
void binaryToAscii(unsigned char binaryData, char asciiRepresentation[3]);
void readFIFO();
void setuprfint(void);

unsigned int const BytesNum = 3;
unsigned char Rx_value, Tx_value,x_data, y_data, z_data;
unsigned char X_data[BytesNum], Y_data[BytesNum], Z_data[BytesNum];
unsigned char rx_value;
unsigned char Rxbuf[BytesNum];
unsigned int i;
unsigned int zdata;
signed int zdatas;
float zvalue;
unsigned int sensorData;
unsigned int valuetowrite[2];
volatile int fifo_buffer[170];
volatile int fifo_buffer_X[170];
volatile int fifo_buffer_Y[170];
volatile int fifo_buffer_Z[1];
unsigned int i=0;
unsigned int rfintflag=0;
unsigned int axis=0;
unsigned int stepCounter=0;
unsigned int x_rawvalue;
unsigned int y_rawvalue;
unsigned int z_rawvalue;
unsigned int firstbyte[1];
unsigned int secondbyte[1];
unsigned int emstatus;

int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
int i=0;
int count=0;


UCA1CTLW0 &= ~UCSWRST; //Takes UART A1 out of sw reset
PM5CTL0 &= ~LOCKLPM5;

clockInit();
spiInit();
UARTInit();
setuprfint();
/*
Set_register_value(0x20, 0x1F);//THRESG_ACT_L
Set_register_value(0x21,0x00);//THRESH_ACT_H
Set_register_value(0x22, 50);//TIME_ACT

Set_register_value(0x23, 0x09);//THRESH_INACT_LOW
Set_register_value(0x24, 0x00);//THRESH_INACT_H
Set_register_value(0x25, 0x01);//TIME_INACT_L
Set_register_value(0x26, 0x00);//TIME_INACT_H

Set_register_value(0x27, 0x03);//ACT_INACT_CTL
*/
Set_register_value(0x29, 0xFF);//FIFO_SAMPLES
Set_register_value(0x28, 0x09);//FIFO_CONTROL,
Set_register_value(0x2A, 0x84);//INTMAP1 Data_ready mapped into INT1, failing edge
Set_register_value(0x2B, 0xC0);//INTMAP2 activity mapped into INT2, failing edge
Set_register_value(0x2C, 0x11);//FILTER_CTL, ODR = 400Hz,+-2g

//Get_register_value(0x28);//clear all interrupts*/
//Get_register_value(0x29);//clear all interrupts*/
//Get_register_value(0x2A);//clear all interrupts*/
//Get_register_value(0x2B);//clear all interrupts*/

Set_register_value(0x2D, 0x02);//POWER_CTL Measurement mode, WAKE UP

//__delay_cycles(100000);

while(1){
    P1OUT &= ~BIT5;
    // send read FIFO register command 0x0D
    while(!(UCB0IFG & UCTXIFG));
    UCB0TXBUF = 0x0D;
    // b15-b14 : 00-x 01-y 10-z 11-temp
    // b11-b0  : data
    // each fifo sample has 16 bits, 2 bits of axis information, 2 bits redundant, 12 bits of data
    // Send dummy byte and receive data for X axis
    while (!(UCB0IFG & UCTXIFG));
    UCB0TXBUF = 0x00;
    while (!(UCB0IFG & UCRXIFG));
//    fifo_buffer_X[i] = UCB0RXBUF;
    UCB0TXBUF = 0x00; // Send another dummy byte
//    while (!(UCB0IFG & UCRXIFG));
//    fifo_buffer_X[i] |= (UCB0RXBUF << 8); // Combine with the received byte
//    fifo_buffer_X[i] = (fifo_buffer_X[i] << 4) >> 4; // Shift left by 4 bits and then right by 4 bits to remove redundant bits
//    x_rawvalue = fifo_buffer_X[i];

    // Send dummy byte and receive data for Y axis
    while (!(UCB0IFG & UCTXIFG));
    UCB0TXBUF = 0x00;
    delay(10); // 10 ms delay, adjust as needed
    while (!(UCB0IFG & UCTXIFG));
//    fifo_buffer_Y[1] = UCB0RXBUF;
    UCB0TXBUF = 0x00; // Send another dummy byte
//    while (!(UCB0IFG & UCRXIFG));
//    fifo_buffer_Y[1] |= (UCB0RXBUF << 8); // Combine with the received byte
//    fifo_buffer_Y[1] = (fifo_buffer_Y[i] << 4) >> 4; // Shift left by 4 bits and then right by 4 bits to remove redundant bits
//    y_rawvalue = fifo_buffer_Y[i];

    // Send dummy byte and receive data for Z axis
    while (!(UCB0IFG & UCTXIFG));
    UCB0TXBUF = 0x00;
    while (UCB0STATW & UCBUSY);
    while (!(UCB0IFG & UCRXIFG));
    fifo_buffer_Z[1] = UCB0RXBUF;
    while (!(UCB0IFG & UCTXIFG));
    UCB0TXBUF = 0x00; // Send another dummy byte
    delay(10); // 10 ms delay, adjust as needed
    while (UCB0STATW & UCBUSY);
    while (!(UCB0IFG & UCRXIFG));
    fifo_buffer_Z[1] |= (UCB0RXBUF << 8); // Combine with the received byte
    fifo_buffer_Z[1] = (fifo_buffer_Z[1] << 4) >> 4; // Shift left by 4 bits and then right by 4 bits to remove redundant bits
               //    z_rawvalue = fifo_buffer_Z[i];

    // chip select high
    P1OUT |= BIT5;
//
//}
    //chip select low to start transmission
    P1OUT &= ~BIT3;

    // send write register file command
    while(!(UCB0IFG & UCTXIFG));
    UCB0TXBUF = 0xEF;
    while (UCB0STATW & UCBUSY);

    // send memory address to write
    while(!(UCB0IFG & UCTXIFG));
    UCB0TXBUF = 0x0001;
    while (UCB0STATW & UCBUSY);

    // Value to write to memory
    valuetowrite[0] = fifo_buffer_Z[1];
    valuetowrite[1] = fifo_buffer_Z[1];

    // Split the 16-bit value into two 8-bit values
    valuetowrite[0] >>=8;
    firstbyte[0] = valuetowrite[0];
    valuetowrite[1] = valuetowrite[1] & 0xFF;
    secondbyte[0] = valuetowrite[1];      // Extract lower 8 bits (0x34)

    // send value to write to the register file
    while(!(UCB0IFG & UCTXIFG));
    UCB0TXBUF = firstbyte[0];
    while (UCB0STATW & UCBUSY);

     // send value to write to the register file
     while(!(UCB0IFG & UCTXIFG));
     UCB0TXBUF = secondbyte[0];
     while (UCB0STATW & UCBUSY);

     //obtain EM4325 memory value
     while(!(UCB0IFG & UCTXIFG));
     UCB0TXBUF = 0x00;
     while (UCB0STATW & UCBUSY);
     while(!(UCB0IFG & UCTXIFG));
     UCB0TXBUF = 0x00;
     while (UCB0STATW & UCBUSY);
     while(!(UCB0IFG & UCRXIFG));
     emstatus=UCB0RXBUF;
     //   emregister[0]=UCB0RXBUF;
     //   UCB0TXBUF = 0x00;
     //   emregister[0]=UCB0RXBUF;
     //chip select low to start transmission
     P1OUT |= BIT3;

}

//Rx_value = Get_register_value(0x02);//read PARTID it should be 0xF2
//chip select low


return 0;
}

void setuprfint(void){
//    //setup switch 1
//    P3DIR &= ~BIT4; //Config 3.4 as input (switch)
//    P3REN |= BIT4;  // Enable resistor
//    P3OUT |= BIT4; // Make resistor a pull up
//    P3IES |= BIT4; // makes sensitivity H-to-L
//
    //setup Interrupt IRQ 1
    P3IE |= BIT4; //Enable P4.5 IRQ
    __enable_interrupt(); //enable mask IRQs
    P3IFG &= ~BIT4; // Clear P4.1 IRQ Flag
}

//// Interrupt Service Routine (ISR) What to do when interrupt occur
#pragma vector = PORT3_VECTOR
__interrupt void ISR_Port3_S4(void){ //interrupt FIFO full reading FIFO Data
     rfintflag = 1;
     __delay_cycles(10);
     P3IFG &= ~BIT4;
}

//Get Register value
unsigned char Get_register_value(unsigned char address)
{
unsigned char rx_value;
//chip select low
P1OUT &= ~BIT5;


// send read register command 0x0B
while(!(UCB0IFG & UCTXIFG));
UCB0TXBUF = 0x0B;
while (!(UCB0IFG & UCRXIFG));
rx_value = UCB0RXBUF;

//send register address to read
while(!(UCB0IFG & UCTXIFG));
UCB0TXBUF = address;
while (!(UCB0IFG & UCRXIFG));
rx_value = UCB0RXBUF;
//tx_value = UCB0TXBUF;

//send dummy byte
while(!(UCB0IFG & UCTXIFG));
UCB0TXBUF = 0xFF;

//get register value
while (!(UCB0IFG & UCRXIFG));
rx_value = UCB0RXBUF;

// chip select high
P1OUT |= BIT5;

return rx_value;
}


//Set register function
void Set_register_value(unsigned char address, unsigned char regValue)
{
unsigned char rx_value;
//chip select low
P1OUT &= (~BIT5);

// send write register command 0x0A
while(!(UCB0IFG & UCTXIFG));
UCB0TXBUF = 0x0A;
//tx_value = UCB0TXBUF;
while (!(UCB0IFG & UCRXIFG));
rx_value = UCB0RXBUF;

//send register address to write in
while(!(UCB0IFG & UCTXIFG));
UCB0TXBUF = address;
while (!(UCB0IFG & UCRXIFG));
rx_value = UCB0RXBUF;

while(!(UCB0IFG & UCTXIFG));
UCB0TXBUF = regValue;//Byte to write in register
while (!(UCB0IFG & UCRXIFG));
rx_value = UCB0RXBUF;

P1OUT |= BIT5;
}

//Initialization of SPI
void spiInit()
{
P1SEL1 |= BIT6 | BIT7; // MOSI AND MISO// USCI_B0 operation
P2SEL1 |= BIT2; //CLK

P1DIR &=~BIT7;//MISO as input
P1DIR |= BIT5 | BIT6 | BIT3; //CHIP SELECT, MOSI as output
P2DIR |=BIT2;//SCLK as output
P1OUT |= BIT5;//set CS high, disable transmition
P1OUT |= BIT3;//set CS high, disable transmition

//PM5CTL0 &=~LOCKLPM5;


UCB0CTLW0 = UCSWRST; // **Put state machine in reset**
//UCB0CTLW0 |= UCMST | UCSYNC | UCMSB | UCCKPL; // 3-pin, 8-bit SPI master
UCB0CTLW0 |= UCMST | UCSYNC | UCCKPH | (0* UCCKPL) | UCMSB;
// Clock polarity high, MSB
UCB0CTLW0 |= UCSSEL__SMCLK; // ACLK
UCB0BR0 = 0x02; // /2
UCB0BR1 = 0; //
// UCB0MCTLW = 0; // No modulation
UCB0CTLW0 &= ~UCSWRST; // **Initialize USCI state machine**

}

// Delay function
void delay(unsigned int ms) {
    unsigned int i, j;
    for (i = 0; i < ms; i++)
        for (j = 0; j < 1000; j++); // Adjust this loop for proper delay
}

//inizialization SPI clock
void clockInit()
{
CSCTL0_H = CSKEY >> 8; // Unlock CS registers
CSCTL1 = DCOFSEL_0; // Set DCO to 1MHz
CSCTL2 = SELA__LFXTCLK | SELS__DCOCLK | SELM__DCOCLK;
CSCTL3 = DIVA__1 | DIVS__1 | DIVM__1; // set all dividers
CSCTL0_H = 0; // Lock CS registers
}

//inizialization UART
void UARTInit()
{
    UCA1CTLW0 |= UCSWRST;
        //Next line selects SMCLK which is DCO (default frequency: 1 MHz)
        UCA1CTLW0 |=  UCSSEL1; //This writes 0x80 which sets BIT7
        //Next two lines divide 1 MHz to get 9600 baud
        UCA1BRW = 0x06;
        UCA1MCTLW |= UCOS16 + UCBRF3 + UCBRS5;
        UCA1CTLW0 &= ~UCSWRST;

//        PM5CTL0 &= ~LOCKLPM5;

        /* Port 2
         P2.1  Button on Launchpad
         P2.2 UCB0 SPI CLK
         P2.5 UCA1 TXD UART
         P2.6 UCA1 RXD UART
        */
         P2DIR |= BIT0 + BIT1 + BIT2 + BIT3 + BIT4 + BIT7;
         P2SEL1 |= BIT2 + BIT5 + BIT6; //Configure pins for SPI CLK; UART on UCA1

}

  • Please delete all the commented out lines of code and use code tags to post.

    (Click the insert button at the bottom of the edit box and select "Code", Language "C". It will make your code look pretty like this:

    #include <stdio.h>
    
    int main()
    {
        printf("Hello, World!\n")
    }

    Some indenting would help, too.

  • In this case i work with spi in msp430fr5969 as the master and there are two slaves adxl362 as slave and em4325 rfid chip as second slave in this case i use the code below. When i debug step by step it is working but when i run the program continously the programme does not work i did not receive anything and other case, i also try only with one slave adxl362 where the code is working without while with step by step debugging but when there is while loop it is not working i did not receive anything i really need help please. 

    #include <msp430.h>
    #include <stdio.h>
    
    void clockInit(void);
    void spiInit(void);
    void UARTInit(void);
    void delay(unsigned int ms);
    unsigned char Get_register_value(unsigned char address);
    void Get_register_Burst(unsigned char address, unsigned char Rxbuf[], unsigned int BytesNum);
    void Set_register_value(unsigned char address, unsigned char regValue);
    void binaryToAscii(unsigned char binaryData, char asciiRepresentation[3]);
    void readFIFO();
    void setuprfint(void);
    
    unsigned int const BytesNum = 3;
    unsigned char Rx_value, Tx_value,x_data, y_data, z_data;
    unsigned char X_data[BytesNum], Y_data[BytesNum], Z_data[BytesNum];
    unsigned char rx_value;
    unsigned char Rxbuf[BytesNum];
    unsigned int i;
    unsigned int zdata;
    signed int zdatas;
    float zvalue;
    unsigned int sensorData;
    unsigned int valuetowrite[2];
    volatile int fifo_buffer[170];
    volatile int fifo_buffer_X[170];
    volatile int fifo_buffer_Y[170];
    volatile int fifo_buffer_Z[1];
    unsigned int i=0;
    unsigned int rfintflag=0;
    unsigned int axis=0;
    unsigned int stepCounter=0;
    unsigned int x_rawvalue;
    unsigned int y_rawvalue;
    unsigned int z_rawvalue;
    unsigned int firstbyte[1];
    unsigned int secondbyte[1];
    unsigned int emstatus;
    
    int main(void)
    {
    WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
    int i=0;
    int count=0;
    
    
    UCA1CTLW0 &= ~UCSWRST; //Takes UART A1 out of sw reset
    PM5CTL0 &= ~LOCKLPM5;
    
    clockInit();
    spiInit();
    UARTInit();
    setuprfint();
    
    Set_register_value(0x29, 0xFF);//FIFO_SAMPLES
    Set_register_value(0x28, 0x09);//FIFO_CONTROL,
    Set_register_value(0x2A, 0x84);//INTMAP1 Data_ready mapped into INT1, failing edge
    Set_register_value(0x2B, 0xC0);//INTMAP2 activity mapped into INT2, failing edge
    Set_register_value(0x2C, 0x11);//FILTER_CTL, ODR = 400Hz,+-2g
    
    Set_register_value(0x2D, 0x02);//POWER_CTL Measurement mode, WAKE UP
    
    
    while(1){
        P1OUT &= ~BIT5;
        // send read FIFO register command 0x0D
        while(!(UCB0IFG & UCTXIFG));
        UCB0TXBUF = 0x0D;
        // b15-b14 : 00-x 01-y 10-z 11-temp
        // b11-b0  : data
        // each fifo sample has 16 bits, 2 bits of axis information, 2 bits redundant, 12 bits of data
        // Send dummy byte and receive data for X axis
        while (!(UCB0IFG & UCTXIFG));
        UCB0TXBUF = 0x00;
        while (!(UCB0IFG & UCRXIFG));
        UCB0TXBUF = 0x00; // Send another dummy byte
    
    
        // Send dummy byte and receive data for Y axis
        while (!(UCB0IFG & UCTXIFG));
        UCB0TXBUF = 0x00;
        delay(10); // 10 ms delay, adjust as needed
        while (!(UCB0IFG & UCTXIFG));
        UCB0TXBUF = 0x00; // Send another dummy byte
        
        // Send dummy byte and receive data for Z axis
        while (!(UCB0IFG & UCTXIFG));
        UCB0TXBUF = 0x00;
        while (UCB0STATW & UCBUSY);
        while (!(UCB0IFG & UCRXIFG));
        fifo_buffer_Z[1] = UCB0RXBUF;
        while (!(UCB0IFG & UCTXIFG));
        UCB0TXBUF = 0x00; // Send another dummy byte
        delay(10); // 10 ms delay, adjust as needed
        while (UCB0STATW & UCBUSY);
        while (!(UCB0IFG & UCRXIFG));
        fifo_buffer_Z[1] |= (UCB0RXBUF << 8); // Combine with the received byte
        fifo_buffer_Z[1] = (fifo_buffer_Z[1] << 4) >> 4; // Shift left by 4 bits and then right by 4 bits to remove redundant bits
                   //    z_rawvalue = fifo_buffer_Z[i];
    
        // chip select high
        P1OUT |= BIT5;
    //
    //}
        //chip select low to start transmission
        P1OUT &= ~BIT3;
    
        // send write register file command
        while(!(UCB0IFG & UCTXIFG));
        UCB0TXBUF = 0xEF;
        while (UCB0STATW & UCBUSY);
    
        // send memory address to write
        while(!(UCB0IFG & UCTXIFG));
        UCB0TXBUF = 0x0001;
        while (UCB0STATW & UCBUSY);
    
        // Value to write to memory
        valuetowrite[0] = fifo_buffer_Z[1];
        valuetowrite[1] = fifo_buffer_Z[1];
    
        // Split the 16-bit value into two 8-bit values
        valuetowrite[0] >>=8;
        firstbyte[0] = valuetowrite[0];
        valuetowrite[1] = valuetowrite[1] & 0xFF;
        secondbyte[0] = valuetowrite[1];      // Extract lower 8 bits (0x34)
    
        // send value to write to the register file
        while(!(UCB0IFG & UCTXIFG));
        UCB0TXBUF = firstbyte[0];
        while (UCB0STATW & UCBUSY);
    
         // send value to write to the register file
         while(!(UCB0IFG & UCTXIFG));
         UCB0TXBUF = secondbyte[0];
         while (UCB0STATW & UCBUSY);
    
         //obtain EM4325 memory value
         while(!(UCB0IFG & UCTXIFG));
         UCB0TXBUF = 0x00;
         while (UCB0STATW & UCBUSY);
         while(!(UCB0IFG & UCTXIFG));
         UCB0TXBUF = 0x00;
         while (UCB0STATW & UCBUSY);
         while(!(UCB0IFG & UCRXIFG));
         emstatus=UCB0RXBUF;
         P1OUT |= BIT3;
    
    }
    
    //Rx_value = Get_register_value(0x02);//read PARTID it should be 0xF2
    //chip select low
    
    
    return 0;
    }
    
    void setuprfint(void){
    
        //setup Interrupt IRQ 1
        P3IE |= BIT4; //Enable P4.5 IRQ
        __enable_interrupt(); //enable mask IRQs
        P3IFG &= ~BIT4; // Clear P4.1 IRQ Flag
    }
    
    //// Interrupt Service Routine (ISR) What to do when interrupt occur
    #pragma vector = PORT3_VECTOR
    __interrupt void ISR_Port3_S4(void){ //interrupt FIFO full reading FIFO Data
         rfintflag = 1;
         __delay_cycles(10);
         P3IFG &= ~BIT4;
    }
    
    //Get Register value
    unsigned char Get_register_value(unsigned char address)
    {
    unsigned char rx_value;
    //chip select low
    P1OUT &= ~BIT5;
    
    
    // send read register command 0x0B
    while(!(UCB0IFG & UCTXIFG));
    UCB0TXBUF = 0x0B;
    while (!(UCB0IFG & UCRXIFG));
    rx_value = UCB0RXBUF;
    
    //send register address to read
    while(!(UCB0IFG & UCTXIFG));
    UCB0TXBUF = address;
    while (!(UCB0IFG & UCRXIFG));
    rx_value = UCB0RXBUF;
    //tx_value = UCB0TXBUF;
    
    //send dummy byte
    while(!(UCB0IFG & UCTXIFG));
    UCB0TXBUF = 0xFF;
    
    //get register value
    while (!(UCB0IFG & UCRXIFG));
    rx_value = UCB0RXBUF;
    
    // chip select high
    P1OUT |= BIT5;
    
    return rx_value;
    }
    
    
    //Set register function
    void Set_register_value(unsigned char address, unsigned char regValue)
    {
    unsigned char rx_value;
    //chip select low
    P1OUT &= (~BIT5);
    
    // send write register command 0x0A
    while(!(UCB0IFG & UCTXIFG));
    UCB0TXBUF = 0x0A;
    //tx_value = UCB0TXBUF;
    while (!(UCB0IFG & UCRXIFG));
    rx_value = UCB0RXBUF;
    
    //send register address to write in
    while(!(UCB0IFG & UCTXIFG));
    UCB0TXBUF = address;
    while (!(UCB0IFG & UCRXIFG));
    rx_value = UCB0RXBUF;
    
    while(!(UCB0IFG & UCTXIFG));
    UCB0TXBUF = regValue;//Byte to write in register
    while (!(UCB0IFG & UCRXIFG));
    rx_value = UCB0RXBUF;
    
    P1OUT |= BIT5;
    }
    
    //Initialization of SPI
    void spiInit()
    {
    P1SEL1 |= BIT6 | BIT7; // MOSI AND MISO// USCI_B0 operation
    P2SEL1 |= BIT2; //CLK
    
    P1DIR &=~BIT7;//MISO as input
    P1DIR |= BIT5 | BIT6 | BIT3; //CHIP SELECT, MOSI as output
    P2DIR |=BIT2;//SCLK as output
    P1OUT |= BIT5;//set CS high, disable transmition
    P1OUT |= BIT3;//set CS high, disable transmition
    
    //PM5CTL0 &=~LOCKLPM5;
    
    
    UCB0CTLW0 = UCSWRST; // **Put state machine in reset**
    //UCB0CTLW0 |= UCMST | UCSYNC | UCMSB | UCCKPL; // 3-pin, 8-bit SPI master
    UCB0CTLW0 |= UCMST | UCSYNC | UCCKPH | (0* UCCKPL) | UCMSB;
    // Clock polarity high, MSB
    UCB0CTLW0 |= UCSSEL__SMCLK; // ACLK
    UCB0BR0 = 0x02; // /2
    UCB0BR1 = 0; //
    // UCB0MCTLW = 0; // No modulation
    UCB0CTLW0 &= ~UCSWRST; // **Initialize USCI state machine**
    
    }
    
    // Delay function
    void delay(unsigned int ms) {
        unsigned int i, j;
        for (i = 0; i < ms; i++)
            for (j = 0; j < 1000; j++); // Adjust this loop for proper delay
    }
    
    //inizialization SPI clock
    void clockInit()
    {
    CSCTL0_H = CSKEY >> 8; // Unlock CS registers
    CSCTL1 = DCOFSEL_0; // Set DCO to 1MHz
    CSCTL2 = SELA__LFXTCLK | SELS__DCOCLK | SELM__DCOCLK;
    CSCTL3 = DIVA__1 | DIVS__1 | DIVM__1; // set all dividers
    CSCTL0_H = 0; // Lock CS registers
    }
    
    //inizialization UART
    void UARTInit()
    {
        UCA1CTLW0 |= UCSWRST;
            //Next line selects SMCLK which is DCO (default frequency: 1 MHz)
            UCA1CTLW0 |=  UCSSEL1; //This writes 0x80 which sets BIT7
            //Next two lines divide 1 MHz to get 9600 baud
            UCA1BRW = 0x06;
            UCA1MCTLW |= UCOS16 + UCBRF3 + UCBRS5;
            UCA1CTLW0 &= ~UCSWRST;
    
    //        PM5CTL0 &= ~LOCKLPM5;
    
            /* Port 2
             P2.1  Button on Launchpad
             P2.2 UCB0 SPI CLK
             P2.5 UCA1 TXD UART
             P2.6 UCA1 RXD UART
            */
             P2DIR |= BIT0 + BIT1 + BIT2 + BIT3 + BIT4 + BIT7;
             P2SEL1 |= BIT2 + BIT5 + BIT6; //Configure pins for SPI CLK; UART on UCA1
    
    }

  • Each SPI byte includes both a Tx byte and an Rx byte, and thus both a TXIFG and RXIFG. RXIFG doesn't auto-clear when you read it, so it just piles up. For a Tx-only transaction, this is (generally) tolerable, but when you want to receive something you're not sure which byte you're receiving.

    I get a lot of value out of a simple function (spix()) discussed over here:

    https://e2e.ti.com/support/microcontrollers/msp-low-power-microcontrollers-group/msp430/f/msp-low-power-microcontroller-forum/758000/msp430fr5729-problems-with-implementing-mcp23s08-8-bit-port-expander-on-msp430fr5729/2800722#2800722

    You give it a Tx byte, and it gives you back the corresponding Rx byte, which you can use or throw away. More to the point, it manages TXIFG/RXIFG so you always know which byte you're getting. It's not particularly fast, but for short transactions it's actually quicker than DMA. I also find it much easier to read. [David Schultz has pointed out that if you promise(!) to only use this function, you can leave out the first line (checking TXIFG) since it's always set.]

  • Hello Bruce,

    Thank you very much for your reply. So you mean that i should try to use spix() function right?.

  • That's what I would suggest. There's a sample set_reg() function in that thread you also might find useful.

    When you get to using very long transactions (e.g. reading out the entire FIFO) you might consider using DMA for that -- but that's not a requirement.

  • Thankss i will try in this case can you give me the reason why when i run it step by step in ccs it is working fine while i run continously in ccs it does not work just to have better understanding what is happening

  • The original code is useless. For one the same sequences of code keep appearing. Perhaps it is  my early exposure to Forth but that offends me and screams out for a function.

    Second is that it doesn't work. Taking an example from early:

        while(!(UCB0IFG & UCTXIFG));
        UCB0TXBUF = 0x0D;
        // b15-b14 : 00-x 01-y 10-z 11-temp
        // b11-b0  : data
        // each fifo sample has 16 bits, 2 bits of axis information, 2 bits redundant, 12 bits of data
        // Send dummy byte and receive data for X axis
        while (!(UCB0IFG & UCTXIFG));
        UCB0TXBUF = 0x00;
        while (!(UCB0IFG & UCRXIFG));
    //    fifo_buffer_X[i] = UCB0RXBUF;

    That wait on RXIFG will not wait at all. Why? Because two bytes have been placed in TXBUF which means that the received value from the first is already in RXBUF.

    So you now have a race. Will the second received byte be put into RXBUF before or after you read RXBUF? That will depend on clock settings and the number of instructions executed. Throw in the debugger and the answer could change. With the first received byte being read at full speed and the second (the one you want) when using the debugger to single step.

  • Hello Bruce thanks for the suggestion for now i am modifying my code in this case in the provided link i just find the sample set_reg() function to write spi can you help how to code a function to read value from the spi. 

  • I did modify my code please find it below. in this case i dont know if it is correct but when i run it continously it still gives me nothing in the buffer the buffer is zero. In this case i want to read FIFO of ADXL362 accelerometer do you think i should use dma?

    #include <msp430.h>
    #include <stdio.h>
    #include <stdint.h>
    
    
    void clockInit(void);
    void spiInit(void);
    void UARTInit(void);
    unsigned char Get_register_value(unsigned char address);
    void Get_register_Burst(unsigned char address, unsigned char Rxbuf[], unsigned int BytesNum);
    void Set_register_value(unsigned char address, unsigned char regValue);
    void binaryToAscii(unsigned char binaryData, char asciiRepresentation[3]);
    void readFIFO();
    
    unsigned int const BytesNum = 3;
    unsigned char Rx_value, Tx_value,x_data, y_data, z_data;
    unsigned char X_data[BytesNum], Y_data[BytesNum], Z_data[BytesNum];
    unsigned char rx_value;
    unsigned char Rxbuf[BytesNum];
    unsigned int i;
    unsigned int zdata;
    signed int zdatas;
    float zvalue;
    unsigned int sensorData;
    unsigned int valuetowrite[170];
    volatile int fifo_buffer[170];
    volatile int fifo_buffer_X[170];
    volatile int fifo_buffer_Y[170];
    volatile int fifo_buffer_Z[170];
    unsigned int i=0;
    unsigned int axis=0;
    unsigned int stepCounter=0;
    unsigned int x_rawvalue;
    unsigned int y_rawvalue;
    unsigned int z_rawvalue;
    
    int main(void)
    {
    WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
    int i=0;
    int count=0;
    
    
    UCA1CTLW0 &= ~UCSWRST; //Takes UART A1 out of sw reset
    PM5CTL0 &= ~LOCKLPM5;
    
    clockInit();
    spiInit();
    UARTInit();
    
    __bis_SR_register(GIE);
    
    Set_register_value(0x1F, 0x52);//software reset
    Set_register_value(0x29, 0xFF);//FIFO_SAMPLES
    Set_register_value(0x28, 0x09);//FIFO_CONTROL,
    Set_register_value(0x2A, 0x84);//INTMAP1 Data_ready mapped into INT1, failing edge
    Set_register_value(0x2B, 0xC0);//INTMAP2 activity mapped into INT2, failing edge
    Set_register_value(0x2C, 0x11);//FILTER_CTL, ODR = 400Hz,+-2g
    
    Set_register_value(0x2D, 0x02);//POWER_CTL Measurement mode, WAKE UP
    
    while(1){
        P1OUT &= ~BIT5;
            // send read FIFO register command 0x0D
            spix(0x0D);
    
            // b15-b14 : 00-x 01-y 10-z 11-temp
            // b11-b0  : data
            for (i = 0; i < 170; i++) {
            // each fifo sample has 16 bits, 2 bits of axis information, 2 bits redundant, 12 bits of data
            // Send dummy byte and receive data for X axis
            fifo_buffer_X[i] = spix(0x00);
            fifo_buffer_X[i] |= (spix(0x00) << 8); // Combine with the received byte
            fifo_buffer_X[i] = (fifo_buffer_X[i] << 4) >> 4; // Shift left by 4 bits and then right by 4 bits to remove redundant bits
            x_rawvalue = fifo_buffer_X[i];
    
            fifo_buffer_Y[i] = spix(0x00);
            fifo_buffer_Y[i] |= (spix(0x00) << 8); // Combine with the received byte
            fifo_buffer_Y[i] = (fifo_buffer_Y[i] << 4) >> 4; // Shift left by 4 bits and then right by 4 bits to remove redundant bits
            y_rawvalue = fifo_buffer_Y[i];
    
            fifo_buffer_Z[i] = spix(0x00);
            fifo_buffer_Z[i] |= (spix(0x00) << 8); // Combine with the received byte
            fifo_buffer_Z[i] = (fifo_buffer_Z[i] << 4) >> 4; // Shift left by 4 bits and then right by 4 bits to remove redundant bits
            x_rawvalue = fifo_buffer_Z[i];
            valuetowrite[i] = fifo_buffer_Z[i];
             }
                // chip select high
            P1OUT |= BIT5;
    //
    }
    
    }
    
    //Get Register value
    unsigned char Get_register_value(unsigned char address)
    {
    unsigned char rx_value;
    //chip select low
    //chip select low
    P1OUT &= (~BIT5);
    
    // send write register command 0x0A
    spix(0x0B);
    //send register address to write in
    spix(address);
    spix(0xFF);//dummybyte
    rx_value = UCB0RXBUF;
    P1OUT |= BIT5;
    return rx_value;
    }
    
    uint8_t spix(uint8_t c)
    {
      while (!(UCB0IFG & UCTXIFG)) /*EMPTY*/;
      UCB0TXBUF = c;
      while (!(UCB0IFG & UCRXIFG)) /*EMPTY*/;
      c = UCB0RXBUF;
      return(c);
    }
    
    
    //Set register function
    void Set_register_value(unsigned char address, unsigned char regValue)
    {
    //chip select low
    P1OUT &= (~BIT5);
    
    // send write register command 0x0A
    spix(0x0A);
    //send register address to write in
    spix(address);
    spix(regValue);//Byte to write in register
    P1OUT |= BIT5;
    return;
    }
    
    //Initialization of SPI
    void spiInit()
    {
    P1SEL1 |= BIT6 | BIT7; // MOSI AND MISO// USCI_B0 operation
    P2SEL1 |= BIT2; //CLK
    
    P1DIR &=~BIT7;//MISO as input
    P1DIR |= BIT5 | BIT6; //CHIP SELECT, MOSI as output
    P2DIR |=BIT2;//SCLK as output
    P1OUT |= BIT5;//set CS high, disable transmition
    
    //PM5CTL0 &=~LOCKLPM5;
    
    
    UCB0CTLW0 = UCSWRST; // **Put state machine in reset**
    //UCB0CTLW0 |= UCMST | UCSYNC | UCMSB | UCCKPL; // 3-pin, 8-bit SPI master
    UCB0CTLW0 |= UCMST | UCSYNC | UCCKPH | (0* UCCKPL) | UCMSB;
    // Clock polarity high, MSB
    UCB0CTLW0 |= UCSSEL__SMCLK; // ACLK
    UCB0BR0 = 0x02; // /2
    UCB0BR1 = 0; //
    // UCB0MCTLW = 0; // No modulation
    UCB0CTLW0 &= ~UCSWRST; // **Initialize USCI state machine**
    
    }
    
    //inizialization SPI clock
    void clockInit()
    {
    CSCTL0_H = CSKEY >> 8; // Unlock CS registers
    CSCTL1 = DCOFSEL_0; // Set DCO to 1MHz
    CSCTL2 = SELA__LFXTCLK | SELS__DCOCLK | SELM__DCOCLK;
    CSCTL3 = DIVA__1 | DIVS__1 | DIVM__1; // set all dividers
    CSCTL0_H = 0; // Lock CS registers
    }
    
    //inizialization UART
    void UARTInit()
    {
        UCA1CTLW0 |= UCSWRST;
            //Next line selects SMCLK which is DCO (default frequency: 1 MHz)
            UCA1CTLW0 |=  UCSSEL1; //This writes 0x80 which sets BIT7
            //Next two lines divide 1 MHz to get 9600 baud
            UCA1BRW = 0x06;
            UCA1MCTLW |= UCOS16 + UCBRF3 + UCBRS5;
            UCA1CTLW0 &= ~UCSWRST;
    
    //        PM5CTL0 &= ~LOCKLPM5;
    
            /* Port 2
             P2.1  Button on Launchpad
             P2.2 UCB0 SPI CLK
             P2.5 UCA1 TXD UART
             P2.6 UCA1 RXD UART
            */
             P2DIR |= BIT0 + BIT1 + BIT2 + BIT3 + BIT4 + BIT7;
             P2SEL1 |= BIT2 + BIT5 + BIT6; //Configure pins for SPI CLK; UART on UCA1
    
    }

  • The first exercise I usually try with a new device is to read a register with a known value and see if it comes back correctly. This makes sure the SPI and CS are set up properly. Registers 0-2 are likely candidates for this.

    -------------

    > Set_register_value(0x1F, 0x52);//software reset

    The data sheet prescribes a 0.5ms delay here. It's possible some/all of the subsequent register settings are being ignored.

    -------------

    > Set_register_value(0x2C, 0x11);//FILTER_CTL, ODR = 400Hz,+-2g

    This appears to set ODR=25Hz, not 400Hz. Even at 400Hz, it will take 400+ms to fill the FIFO, so you're probably underrunning it. The data sheet doesn't say what happens in this case, but getting zeroes wouldn't be surprising.

    Try introducing a delay (maybe 1 second to start with) into your loop, to allow some data to collect.

  • Before trying DMA, get the polled version working. If that isn't fast enough, then try DMA.

    This is a problem:

            fifo_buffer_X[i] |= (spix(0x00) << 8); // Combine with the received byte

    Since spix() returns a byte, a left shift discards all of it. Promote to an integer (16 bits) first.

    ((int)spix(0))

    Without even digging out a device data sheet I see problems with your code.

    1) You read the FIFO without checking to see if anything is in it.

    2) Your loop index is 170 with 6 reads per loop. Is there 6X170 bytes of data in the FIFO?

    After digging up the ADXL data sheet I see you have set it for 25 SPS. So the FIFO really isn't needed. It will take several seconds to fill the FIFO and you aren't waiting. You aren't even reading the registers that tell you how much data is in the FIFO.

  • Thanks to both of you guys you took me from a dead end. In this case i am able to read the FIFO with putting some waiting delay to wait the FIFO to be filled. I do not want to implement the delay later on in this case i want to put FIFO watermark interrupt but when i use this FIFO watermark interrupt again i get nothing in my memory but when i did not use the FIFO watermark i just put delay it is working please find the code below 

    #include <msp430.h>
    #include <stdio.h>
    #include <stdint.h>
    
    
    void clockInit(void);
    void spiInit(void);
    void UARTInit(void);
    unsigned char Get_register_value(unsigned char address);
    void Get_register_Burst(unsigned char address, unsigned char Rxbuf[], unsigned int BytesNum);
    void Set_register_value(unsigned char address, unsigned char regValue);
    void binaryToAscii(unsigned char binaryData, char asciiRepresentation[3]);
    void readFIFO();
    void EM4325initiate(void);
    void EM4325readmemory(void);
    void EM4325writememory(void);
    void EM4325readsensor(void);
    void EM4325readregisterfile(void);
    void EM4325writeregisterfile(void);
    void setupaccint(void);
    
    unsigned int sensorData;
    unsigned int emsensor[9];
    unsigned int emmemory[3];
    unsigned int emregister[2];
    unsigned int emregistervalue[1];
    unsigned int valuetowrite[2];
    unsigned int firstbyte[1];
    unsigned int secondbyte[1];
    unsigned int emstatus;
    unsigned int const BytesNum = 3;
    unsigned char Rx_value, Tx_value,x_data, y_data, z_data;
    unsigned char X_data[BytesNum], Y_data[BytesNum], Z_data[BytesNum];
    unsigned int fifowatermark=0;
    unsigned char rx_value;
    unsigned char Rxbuf[BytesNum];
    unsigned int i;
    unsigned int zdata;
    signed int zdatas;
    float zvalue;
    unsigned int sensorData;
    //unsigned int valuetowrite[170];
    volatile int fifo_buffer[170];
    volatile int fifo_buffer_X[170];
    volatile int fifo_buffer_Y[170];
    volatile int fifo_buffer_Z[170];
    unsigned int i=0;
    unsigned int axis=0;
    unsigned int stepCounter=0;
    unsigned int x_rawvalue;
    unsigned int y_rawvalue;
    unsigned int z_rawvalue;
    
    int main(void)
    {
    WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
    int i=0;
    int count=0;
    
    
    UCA1CTLW0 &= ~UCSWRST; //Takes UART A1 out of sw reset
    PM5CTL0 &= ~LOCKLPM5;
    
    clockInit();
    spiInit();
    UARTInit();
    //setupaccint();
    
    __bis_SR_register(GIE);
    
    Set_register_value(0x1F, 0x52);//software reset
    __delay_cycles(6000);
    Set_register_value(0x29, 0xFF);//FIFO_SAMPLES
    Set_register_value(0x28, 0x09);//FIFO_CONTROL,
    Set_register_value(0x2A, 0x84);//INTMAP1 Data_ready mapped into INT1, failing edge
    Set_register_value(0x2B, 0xC0);//INTMAP2 activity mapped into INT2, failing edge
    Set_register_value(0x2C, 0x11);//FILTER_CTL, ODR = 25Hz,+-2g
    
    Set_register_value(0x2D, 0x02);//POWER_CTL Measurement mode, WAKE UP
    
    while(1){
        if(fifowatermark){ //if i did not use this condition it works
    //   __delay_cycles(6000000);
        readFIFO();
        P4IFG &= ~BIT3;
        }
    }
    
    }
    
    void setupaccint(void){
        //setup switch 1
    
        //setup Interrupt IRQ 1
        P4IE |= BIT3; //Enable P4.5 IRQ
        __enable_interrupt(); //enable mask IRQs
        P4IFG &= ~BIT3; // Clear P4.1 IRQ Flag
    }
    
    //// Interrupt Service Routine (ISR) What to do when interrupt occur
    #pragma vector = PORT4_VECTOR
    __interrupt void ISR_Port4_S3(void){ //interrupt FIFO full reading FIFO Data
         fifowatermark = 1;
    }
    
    
    //Get Register value
    unsigned char Get_register_value(unsigned char address)
    {
    unsigned char rx_value;
    //chip select low
    //chip select low
    P1OUT &= (~BIT5);
    
    // send write register command 0x0A
    spix(0x0B);
    //send register address to write in
    spix(address);
    spix(0xFF);//dummybyte
    rx_value = UCB0RXBUF;
    P1OUT |= BIT5;
    return rx_value;
    }
    
    uint8_t spix(uint8_t c)
    {
      while (!(UCB0IFG & UCTXIFG)) /*EMPTY*/;
      UCB0TXBUF = c;
      while (!(UCB0IFG & UCRXIFG)) /*EMPTY*/;
      c = UCB0RXBUF;
      return(c);
    }
    
    void readFIFO(){
        P1OUT &= ~BIT5;
            // send read FIFO register command 0x0D
            spix(0x0D);
    
    
            // b15-b14 : 00-x 01-y 10-z 11-temp
            // b11-b0  : data
            for (i = 0; i < 170; i++) {
            // each fifo sample has 16 bits, 2 bits of axis information, 2 bits redundant, 12 bits of data
            // Send dummy byte and receive data for X axis
            fifo_buffer_X[i] = spix(0x00);
            fifo_buffer_X[i] |= ((int)spix(0x00) << 8); // Combine with the received byte
            fifo_buffer_X[i] = (fifo_buffer_X[i] << 4) >> 4; // Shift left by 4 bits and then right by 4 bits to remove redundant bits
            x_rawvalue = fifo_buffer_X[i];
    
            fifo_buffer_Y[i] = spix(0x00);
            fifo_buffer_Y[i] |= ((int)spix(0x00) << 8); // Combine with the received byte
            fifo_buffer_Y[i] = (fifo_buffer_Y[i] << 4) >> 4; // Shift left by 4 bits and then right by 4 bits to remove redundant bits
            y_rawvalue = fifo_buffer_Y[i];
    
            fifo_buffer_Z[i] = spix(0x00);
            fifo_buffer_Z[i] |= ((int)spix(0x00) << 8); // Combine with the received byte
            fifo_buffer_Z[i] = (fifo_buffer_Z[i] << 4) >> 4; // Shift left by 4 bits and then right by 4 bits to remove redundant bits
            z_rawvalue = fifo_buffer_Z[i];
        //    valuetowrite[i] = fifo_buffer_Z[i];
            }
            P1OUT |= BIT5;
    }
    
    
    //Set register function
    void Set_register_value(unsigned char address, unsigned char regValue)
    {
    //chip select low
    P1OUT &= (~BIT5);
    
    // send write register command 0x0A
    spix(0x0A);
    //send register address to write in
    spix(address);
    spix(regValue);//Byte to write in register
    P1OUT |= BIT5;
    return;
    }
    
    //Initialization of SPI
    void spiInit()
    {
    P1SEL1 |= BIT6 | BIT7; // MOSI AND MISO// USCI_B0 operation
    P2SEL1 |= BIT2; //CLK
    
    P1DIR &=~BIT7;//MISO as input
    P1DIR |= BIT3 | BIT5 | BIT6; //CHIP SELECT, MOSI as output
    P2DIR |=BIT2;//SCLK as output
    P1OUT |= BIT5;//set CS high, disable transmition
    P1OUT |= BIT3;//set CS high, disable transmition
    
    //PM5CTL0 &=~LOCKLPM5;
    
    
    UCB0CTLW0 = UCSWRST; // **Put state machine in reset**
    //UCB0CTLW0 |= UCMST | UCSYNC | UCMSB | UCCKPL; // 3-pin, 8-bit SPI master
    UCB0CTLW0 |= UCMST | UCSYNC | UCCKPH | (0* UCCKPL) | UCMSB;
    // Clock polarity high, MSB
    UCB0CTLW0 |= UCSSEL__SMCLK; // ACLK
    UCB0BR0 = 0x02; // /2
    UCB0BR1 = 0; //
    // UCB0MCTLW = 0; // No modulation
    UCB0CTLW0 &= ~UCSWRST; // **Initialize USCI state machine**
    
    }
    
    //inizialization SPI clock
    void clockInit()
    {
    CSCTL0_H = CSKEY >> 8; // Unlock CS registers
    CSCTL1 = DCOFSEL_0; // Set DCO to 1MHz
    CSCTL2 = SELA__LFXTCLK | SELS__DCOCLK | SELM__DCOCLK;
    CSCTL3 = DIVA__1 | DIVS__1 | DIVM__1; // set all dividers
    CSCTL0_H = 0; // Lock CS registers
    }
    
    //inizialization UART
    void UARTInit()
    {
        UCA1CTLW0 |= UCSWRST;
            //Next line selects SMCLK which is DCO (default frequency: 1 MHz)
            UCA1CTLW0 |=  UCSSEL1; //This writes 0x80 which sets BIT7
            //Next two lines divide 1 MHz to get 9600 baud
            UCA1BRW = 0x06;
            UCA1MCTLW |= UCOS16 + UCBRF3 + UCBRS5;
            UCA1CTLW0 &= ~UCSWRST;
    
    //        PM5CTL0 &= ~LOCKLPM5;
    
            /* Port 2
             P2.1  Button on Launchpad
             P2.2 UCB0 SPI CLK
             P2.5 UCA1 TXD UART
             P2.6 UCA1 RXD UART
            */
             P2DIR |= BIT0 + BIT1 + BIT2 + BIT3 + BIT4 + BIT7;
             P2SEL1 |= BIT2 + BIT5 + BIT6; //Configure pins for SPI CLK; UART on UCA1
    
    }

  • 1) fifowatermark should be declared "volatile".

    2) You need to clear P4IFG.3 inside the ISR (not some time later) or it will continue to interrupt and you'll never return to main().

    3) More generally: There's a prescribed sequence for setting up a port interrupt. You can get some idea from example msp430fr59xx_p1_03.c here:

    https://dev.ti.com/tirex/explore/node?node=A__ADK3rB2bf-DOgn6aSbCoTQ__msp430ware__IOGqZri__LATEST

    [Edit: Unsolicited:

    4) This sequence

    > spix(0xFF);//dummybyte
    > rx_value = UCB0RXBUF;

    works by accident, but should properly be:

    > rx_value = spix(0xFF);//dummybyte
    ]

  • Hello Bruce thanks i tried your suggestion i also modify the code

    #include <msp430.h>
    #include <stdio.h>
    #include <stdint.h>
    
    
    void clockInit(void);
    void spiInit(void);
    void UARTInit(void);
    unsigned char Get_register_value(unsigned char address);
    void Get_register_Burst(unsigned char address, unsigned char Rxbuf[], unsigned int BytesNum);
    void Set_register_value(unsigned char address, unsigned char regValue);
    void binaryToAscii(unsigned char binaryData, char asciiRepresentation[3]);
    void readFIFO();
    void EM4325initiate(void);
    void EM4325readmemory(void);
    void EM4325writememory(void);
    void EM4325readsensor(void);
    void EM4325readregisterfile(void);
    void EM4325writeregisterfile(void);
    void setupaccint(void);
    
    unsigned int sensorData;
    unsigned int emsensor[9];
    unsigned int emmemory[3];
    unsigned int emregister[2];
    unsigned int emregistervalue[1];
    unsigned int valuetowrite[2];
    unsigned int firstbyte[1];
    unsigned int secondbyte[1];
    unsigned int emstatus;
    unsigned int const BytesNum = 3;
    unsigned char Rx_value, Tx_value,x_data, y_data, z_data;
    unsigned char X_data[BytesNum], Y_data[BytesNum], Z_data[BytesNum];
    volatile int fifowatermark=0;
    unsigned char rx_value;
    unsigned char Rxbuf[BytesNum];
    unsigned int i;
    unsigned int zdata;
    signed int zdatas;
    float zvalue;
    unsigned int sensorData;
    //unsigned int valuetowrite[170];
    volatile int fifo_buffer[170];
    volatile int fifo_buffer_X[170];
    volatile int fifo_buffer_Y[170];
    volatile int fifo_buffer_Z[170];
    unsigned int i=0;
    unsigned int axis=0;
    unsigned int stepCounter=0;
    unsigned int x_rawvalue;
    unsigned int y_rawvalue;
    unsigned int z_rawvalue;
    
    int main(void)
    {
    WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
    int i=0;
    int count=0;
    
    
    UCA1CTLW0 &= ~UCSWRST; //Takes UART A1 out of sw reset
    PM5CTL0 &= ~LOCKLPM5;
    
    clockInit();
    spiInit();
    UARTInit();
    //setupaccint();
    
    __bis_SR_register(GIE);
    
    Set_register_value(0x1F, 0x52);//software reset
    __delay_cycles(6000);
    Set_register_value(0x29, 0xFF);//FIFO_SAMPLES
    Set_register_value(0x28, 0x09);//FIFO_CONTROL,
    Set_register_value(0x2A, 0x84);//INTMAP1 Data_ready mapped into INT1, failing edge
    Set_register_value(0x2B, 0xC0);//INTMAP2 activity mapped into INT2, failing edge
    Set_register_value(0x2C, 0x11);//FILTER_CTL, ODR = 25Hz,+-2g
    
    Set_register_value(0x2D, 0x02);//POWER_CTL Measurement mode, WAKE UP
    
    while(1){
        if(fifowatermark==1){
    //   __delay_cycles(6000000);
        readFIFO();
        fifowatermark=0;
        }
    
    }
    
    }
    
    void setupaccint(void){
        //setup switch 1
    
        //setup Interrupt IRQ 1
    //    P4IE |= BIT3; //Enable P4.5 IRQ
    //    __enable_interrupt(); //enable mask IRQs
    //    P4IFG &= ~BIT3; // Clear P4.1 IRQ Flag
        // Configure GPIO
        P4OUT = BIT3;                             // Pull-up resistor on P1.1
        P4REN = BIT3;                             // Select pull-up mode for P1.1
        P4DIR = 0xFF ^ BIT3;                      // Set all but P1.1 to output direction
        P4IES = BIT3;                             // P1.1 Hi/Lo edge
        P4IFG = 0;                                // Clear all P1 interrupt flags
        P4IE = BIT3;                              // P1.1 interrupt enabled
    }
    
    //// Interrupt Service Routine (ISR) What to do when interrupt occur
    #pragma vector = PORT4_VECTOR
    __interrupt void ISR_Port4_S3(void){ //interrupt FIFO full reading FIFO Data
        P4IES ^= BIT3;                            // Toggle interrupt edge
         fifowatermark = 1;
         P4IFG &= ~BIT3;
    }
    
    
    //Get Register value
    unsigned char Get_register_value(unsigned char address)
    {
    unsigned char rx_value;
    //chip select low
    //chip select low
    P1OUT &= (~BIT5);
    
    // send write register command 0x0A
    spix(0x0B);
    //send register address to write in
    spix(address);
    rx_value = spix(0xFF);//dummybyte
    P1OUT |= BIT5;
    return rx_value;
    }
    
    uint8_t spix(uint8_t c)
    {
      while (!(UCB0IFG & UCTXIFG)) /*EMPTY*/;
      UCB0TXBUF = c;
      while (!(UCB0IFG & UCRXIFG)) /*EMPTY*/;
      c = UCB0RXBUF;
      return(c);
    }
    
    void readFIFO(){
        P1OUT &= ~BIT5;
            // send read FIFO register command 0x0D
            spix(0x0D);
    
    
            // b15-b14 : 00-x 01-y 10-z 11-temp
            // b11-b0  : data
            for (i = 0; i < 170; i++) {
            // each fifo sample has 16 bits, 2 bits of axis information, 2 bits redundant, 12 bits of data
            // Send dummy byte and receive data for X axis
            fifo_buffer_X[i] = spix(0x00);
            fifo_buffer_X[i] |= ((int)spix(0x00) << 8); // Combine with the received byte
            fifo_buffer_X[i] = (fifo_buffer_X[i] << 4) >> 4; // Shift left by 4 bits and then right by 4 bits to remove redundant bits
            x_rawvalue = fifo_buffer_X[i];
    
            fifo_buffer_Y[i] = spix(0x00);
            fifo_buffer_Y[i] |= ((int)spix(0x00) << 8); // Combine with the received byte
            fifo_buffer_Y[i] = (fifo_buffer_Y[i] << 4) >> 4; // Shift left by 4 bits and then right by 4 bits to remove redundant bits
            y_rawvalue = fifo_buffer_Y[i];
    
            fifo_buffer_Z[i] = spix(0x00);
            fifo_buffer_Z[i] |= ((int)spix(0x00) << 8); // Combine with the received byte
            fifo_buffer_Z[i] = (fifo_buffer_Z[i] << 4) >> 4; // Shift left by 4 bits and then right by 4 bits to remove redundant bits
            z_rawvalue = fifo_buffer_Z[i];
        //    valuetowrite[i] = fifo_buffer_Z[i];
            }
            P1OUT |= BIT5;
    }
    
    
    //Set register function
    void Set_register_value(unsigned char address, unsigned char regValue)
    {
    //chip select low
    P1OUT &= (~BIT5);
    
    // send write register command 0x0A
    spix(0x0A);
    //send register address to write in
    spix(address);
    spix(regValue);//Byte to write in register
    P1OUT |= BIT5;
    return;
    }
    
    //Initialization of SPI
    void spiInit()
    {
    P1SEL1 |= BIT6 | BIT7; // MOSI AND MISO// USCI_B0 operation
    P2SEL1 |= BIT2; //CLK
    
    P1DIR &=~BIT7;//MISO as input
    P1DIR |= BIT3 | BIT5 | BIT6; //CHIP SELECT, MOSI as output
    P2DIR |=BIT2;//SCLK as output
    P1OUT |= BIT5;//set CS high, disable transmition
    P1OUT |= BIT3;//set CS high, disable transmition
    
    //PM5CTL0 &=~LOCKLPM5;
    
    
    UCB0CTLW0 = UCSWRST; // **Put state machine in reset**
    //UCB0CTLW0 |= UCMST | UCSYNC | UCMSB | UCCKPL; // 3-pin, 8-bit SPI master
    UCB0CTLW0 |= UCMST | UCSYNC | UCCKPH | (0* UCCKPL) | UCMSB;
    // Clock polarity high, MSB
    UCB0CTLW0 |= UCSSEL__SMCLK; // ACLK
    UCB0BR0 = 0x02; // /2
    UCB0BR1 = 0; //
    // UCB0MCTLW = 0; // No modulation
    UCB0CTLW0 &= ~UCSWRST; // **Initialize USCI state machine**
    
    }
    
    //inizialization SPI clock
    void clockInit()
    {
    CSCTL0_H = CSKEY >> 8; // Unlock CS registers
    CSCTL1 = DCOFSEL_0; // Set DCO to 1MHz
    CSCTL2 = SELA__LFXTCLK | SELS__DCOCLK | SELM__DCOCLK;
    CSCTL3 = DIVA__1 | DIVS__1 | DIVM__1; // set all dividers
    CSCTL0_H = 0; // Lock CS registers
    }
    
    //inizialization UART
    void UARTInit()
    {
        UCA1CTLW0 |= UCSWRST;
            //Next line selects SMCLK which is DCO (default frequency: 1 MHz)
            UCA1CTLW0 |=  UCSSEL1; //This writes 0x80 which sets BIT7
            //Next two lines divide 1 MHz to get 9600 baud
            UCA1BRW = 0x06;
            UCA1MCTLW |= UCOS16 + UCBRF3 + UCBRS5;
            UCA1CTLW0 &= ~UCSWRST;
    
    //        PM5CTL0 &= ~LOCKLPM5;
    
            /* Port 2
             P2.1  Button on Launchpad
             P2.2 UCB0 SPI CLK
             P2.5 UCA1 TXD UART
             P2.6 UCA1 RXD UART
            */
             P2DIR |= BIT0 + BIT1 + BIT2 + BIT3 + BIT4 + BIT7;
             P2SEL1 |= BIT2 + BIT5 + BIT6; //Configure pins for SPI CLK; UART on UCA1
    
    }
     but it does not work in this case i think it stuck in the if(fifowatermark) but doesnt run the readFIFO function i did not know if i implement the if condition properly. Another suggestion please

  • Are you fairly certain P4.3 is connected to INT1 (not INT2)?

    In the spirit of "solve one thing at a time", I suggest you set aside (don't set up) the pin interrupt and poll the pin directly e.g. "if ((P4IN & BIT3) == 0) {".

    Unsolicited: I also suggest you not set FIFO_SAMPLES=511, since the probability of overrun is very high. Try a smaller number, like maybe half-size (512/2). This isn't your problem now but it will be.

  • Hello again Bruce i am back. I follow your suggestion and it is working thank you so muchh i did not us interrupt i used directly if condition like you mention. In this case i am adding another device which is EM4325 UHF RFID chip which also use SPI protocol i want to write the z axis acceleration value to specific memory of the RFID chip. Please give me suggestion about this code i am confused how i can implement the writing data to the EM4325 UHF RFID chip do you think this is the correct way? so i am storing the acceleration value in the microcontroller buffer then write it one by one to the UHF RFID chip memory.

    #include <msp430.h>
    #include <stdio.h>
    #include <stdint.h>
    
    
    void clockInit(void);
    void spiInit(void);
    void UARTInit(void);
    unsigned char Get_register_value(unsigned char address);
    void Get_register_Burst(unsigned char address, unsigned char Rxbuf[], unsigned int BytesNum);
    void Set_register_value(unsigned char address, unsigned char regValue);
    void binaryToAscii(unsigned char binaryData, char asciiRepresentation[3]);
    void readFIFO();
    void EM4325initiate(void);
    void EM4325readmemory(void);
    void EM4325writememory(void);
    void EM4325readsensor(void);
    void EM4325readregisterfile(void);
    void EM4325writeregisterfile(void);
    //void setupaccint(void);
    void gpiosetup(void);
    
    unsigned int sensorData;
    unsigned int emsensor[9];
    unsigned int emmemory[3];
    unsigned int emregister[2];
    unsigned int emregistervalue[1];
    unsigned int valuetowrite[2];
    unsigned int firstbyte[1];
    unsigned int secondbyte[1];
    unsigned int emstatus;
    unsigned int const BytesNum = 3;
    unsigned char Rx_value, Tx_value,x_data, y_data, z_data;
    unsigned char X_data[BytesNum], Y_data[BytesNum], Z_data[BytesNum];
    volatile int fifowatermark=0;
    unsigned char rx_value;
    unsigned char Rxbuf[BytesNum];
    unsigned int i;
    unsigned int zdata;
    signed int zdatas;
    float zvalue;
    unsigned int sensorData;
    //unsigned int valuetowrite[170];
    volatile int fifo_buffer[170];
    volatile int fifo_buffer_X[170];
    volatile int fifo_buffer_Y[170];
    volatile int fifo_buffer_Z[170];
    unsigned int i=0;
    unsigned int axis=0;
    unsigned int stepCounter=0;
    unsigned int x_rawvalue;
    unsigned int y_rawvalue;
    unsigned int z_rawvalue;
    
    int main(void)
    {
    WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
    int i=0;
    int count=0;
    
    
    UCA1CTLW0 &= ~UCSWRST; //Takes UART A1 out of sw reset
    PM5CTL0 &= ~LOCKLPM5;
    
    clockInit();
    spiInit();
    UARTInit();
    gpiosetup();
    //setupaccint();
    
    __bis_SR_register(GIE);
    
    Set_register_value(0x1F, 0x52);//software reset
    __delay_cycles(6000);
    Set_register_value(0x29, 0xFF);//FIFO_SAMPLES
    Set_register_value(0x28, 0x09);//FIFO_CONTROL,
    Set_register_value(0x2A, 0x84);//INTMAP1 Data_ready mapped into INT1, failing edge
    Set_register_value(0x2B, 0xC0);//INTMAP2 activity mapped into INT2, failing edge
    Set_register_value(0x2C, 0x11);//FILTER_CTL, ODR = 25Hz,+-2g
    
    Set_register_value(0x2D, 0x02);//POWER_CTL Measurement mode, WAKE UP
    
    while(1){
        if((P4IN & BIT3) == 0){
    //   __delay_cycles(6000000);
    //    __bis_SR_register_on_exit(LPM4_bits);
        readFIFO();
        }
    
    
        else if((P3IN & BIT4) == 0){
        //wake up and start writing to the chip memory
        for (i = 0; i < 170; i++) {
        //chip select low to start transmission
        P1OUT &= ~BIT3;
    
        // send write register file command
        spix(0xEF);
    
        // send memory address to write
        spix(0x0001);
    
        // Value to write to memory
        valuetowrite[0] = fifo_buffer_Z[i]; //pass the accelerometer value here
        valuetowrite[1] = fifo_buffer_Z[i]; //pass the accelerometer value here this two are same
    
        // Split the 16-bit value into two 8-bit values
        valuetowrite[0] >>=8;
        firstbyte[0] = valuetowrite[0];
        valuetowrite[1] = valuetowrite[1] & 0xFF;
        secondbyte[0] = valuetowrite[1];      // Extract lower 8 bits (0x34)
    
        // send value to write to the register file
        spix(firstbyte[0]);
        __delay_cycles(2000); //0.3ms delay the time to write to the memory
        // send value to write to the register file
        spix(secondbyte[0]);
        __delay_cycles(2000); //0.3ms delay the time to write to the memory
    
        spix(0x00);
        emstatus=spix(0x00);;
        P1OUT |= BIT3;
        }
        }
    
        else if((P3IN & BIT4) == 1){
       //go to sleep
    //   __bis_SR_register(LPM4_bits);
       }
    
    }
    
    }
    /*
    void setupaccint(void){
        //setup switch 1
    
        //setup Interrupt IRQ 1
    //    P4IE |= BIT3; //Enable P4.5 IRQ
    //    __enable_interrupt(); //enable mask IRQs
    //    P4IFG &= ~BIT3; // Clear P4.1 IRQ Flag
        // Configure GPIO
        P4OUT = BIT3;                             // Pull-up resistor on P1.1
        P4REN = BIT3;                             // Select pull-up mode for P1.1
        P4DIR = 0xFF ^ BIT3;                      // Set all but P1.1 to output direction
        P4IES = BIT3;                             // P1.1 Hi/Lo edge
        P4IFG = 0;                                // Clear all P1 interrupt flags
        P4IE = BIT3;                              // P1.1 interrupt enabled
    }
    
    //// Interrupt Service Routine (ISR) What to do when interrupt occur
    #pragma vector = PORT4_VECTOR
    __interrupt void ISR_Port4_S3(void){ //interrupt FIFO full reading FIFO Data
        P4IES ^= BIT3;                            // Toggle interrupt edge
         fifowatermark = 1;
         P4IFG &= ~BIT3;
    }
    */
    
    void gpiosetup(){
        P4DIR &= ~BIT3; //enable the acc int pin as input.
        P4OUT |= BIT3; //enable the pull up pull down resistor
        P3DIR &= ~BIT4; //enable the rfid chip int pin as input.
        P3OUT |= BIT4; //enable the pull up pull down resistor
    
    }
    
    
    //Get Register value
    unsigned char Get_register_value(unsigned char address)
    {
    unsigned char rx_value;
    //chip select low
    //chip select low
    P1OUT &= (~BIT5);
    
    // send write register command 0x0A
    spix(0x0B);
    //send register address to write in
    spix(address);
    rx_value = spix(0xFF);//dummybyte
    P1OUT |= BIT5;
    return rx_value;
    }
    
    uint8_t spix(uint8_t c)
    {
      while (!(UCB0IFG & UCTXIFG)) /*EMPTY*/;
      UCB0TXBUF = c;
      while (!(UCB0IFG & UCRXIFG)) /*EMPTY*/;
      c = UCB0RXBUF;
      return(c);
    }
    
    void readFIFO(){
        P1OUT &= ~BIT5;
            // send read FIFO register command 0x0D
            spix(0x0D);
    
    
            // b15-b14 : 00-x 01-y 10-z 11-temp
            // b11-b0  : data
            for (i = 0; i < 170; i++) {
            // each fifo sample has 16 bits, 2 bits of axis information, 2 bits redundant, 12 bits of data
            // Send dummy byte and receive data for X axis
            fifo_buffer_X[i] = spix(0x00);
            fifo_buffer_X[i] |= ((int)spix(0x00) << 8); // Combine with the received byte
            fifo_buffer_X[i] = (fifo_buffer_X[i] << 4) >> 4; // Shift left by 4 bits and then right by 4 bits to remove redundant bits
            x_rawvalue = fifo_buffer_X[i];
    
            fifo_buffer_Y[i] = spix(0x00);
            fifo_buffer_Y[i] |= ((int)spix(0x00) << 8); // Combine with the received byte
            fifo_buffer_Y[i] = (fifo_buffer_Y[i] << 4) >> 4; // Shift left by 4 bits and then right by 4 bits to remove redundant bits
            y_rawvalue = fifo_buffer_Y[i];
    
            fifo_buffer_Z[i] = spix(0x00);
            fifo_buffer_Z[i] |= ((int)spix(0x00) << 8); // Combine with the received byte
            fifo_buffer_Z[i] = (fifo_buffer_Z[i] << 4) >> 4; // Shift left by 4 bits and then right by 4 bits to remove redundant bits
            z_rawvalue = fifo_buffer_Z[i];
        //    valuetowrite[i] = fifo_buffer_Z[i];
            }
            P1OUT |= BIT5;
    }
    
    
    //Set register function
    void Set_register_value(unsigned char address, unsigned char regValue)
    {
    //chip select low
    P1OUT &= (~BIT5);
    
    // send write register command 0x0A
    spix(0x0A);
    //send register address to write in
    spix(address);
    spix(regValue);//Byte to write in register
    P1OUT |= BIT5;
    return;
    }
    
    //Initialization of SPI
    void spiInit()
    {
    P1SEL1 |= BIT6 | BIT7; // MOSI AND MISO// USCI_B0 operation
    P2SEL1 |= BIT2; //CLK
    
    P1DIR &=~BIT7;//MISO as input
    P1DIR |= BIT3 | BIT5 | BIT6; //CHIP SELECT, MOSI as output
    P2DIR |=BIT2;//SCLK as output
    P1OUT |= BIT5;//set CS high, disable transmition
    P1OUT |= BIT3;//set CS high, disable transmition
    
    //PM5CTL0 &=~LOCKLPM5;
    
    
    UCB0CTLW0 = UCSWRST; // **Put state machine in reset**
    //UCB0CTLW0 |= UCMST | UCSYNC | UCMSB | UCCKPL; // 3-pin, 8-bit SPI master
    UCB0CTLW0 |= UCMST | UCSYNC | UCCKPH | (0* UCCKPL) | UCMSB;
    // Clock polarity high, MSB
    UCB0CTLW0 |= UCSSEL__SMCLK; // ACLK
    UCB0BR0 = 0x02; // /2
    UCB0BR1 = 0; //
    // UCB0MCTLW = 0; // No modulation
    UCB0CTLW0 &= ~UCSWRST; // **Initialize USCI state machine**
    
    }
    
    //inizialization SPI clock
    void clockInit()
    {
    CSCTL0_H = CSKEY >> 8; // Unlock CS registers
    CSCTL1 = DCOFSEL_0; // Set DCO to 1MHz
    CSCTL2 = SELA__LFXTCLK | SELS__DCOCLK | SELM__DCOCLK;
    CSCTL3 = DIVA__1 | DIVS__1 | DIVM__1; // set all dividers
    CSCTL0_H = 0; // Lock CS registers
    }
    
    //inizialization UART
    void UARTInit()
    {
        UCA1CTLW0 |= UCSWRST;
            //Next line selects SMCLK which is DCO (default frequency: 1 MHz)
            UCA1CTLW0 |=  UCSSEL1; //This writes 0x80 which sets BIT7
            //Next two lines divide 1 MHz to get 9600 baud
            UCA1BRW = 0x06;
            UCA1MCTLW |= UCOS16 + UCBRF3 + UCBRS5;
            UCA1CTLW0 &= ~UCSWRST;
    
    //        PM5CTL0 &= ~LOCKLPM5;
    
            /* Port 2
             P2.1  Button on Launchpad
             P2.2 UCB0 SPI CLK
             P2.5 UCA1 TXD UART
             P2.6 UCA1 RXD UART
            */
             P2DIR |= BIT0 + BIT1 + BIT2 + BIT3 + BIT4 + BIT7;
             P2SEL1 |= BIT2 + BIT5 + BIT6; //Configure pins for SPI CLK; UART on UCA1
    
    }
    
    //try to check EM4325 if it is working
    void EM4325initiate()
    {
        //chip select low to start transmission
        P1OUT &= ~BIT3;
    
       // send initiate command
       spix(0xE0);
       spix(0x00);
       emstatus=spix(0x00);;
       //chip select high to stop transmission
       P1OUT |= BIT3;
    }
    
    void EM4325readmemory()
    {
        //chip select low to start transmission
        P1OUT &= ~BIT3;
    
        // send read command
        spix(0xE7);
    
        // send memory address to read
        spix(0x00F2);
    
        //obtain EM4325 memory value
        while(!(UCB0IFG & UCTXIFG));
        spix(0x00);
        emstatus=spix(0x00);
        emmemory[0] = spix(0x00);
        emmemory[1] = spix(0x00);;
        //chip select high to stop transmission
        P1OUT |= BIT3;
    }
    
    void EM4325writememory()
    {
      //chip select low to start transmission
      P1OUT &= ~BIT3;
       spix(0xE8);
    
       // send memory address to write
       spix(0x00F2);
       // send word address to write
       spix(0x8000);
    
       //obtain EM4325 memory value
       while(!(UCB0IFG & UCTXIFG));
       spix(0x00);
       emstatus=spix(0x00);
    
       //chip select high to stop transmission
       P1OUT |= BIT3;
    }
    
    void EM4325writeregisterfile()
    {
        //chip select low to start transmission
        P1OUT &= ~BIT3;
    
       // send write register file command
       spix(0xEF);
    
       // send memory address to write
       spix(0x0001);
    
       // Value to write to memory
       valuetowrite[0] = 0x1234; //pass the accelerometer value here
       valuetowrite[1] = 0x1234; //pass the accelerometer value here this two are same
    
       // Split the 16-bit value into two 8-bit values
       valuetowrite[0] >>=8;
       firstbyte[0] = valuetowrite[0];
       valuetowrite[1] = valuetowrite[1] & 0xFF;
       secondbyte[0] = valuetowrite[1];      // Extract lower 8 bits (0x34)
    
       // send value to write to the register file
       spix(firstbyte[0]);
    
       // send value to write to the register file
       spix(secondbyte[0]);
    
       spix(0x00);
       emstatus=spix(0x00);;
       P1OUT |= BIT3;
    
    }
    
    void EM4325readregisterfile()
    {
        //chip select low to start transmission
        P1OUT &= ~BIT3;
    
        // send read register file command
        spix(0xEE);
    
        // send register address to read
        spix(0x0007);
    
    //   // send memory address to read
        //   while(!(UCB0IFG & UCTXIFG));
        //   UCB0TXBUF = 0x0012;
    
       //obtain EM4325 memory value
       spix(0x00);
       emstatus=spix(0x00);;
       emregister[0]=spix(0x00);
       emregister[1]=spix(0x00);;
    
       emregister[0] <<=8;
    
       emregistervalue[0] = emregister[0]+emregister[1];
    
       P1OUT |= BIT3;
    }
    
    
    
    

  • I'm not familiar with this device, but I suppose what you're doing is OK. This code writes (repeatedly) to the Register File (0xEF) rather than to the EEPROM (0xE8/0xEA), so I suppose something in the device knows what to do with it.

    I'm not sure you need the delay between writing the two bytes.

    Unsolicited:

    >   else if((P3IN & BIT4) == 1){

    This test is always False, since (BIT4 != 1). This is probably better stated:

    >  else if((P3IN & BIT4) == BIT4){

  • Better to leave out the "== BIT4" entirely.

    But if you must, use "!= 0" and "== 0".

  • Thank you so much guys for your reply sorry i am back a bit late as i am doing some experiment around. I will try both of your suggestion after this in this case i wanted to ask regarding the FIFO buffer in the accelerometer hope you know about it. In this case when i setup the output data rate of the accelerometer to 25 Hz it means that 25 data stored in the FIFO per second then when i retrieve the data from the FIFO if i get 25 data from the FIFO this data means  1 second data right? then if set the ODR to 100 Hz then i get 100 data from the FIFO it means 1 second data right?. Because later on i need to do some signal processing to my acceleration data and i will need timestamp and i think with using FIFO the data will not be in realtime right?. 

  • When you read the data sheet, be careful to distinguish a "sample" (one 16-bit word) from a "sample set" (three words in your case). It makes a difference since the FIFO size isn't an integer number of sample sets.

    So ODR=25Hz means 25 sample sets / second.

  • You mean that sample is like one sample of one x axis data bit which consist of 16 bit rght? Then sample set is the x,y,z in one package? sorry if i did not catch it. So if the sample set is x,y,w in one package when i define 25 Hz = 25 sample set per second so i will get 25 x samples, 25 y samples, and 25 z samples is it like this? 

  • Yes, 25Hz is 25 sample sets/second, where a sample set is of the form {x,y,z}. You should always read a multiple of a sample set (3 words) to avoid putting the FIFO out of order.

  • Hello again bruce,

    I am back hope you are still around i have a question in this case i dont want to have a data loss during the data transfer process because the data transfer rate between the RFID tag and RFID reader is low approximately 20-30 read per second in this case my code is still the same please find it below in this case according to my code i get the acceleration value from the Accelerometer FIFO buffer then save it into my microcontroller buffer. Then later on i write it into the RFID chip memory. in this case i want to write the data saved in the microcontroller buffer in the same order and i dont want any data lose for example before all data transferred into the RFID chip memory the data in the microcontroller already written by new data acquired from the accelerometer FIFO buffer. Did you have any suggestion about this and how to avoid this. Thank youu.

    #include <msp430.h>
    #include <stdio.h>
    #include <stdint.h>
    
    
    void clockInit(void);
    void spiInit(void);
    void UARTInit(void);
    unsigned char Get_register_value(unsigned char address);
    void Get_register_Burst(unsigned char address, unsigned char Rxbuf[], unsigned int BytesNum);
    void Set_register_value(unsigned char address, unsigned char regValue);
    void binaryToAscii(unsigned char binaryData, char asciiRepresentation[3]);
    void readFIFO();
    void EM4325initiate(void);
    void EM4325readmemory(void);
    void EM4325writememory(void);
    void EM4325readsensor(void);
    void EM4325readregisterfile(void);
    void EM4325writeregisterfile(void);
    //void setupaccint(void);
    void gpiosetup(void);
    
    unsigned int sensorData;
    unsigned int emsensor[9];
    unsigned int emmemory[3];
    unsigned int emregister[2];
    unsigned int emregistervalue[1];
    unsigned int valuetowrite[2];
    unsigned int firstbyte[1];
    unsigned int secondbyte[1];
    unsigned int emstatus;
    unsigned int const BytesNum = 3;
    unsigned char Rx_value, Tx_value,x_data, y_data, z_data;
    unsigned char X_data[BytesNum], Y_data[BytesNum], Z_data[BytesNum];
    volatile int fifowatermark=0;
    unsigned char rx_value;
    unsigned char Rxbuf[BytesNum];
    unsigned int i;
    unsigned int zdata;
    signed int zdatas;
    float zvalue;
    unsigned int sensorData;
    //unsigned int valuetowrite[170];
    volatile int fifo_buffer[170];
    volatile int fifo_buffer_X[170];
    volatile int fifo_buffer_Y[170];
    volatile int fifo_buffer_Z[170];
    unsigned int i=0;
    unsigned int axis=0;
    unsigned int stepCounter=0;
    unsigned int x_rawvalue;
    unsigned int y_rawvalue;
    unsigned int z_rawvalue;
    
    int main(void)
    {
    WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
    int i=0;
    int count=0;
    
    
    UCA1CTLW0 &= ~UCSWRST; //Takes UART A1 out of sw reset
    PM5CTL0 &= ~LOCKLPM5;
    
    clockInit();
    spiInit();
    UARTInit();
    gpiosetup();
    //setupaccint();
    
    __bis_SR_register(GIE);
    
    Set_register_value(0x1F, 0x52);//software reset
    __delay_cycles(6000);
    Set_register_value(0x29, 0xFF);//FIFO_SAMPLES
    Set_register_value(0x28, 0x09);//FIFO_CONTROL,
    Set_register_value(0x2A, 0x84);//INTMAP1 Data_ready mapped into INT1, failing edge
    Set_register_value(0x2B, 0xC0);//INTMAP2 activity mapped into INT2, failing edge
    Set_register_value(0x2C, 0x11);//FILTER_CTL, ODR = 25Hz,+-2g
    
    Set_register_value(0x2D, 0x02);//POWER_CTL Measurement mode, WAKE UP
    
    while(1){
        if((P4IN & BIT3) == 0){
    //   __delay_cycles(6000000);
    //    __bis_SR_register_on_exit(LPM4_bits);
        readFIFO();
        }
    
    
        else if((P3IN & BIT4) == 0){
        //wake up and start writing to the chip memory
        for (i = 0; i < 170; i++) {
        //chip select low to start transmission
        P1OUT &= ~BIT3;
    
        // send write register file command
        spix(0xEF);
    
        // send memory address to write
        spix(0x0001);
    
        // Value to write to memory
        valuetowrite[0] = fifo_buffer_Z[i]; //pass the accelerometer value here
        valuetowrite[1] = fifo_buffer_Z[i]; //pass the accelerometer value here this two are same
    
        // Split the 16-bit value into two 8-bit values
        valuetowrite[0] >>=8;
        firstbyte[0] = valuetowrite[0];
        valuetowrite[1] = valuetowrite[1] & 0xFF;
        secondbyte[0] = valuetowrite[1];      // Extract lower 8 bits (0x34)
    
        // send value to write to the register file
        spix(firstbyte[0]);
        __delay_cycles(2000); //0.3ms delay the time to write to the memory
        // send value to write to the register file
        spix(secondbyte[0]);
        __delay_cycles(2000); //0.3ms delay the time to write to the memory
    
        spix(0x00);
        emstatus=spix(0x00);;
        P1OUT |= BIT3;
        }
        }
    
        else if((P3IN & BIT4) == 1){
       //go to sleep
    //   __bis_SR_register(LPM4_bits);
       }
    
    }
    
    }
    /*
    void setupaccint(void){
        //setup switch 1
    
        //setup Interrupt IRQ 1
    //    P4IE |= BIT3; //Enable P4.5 IRQ
    //    __enable_interrupt(); //enable mask IRQs
    //    P4IFG &= ~BIT3; // Clear P4.1 IRQ Flag
        // Configure GPIO
        P4OUT = BIT3;                             // Pull-up resistor on P1.1
        P4REN = BIT3;                             // Select pull-up mode for P1.1
        P4DIR = 0xFF ^ BIT3;                      // Set all but P1.1 to output direction
        P4IES = BIT3;                             // P1.1 Hi/Lo edge
        P4IFG = 0;                                // Clear all P1 interrupt flags
        P4IE = BIT3;                              // P1.1 interrupt enabled
    }
    
    //// Interrupt Service Routine (ISR) What to do when interrupt occur
    #pragma vector = PORT4_VECTOR
    __interrupt void ISR_Port4_S3(void){ //interrupt FIFO full reading FIFO Data
        P4IES ^= BIT3;                            // Toggle interrupt edge
         fifowatermark = 1;
         P4IFG &= ~BIT3;
    }
    */
    
    void gpiosetup(){
        P4DIR &= ~BIT3; //enable the acc int pin as input.
        P4OUT |= BIT3; //enable the pull up pull down resistor
        P3DIR &= ~BIT4; //enable the rfid chip int pin as input.
        P3OUT |= BIT4; //enable the pull up pull down resistor
    
    }
    
    
    //Get Register value
    unsigned char Get_register_value(unsigned char address)
    {
    unsigned char rx_value;
    //chip select low
    //chip select low
    P1OUT &= (~BIT5);
    
    // send write register command 0x0A
    spix(0x0B);
    //send register address to write in
    spix(address);
    rx_value = spix(0xFF);//dummybyte
    P1OUT |= BIT5;
    return rx_value;
    }
    
    uint8_t spix(uint8_t c)
    {
      while (!(UCB0IFG & UCTXIFG)) /*EMPTY*/;
      UCB0TXBUF = c;
      while (!(UCB0IFG & UCRXIFG)) /*EMPTY*/;
      c = UCB0RXBUF;
      return(c);
    }
    
    void readFIFO(){
        P1OUT &= ~BIT5;
            // send read FIFO register command 0x0D
            spix(0x0D);
    
    
            // b15-b14 : 00-x 01-y 10-z 11-temp
            // b11-b0  : data
            for (i = 0; i < 170; i++) {
            // each fifo sample has 16 bits, 2 bits of axis information, 2 bits redundant, 12 bits of data
            // Send dummy byte and receive data for X axis
            fifo_buffer_X[i] = spix(0x00);
            fifo_buffer_X[i] |= ((int)spix(0x00) << 8); // Combine with the received byte
            fifo_buffer_X[i] = (fifo_buffer_X[i] << 4) >> 4; // Shift left by 4 bits and then right by 4 bits to remove redundant bits
            x_rawvalue = fifo_buffer_X[i];
    
            fifo_buffer_Y[i] = spix(0x00);
            fifo_buffer_Y[i] |= ((int)spix(0x00) << 8); // Combine with the received byte
            fifo_buffer_Y[i] = (fifo_buffer_Y[i] << 4) >> 4; // Shift left by 4 bits and then right by 4 bits to remove redundant bits
            y_rawvalue = fifo_buffer_Y[i];
    
            fifo_buffer_Z[i] = spix(0x00);
            fifo_buffer_Z[i] |= ((int)spix(0x00) << 8); // Combine with the received byte
            fifo_buffer_Z[i] = (fifo_buffer_Z[i] << 4) >> 4; // Shift left by 4 bits and then right by 4 bits to remove redundant bits
            z_rawvalue = fifo_buffer_Z[i];
        //    valuetowrite[i] = fifo_buffer_Z[i];
            }
            P1OUT |= BIT5;
    }
    
    
    //Set register function
    void Set_register_value(unsigned char address, unsigned char regValue)
    {
    //chip select low
    P1OUT &= (~BIT5);
    
    // send write register command 0x0A
    spix(0x0A);
    //send register address to write in
    spix(address);
    spix(regValue);//Byte to write in register
    P1OUT |= BIT5;
    return;
    }
    
    //Initialization of SPI
    void spiInit()
    {
    P1SEL1 |= BIT6 | BIT7; // MOSI AND MISO// USCI_B0 operation
    P2SEL1 |= BIT2; //CLK
    
    P1DIR &=~BIT7;//MISO as input
    P1DIR |= BIT3 | BIT5 | BIT6; //CHIP SELECT, MOSI as output
    P2DIR |=BIT2;//SCLK as output
    P1OUT |= BIT5;//set CS high, disable transmition
    P1OUT |= BIT3;//set CS high, disable transmition
    
    //PM5CTL0 &=~LOCKLPM5;
    
    
    UCB0CTLW0 = UCSWRST; // **Put state machine in reset**
    //UCB0CTLW0 |= UCMST | UCSYNC | UCMSB | UCCKPL; // 3-pin, 8-bit SPI master
    UCB0CTLW0 |= UCMST | UCSYNC | UCCKPH | (0* UCCKPL) | UCMSB;
    // Clock polarity high, MSB
    UCB0CTLW0 |= UCSSEL__SMCLK; // ACLK
    UCB0BR0 = 0x02; // /2
    UCB0BR1 = 0; //
    // UCB0MCTLW = 0; // No modulation
    UCB0CTLW0 &= ~UCSWRST; // **Initialize USCI state machine**
    
    }
    
    //inizialization SPI clock
    void clockInit()
    {
    CSCTL0_H = CSKEY >> 8; // Unlock CS registers
    CSCTL1 = DCOFSEL_0; // Set DCO to 1MHz
    CSCTL2 = SELA__LFXTCLK | SELS__DCOCLK | SELM__DCOCLK;
    CSCTL3 = DIVA__1 | DIVS__1 | DIVM__1; // set all dividers
    CSCTL0_H = 0; // Lock CS registers
    }
    
    //inizialization UART
    void UARTInit()
    {
        UCA1CTLW0 |= UCSWRST;
            //Next line selects SMCLK which is DCO (default frequency: 1 MHz)
            UCA1CTLW0 |=  UCSSEL1; //This writes 0x80 which sets BIT7
            //Next two lines divide 1 MHz to get 9600 baud
            UCA1BRW = 0x06;
            UCA1MCTLW |= UCOS16 + UCBRF3 + UCBRS5;
            UCA1CTLW0 &= ~UCSWRST;
    
    //        PM5CTL0 &= ~LOCKLPM5;
    
            /* Port 2
             P2.1  Button on Launchpad
             P2.2 UCB0 SPI CLK
             P2.5 UCA1 TXD UART
             P2.6 UCA1 RXD UART
            */
             P2DIR |= BIT0 + BIT1 + BIT2 + BIT3 + BIT4 + BIT7;
             P2SEL1 |= BIT2 + BIT5 + BIT6; //Configure pins for SPI CLK; UART on UCA1
    
    }
    
    //try to check EM4325 if it is working
    void EM4325initiate()
    {
        //chip select low to start transmission
        P1OUT &= ~BIT3;
    
       // send initiate command
       spix(0xE0);
       spix(0x00);
       emstatus=spix(0x00);;
       //chip select high to stop transmission
       P1OUT |= BIT3;
    }
    
    void EM4325readmemory()
    {
        //chip select low to start transmission
        P1OUT &= ~BIT3;
    
        // send read command
        spix(0xE7);
    
        // send memory address to read
        spix(0x00F2);
    
        //obtain EM4325 memory value
        while(!(UCB0IFG & UCTXIFG));
        spix(0x00);
        emstatus=spix(0x00);
        emmemory[0] = spix(0x00);
        emmemory[1] = spix(0x00);;
        //chip select high to stop transmission
        P1OUT |= BIT3;
    }
    
    void EM4325writememory()
    {
      //chip select low to start transmission
      P1OUT &= ~BIT3;
       spix(0xE8);
    
       // send memory address to write
       spix(0x00F2);
       // send word address to write
       spix(0x8000);
    
       //obtain EM4325 memory value
       while(!(UCB0IFG & UCTXIFG));
       spix(0x00);
       emstatus=spix(0x00);
    
       //chip select high to stop transmission
       P1OUT |= BIT3;
    }
    
    void EM4325writeregisterfile()
    {
        //chip select low to start transmission
        P1OUT &= ~BIT3;
    
       // send write register file command
       spix(0xEF);
    
       // send memory address to write
       spix(0x0001);
    
       // Value to write to memory
       valuetowrite[0] = 0x1234; //pass the accelerometer value here
       valuetowrite[1] = 0x1234; //pass the accelerometer value here this two are same
    
       // Split the 16-bit value into two 8-bit values
       valuetowrite[0] >>=8;
       firstbyte[0] = valuetowrite[0];
       valuetowrite[1] = valuetowrite[1] & 0xFF;
       secondbyte[0] = valuetowrite[1];      // Extract lower 8 bits (0x34)
    
       // send value to write to the register file
       spix(firstbyte[0]);
    
       // send value to write to the register file
       spix(secondbyte[0]);
    
       spix(0x00);
       emstatus=spix(0x00);;
       P1OUT |= BIT3;
    
    }
    
    void EM4325readregisterfile()
    {
        //chip select low to start transmission
        P1OUT &= ~BIT3;
    
        // send read register file command
        spix(0xEE);
    
        // send register address to read
        spix(0x0007);
    
    //   // send memory address to read
        //   while(!(UCB0IFG & UCTXIFG));
        //   UCB0TXBUF = 0x0012;
    
       //obtain EM4325 memory value
       spix(0x00);
       emstatus=spix(0x00);;
       emregister[0]=spix(0x00);
       emregister[1]=spix(0x00);;
    
       emregister[0] <<=8;
    
       emregistervalue[0] = emregister[0]+emregister[1];
    
       P1OUT |= BIT3;
    }

**Attention** This is a public forum