采用的是例程(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函數(shù)中的代碼如下:
int?main(void)?{ ????//優(yōu)先級(jí)分組,延時(shí)以及調(diào)試串口初始化(使用的是USART3->D8(TX)、D9(RX)?,需要用別的串口的同學(xué)可以修改USART_Printf_Init()中的參數(shù) ????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?}; ????//檢測(cè)是否達(dá)到發(fā)送數(shù)據(jù)的要求 ????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); ????} }
得到的波形如下圖:只有前幾個(gè)數(shù)據(jù)是正常的,當(dāng)NSS引腳出現(xiàn)高電平時(shí)數(shù)據(jù)解析出現(xiàn)問題,我的從設(shè)備收到的數(shù)據(jù)為亂碼。
并且看mian函數(shù)中我發(fā)送的是18個(gè)數(shù)據(jù),這里明顯不到18個(gè)數(shù)據(jù)。發(fā)送完成后NSS引腳也沒有自動(dòng)拉高,順便說一下,NSS引腳接了10K的上拉電阻。