void Init_SPI(void) {
// SCS - P2.0
// SCLK - P1.5
// MISO - P1.6
// MOSI - P1.7
P1DIR |= BIT5 + BIT6 + BIT7;
P1SEL |= BIT5 + BIT6 + BIT7;
P1SEL2 |= BIT5 + BIT6 + BIT7;
P2DIR |= BIT0;
P2OUT |= BIT0;
// USCI B0 SPI
UCB0CTL1 = UCSWRST; //USCI 逻辑保持在复位状态
UCB0CTL1 |= UCSSEL_2; // source USCI clock from SMCLK
UCB0CTL0 = SPI_MODE_0 | UCMSB | UCMST | UCSYNC ; // SPI MODE 0 ,MSB优先, 3-pin,8-bit SPI master。注:#define SPI_MODE_0 (UCCKPH)
UCB0CTL1 &= ~UCSWRST; // USCI 复位操作释放
}
void MSP430_SPI_write_byte(u_char byte)
{
// USCI_B0 SPI Mode
while (!(UC0IFG & UCB0TXIFG)) //UC0IFG就是IFG2
; // wait for previous tx to complete
UCB0TXBUF = byte; // setting TXBUF clears the TXIFG flag
}
uint8 MSP430_SPI_read_byte(void) //uint8 在其他地方已定义
{
while (!(UC0IFG & UCB0RXIFG)) //UC0IFG就是IFG2
; // wait for an rx character?
return UCB0RXBUF; // reading clears RXIFG flag
} |