ATmega8从机:
void spi_init(void) { SPCR = (1<<SPE)|(1<<CPHA); // 从机模式,前沿输出,后沿采样,MSB先,闲置低电平 SPSR = 0x00; }
uint8 spi_send_byte(uint8 data) //循环等待主机读取读取 { SPDR = data; while (!(SPSR & (1<<SPIF))); data = SPDR; //清SPIF位 return 1; }
主机MSP430F2012: void spi_init(void) //SPI初始化 { USICTL0 = USIPE7+USIPE6+USIPE5+USIMST+USIOE+USISWRST; // SPI Port使能,SPI输出使能, SPI master USICTL1 = 0; // 前沿输出,后沿采样 USICKCTL = USIDIV_5 + USISSEL_2; // 时钟源SMCLK,32分频 250k TI_CC_SPI_USART0_PxSEL |= TI_CC_SPI_USART0_SIMO | TI_CC_SPI_USART0_SOMI | TI_CC_SPI_USART0_UCLK; // SPI option select TI_CC_SPI_USART0_PxDIR |= TI_CC_SPI_USART0_SIMO + TI_CC_SPI_USART0_UCLK; USICTL0 &= ~USISWRST; }
uint8 spiReadByte(void) { uint8 rByte; P2OUT &= ~Test_cs; USICNT=8; while(!(USICTL1 & USIIFG)) ; //等接收完 rByte = USISRL; //接收数据 P2OUT |= Test_cs; return rByte; }
请问设置对吗? 怎么通讯不起来?
|