240
while(UCA0STAT & UCBUSY);
}
unsigned char SPI_read(void)
{
unsigned char rx_data = 0;
while(!(IFG2 & UCA0RXIFG));
rx_data = UCA0RXBUF;
while(UCA0STAT & UCBUSY);
return rx_data;
}
unsigned char SPI_transfer(unsigned char tx_data)
{
unsigned char rx_data = 0;
while(!(IFG2 & UCA0TXIFG));
UCA0TXBUF = tx_data;
while(UCA0STAT & UCBUSY);
while(!(IFG2 & UCA0RXIFG));
rx_data = UCA0RXBUF;
while(UCA0STAT & UCBUSY);
return rx_data;
}
The SPI read and write processes are simplest to understand. Before starting communication,
respective data transaction interrupt flags are polled. Note that these flags are needed even if we
don’t use USCI interrupts. Once polled okay, data is sent from MSP430 device in SPI write mode or
received while in read mode. Then we have to check if data has been fully received/transmitted by
asserting the USCI busy flag. Lastly the USCI SPI transfer function is a mixture of both SPI read and
write functions.
The code here is used to read a MPL115A1 barometric pressure sensor and display atmospheric
pressure-temperature data. MPL115A1 uses full-duplex SPI communication medium to communicate
with its host device and here MSP430’s USCI_A0 in SPI mode is employed to achieved that.