SPI设置软件片选,结果通信是引脚一拉低就又被拉高了。波形如下
代码如下:
#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; // 数据大小为8位
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; // 时钟极性为高电平
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; // 第2个时钟边沿采样数据
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;//SPI_NSS_Soft; // 软件控制NSS//
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; // 波特率预分频器,这里设为256
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; // 数据传输的第一个位为最高位
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);
}