采用的是例程(FullDuplex_HardNSS)中的初始化:
void SPI_FullDuplex_Init(void) { GPIO_InitTypeDef GPIO_InitStructure={0}; SPI_InitTypeDef SPI_InitStructure={0}; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init( GPIOA, &GPIO_InitStructure ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init( GPIOA, &GPIO_InitStructure ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init( GPIOA, &GPIO_InitStructure ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init( GPIOA, &GPIO_InitStructure ); SPI_SSOutputCmd( SPI1, ENABLE ); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Hard; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init( SPI1, &SPI_InitStructure ); SPI_Cmd( SPI1, ENABLE ); }
main函数中的代码如下:
int main(void) { //优先级分组,延时以及调试串口初始化(使用的是USART3->D8(TX)、D9(RX) ,需要用别的串口的同学可以修改USART_Printf_Init()中的参数 LQ_V30xInit(); PIN_InitConfig(PC4, GPI, 0, GPIO_Mode_IN_FLOATING); SPI_FullDuplex_Init(); u8 i=0; u8 TxData[18] = { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58 }; //检测是否达到发送数据的要求 while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_4) == Bit_RESET) { LED_Ctrl(LED1, OFF); } LED_Ctrl(LED1, ON); while(1) { for(i = 0; i < 18; i++) { while( SPI_I2S_GetFlagStatus( SPI1, SPI_I2S_FLAG_TXE ) == RESET ); SPI_I2S_SendData( SPI1, TxData[i] ); } LED_Ctrl(LED0, RVS); Delay_Ms(100); } }
得到的波形如下图:只有前几个数据是正常的,当NSS引脚出现高电平时数据解析出现问题,我的从设备收到的数据为乱码。
并且看mian函数中我发送的是18个数据,这里明显不到18个数据。发送完成后NSS引脚也没有自动拉高,顺便说一下,NSS引脚接了10K的上拉电阻。