最近在用5310单片机做一个与蓝牙通讯的试验,蓝牙芯片为主,而单片机为从,查找了资料及相关例程,拼凑了一个关于单片机为从机的配置程序,仿真起来不是很顺,望各位大虾给点意见啊,现将单片机部分的关于SPI总线通讯的程序代码贴出来如下,望给检查下的啊。
void SPIInterfaceConfig() //SPI从机配置
{
while (!(P4IN & 0x01)); // P4.0If clock sig from mstr stays low,
// it is not yet in SPI mode
P4SEL = 0x31; // P4.0,4,5 USCI_A0 option select
UCA1CTL1 = UCSWRST; // **Put state machine in reset**
UCA1CTL0 |= UCCKPL+UCSYNC; // 3-pin, 8-bit slave SPI,LSB firstUCCKPH+
UCA1CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCA1IE |= UCRXIE;
}
#pragma vector=USCI_A1_VECTOR //主要用于对于GSM数据的通讯
__interrupt void USCI_A1_ISR(void)
{
while (!(UCA1IFG&UCTXIFG)); // USCI_A0 TX buffer ready?
ucSPIData = UCA1RXBUF; //
bSPIIF =0x01; //ucSPIData为从蓝牙接收到的数据
}
//SPI 写字节 Host -> ANT
void WriteByte(uchar ucByte)
//------------------------------------------------------------------------------
{
UCA1TXBUF = ucByte; //发送数据到蓝牙芯片
while(SEN); //等待SEN引脚变低 (SEN引脚高电平指示AP2芯片忙,禁止写入)
SYNC_SRDY_PULSE(); //产生SRDY负脉冲
// Dly15uS(); //延时15uS.
// while (!(IFG2 & UCA0TXIFG));
Dly50uS();
while (!(UCA1IFG&UCTXIFG));
_NOP();
} |