MSP430的 SPI读取AFE4400寄存器值的代码
[复制链接]
本帖最后由 Aguilera 于 2020-5-6 21:35 编辑
unsigned long AFE4400_Reg_Read(unsigned char Reg_address)
{
unsigned char SPI_Rx_buf[4]; //存放读取到的寄存器值
unsigned long retVal;
retVal = 0;
P1OUT&= ~BIT5; // 拉低STE
UCB0TXBUF = Reg_address; // 发送需要读取的寄存器地址
while ( (UCB0STAT & UCBUSY) ); // USCI_B0 TX buffer ready?
SPI_Rx_buf[0] = UCB0RXBUF; // 读取接收到的数据,此时为空数据
UCB0TXBUF = 0; // 空指令,等待延时作用
while ( (UCB0STAT & UCBUSY) ); // USCI_B0 TX buffer ready?
SPI_Rx_buf[1] = UCB0RXBUF; // 读取接收到的数据: Data[23:16]
UCB0TXBUF = 0; // 空指令,等待延时作用
while ( (UCB0STAT & UCBUSY) ); // USCI_B0 TX buffer ready?
SPI_Rx_buf[2] = UCB0RXBUF; // 读取接收到的数据: Data[15:8]
UCB0TXBUF = 0; // 空指令,等待延时作用
while ( (UCB0STAT & UCBUSY) ); // USCI_B0 TX buffer ready?
SPI_Rx_buf[3] = UCB0RXBUF; // 读取接收到的数据: Data[7:0]
P1OUT|=BIT5; // 读取完成,拉高STE
retVal = SPI_Rx_buf[1]; //数据整合成24位数据
retVal = (retVal << 8) | SPI_Rx_buf[2];
retVal = (retVal << 8) | SPI_Rx_buf[3];
return retVal;
}
|