库有宏定义SPI_Direction_1Line_Rx。
用中断法读74hc165数据。
初始化配置:
/* SPI1 configuration */
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Rx; //SPI设置为双线双向全双工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //设置为主 SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //SPI发送接收 8 位帧结构
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //时钟悬空高
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //数据捕获于第二个时钟沿
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //内部 NSS 信号有 SSI位控制
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //波特率预分频值为 16
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //数据传输从 MSB 位开始
SPI_InitStructure.SPI_CRCPolynomial = 7; //定义了用于 CRC值计算的多项式 7
SPI_Init(SPI2, &SPI_InitStructure);
//SPI_BiDirectionalLineConfig(SPI2, SPI_Direction_Rx);
/* Enable SPI1 */
SPI_Cmd(SPI2, ENABLE);
主程序10ms锁存一次74hc65,并打开接收中断:
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
GPIO_SetBits(GPIOB, GPIO_Pin_12);
SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);
接收中断里读数据,并关闭接收中断:
if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_RXNE) != RESET)
{
data = SPI_I2S_ReceiveData(SPI2);
if (data != 0xff)
{
data = 0xff;
}
SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, DISABLE);
} |