Hi everyone,
When I try to make SPI interface between ADS1298 and MSP430F5529, it always doesn't work. Hope to get some help.
The following is the code.
#define CS_HIGH (P2OUT |= 0x20)
#define CS_LOW (P2OUT &= ~0x20)
//the setup for MSP430F66xx spi.
void spi_setup(void)
{
UCA0CTL1 |= UCSWRST; // **Put state machine in reset**
UCA0CTL0 |= UCMST+UCSYNC+UCMSB; // 3-pin, 8-bit SPI master, MSB
UCA0CTL1 |= UCSSEL_2; // SMCLK=8MHz
UCA0BR0 = 0x0f; // /15
UCA0BR1 = 0; //
UCA0MCTL = 0; // No modulation
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
}
//the spi write and read function
uint8_t spi_WriteReadByte(uint8_t txdata)
{
uint8_t retry = 0, rx;
CS_LOW;
while ((UCA0IFG&0x02) == 0) //txbuf empty flag
{
retry++;
if (retry > 200)
{
return 0;
}
}
UCA0TXBUF = txdata;
retry = 0;
while ((UCA0STAT&0x1)) //busy flag
{
retry++;
if (retry > 200)
{
return 0;
}
}
rx = UCA0RXBUF;
CS_HIGH;
return rx;
}
// to read the reg
uint8_t spi_read_reg(uint8_t addr, uint8_t len, uint8_t dat[])
{
uint8_t i;
CS_LOW;
spi_WriteReadByte(addr+0x20);
spi_WriteReadByte(len-1);
for (i = 0; i < len; i++)
{
dat[i]=spi_WriteReadByte(0xff);
}
CS_HIGH;
return 0;
}
void TI_ADS1298_Init(void)
{
uint8_t d;
P2OUT &= ~0x10; //p2.4 start=0
P2OUT |= 0x01; //p2.0 reset=1
__delay_cycles(8000000); // delay 1s
P2OUT &= ~0x01; //p2.0 reset=0
__delay_cycles(800000); // delay 100ms
P2OUT |= 0x01; //p2.0 reset=1
__delay_cycles(8000); // delay 1ms
spi_WriteReadByte(ADS1298_SDATAC);
__delay_cycles(64); // delay 8us
spi_WriteReadByte(ADS1298_STOP);
__delay_cycles(64); // delay 8us
spi_read_reg(0x00, 1, &d);
.... ....
}
The device id (0x92) can't be read out, when debuging, the UCA0RXBUF is always 0xFF.
What's wrong with it ?
Lots of thanks !