外部主机模式为CPOL=0,上升沿读取数据:
void SPI0_Init(void)
{
P3SEL |= 0x0F; // P3.1-4 SPI option select
UCTL0 |= SWRST;
UCTL0 |= (CHAR + SYNC); //MSB first,8bit,1 stop,slave mode
//UTCTL0 = CKPH + STC; // CLK from Out, 3-pin SPI
UTCTL0 = CKPH; // CLK from Out, 4-pin SPI
UMCTL0 = 0x00; // no modulation
ME1 |= USPIE0; // Enable USART0 SPI mode
UCTL0 &= ~SWRST; // Initalize USART state machine
}
void SPI0_SendByte(unsigned char dat)
{
while (!(IFG1 & UTXIFG0)); // USART0 TX buffer ready?
TXBUF0 = dat; // dat to TXBUF0
}
void SPI_Send4Bytes( unsigned long tempData )
{
SPI0_SendByte((unsigned char)((tempData>>24)&0x000000ff));
SPI0_SendByte((unsigned char)((tempData>>16)&0x000000ff));
SPI0_SendByte((unsigned char)((tempData>>8)&0x000000ff));
SPI0_SendByte((unsigned char)((tempData)&0x000000ff));
}
下面是主循环部分:
while(1)
{
if(!(P3IN & BIT0)) //外部设备提供SSN信号
{
spiSendData = 0xAA010200;
SPI_Send4Bytes(spiSendData);
}
}
|