SPI設(shè)置軟件片選,結(jié)果通信是引腳一拉低就又被拉高了。波形如下
代碼如下:
#include "debug.h"
#define SPI1_CLK? ? ? ? ? ? ? ? RCC_APB2Periph_SPI1
#define SPI1_GPIO? ? ? ? ? ? ? ?GPIOA
#define SPI1_GPIO_CLK? ? ? ? ? ?RCC_APB2Periph_GPIOA
#define SPI1_PIN_SCK? ? ? ? ? ? GPIO_Pin_5
#define SPI1_PIN_MISO? ? ? ? ? ?GPIO_Pin_6
#define SPI1_PIN_MOSI? ? ? ? ? ?GPIO_Pin_7
#define SPI1_PIN_SS? ? ? ? ? ? ?GPIO_Pin_4
/* Global typedef */
/* Global define */
/* Global Variable */
void SPI1_FullDuplex_Init(void);
void TEMPE1_Init(void);
/*********************************************************************
?* @fn? ? ? main
?*
?* @brief? ?Main program.
?*
?* @return? none
?*/
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
SystemCoreClockUpdate();
Delay_Init();
// USART_Printf_Init(115200);
// printf("SystemClk:%d\r\n",SystemCoreClock);
// printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
// printf("This is printf example\r\n");
SPI1_FullDuplex_Init();
while(1)
? ? {
? ? Delay_Ms(500);
? ? TEMPE1_Init();
}
}
void TEMPE1_Init(void)
{
? ? GPIO_ResetBits(SPI1_GPIO, SPI1_PIN_SS);
? ? GPIO_ResetBits(SPI1_GPIO, SPI1_PIN_SS);
? ? while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
? ? SPI_I2S_SendData(SPI1, 0x80);
? ? while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
? ? SPI_I2S_SendData(SPI1, 0xd1);
? ? GPIO_SetBits(SPI1_GPIO, SPI1_PIN_SS);
}
void SPI1_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_Out_PP;
? ? GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
? ? GPIO_Init( GPIOA, &GPIO_InitStructure );
? ? GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_7;
? ? 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 );
? ? SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;? ?// 雙線全雙工
? ? SPI_InitStructure.SPI_Mode = SPI_Mode_Master;? ?// 主機模式
? ? SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;? ?// 數(shù)據(jù)大小為8位
? ? SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;? ?// 時鐘極性為高電平
? ? SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;? ?// 第2個時鐘邊沿采樣數(shù)據(jù)
? ? SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;//SPI_NSS_Soft;? ?// 軟件控制NSS//
? ? SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;? ?// 波特率預(yù)分頻器,這里設(shè)為256
? ? SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;? ?// 數(shù)據(jù)傳輸?shù)牡谝粋€位為最高位
? ? SPI_InitStructure.SPI_CRCPolynomial = 7;? ?// CRC校驗
? ? SPI_Init( SPI1, &SPI_InitStructure );
? ? SPI_SSOutputCmd( SPI1, DISABLE );
? ? SPI_Cmd( SPI1, ENABLE );
? ? GPIO_SetBits(SPI1_GPIO, SPI1_PIN_SS);
? ? Delay_Ms(10);
}