本帖最后由 地瓜ing 于 2023-12-13 23:25 编辑
在调试SPI的过程中遇到使用硬件NSS时,对应的IO没有输出的问题,以下是相应代码
PA15作为SPI1的硬件NSS引脚一直输出低电平,AFIO中SPI1的NSS空闲状态已经设置为高电平了,还是一直输出低电平
以下是初始化程序
void Spi1GpioConfig()
{
GPIO_InitType GPIO_InitStructure;
/* Initialize GPIO_InitStructure */
GPIO_Structure_Initialize(&GPIO_InitStructure);
RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOA|RCC_AHB_PERIPH_GPIOB);
RCC_APB2_Peripheral_Clock_Enable(RCC_APB2_PERIPH_AFIO);
GPIO_InitStructure.Pin = GPIO_PIN_3 | GPIO_PIN_4 ;//PB3->SCK,PB4->MISO
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.GPIO_Slew_Rate = GPIO_SLEW_RATE_FAST;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF2_SPI1;
GPIO_Peripheral_Initialize(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_5 ; //PB5->MOSI
GPIO_InitStructure.GPIO_Alternate = GPIO_AF1_SPI1;
GPIO_Peripheral_Initialize(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_15 ; //PA15->NSS
GPIO_InitStructure.GPIO_Alternate = GPIO_AF1_SPI1;
GPIO_Peripheral_Initialize(GPIOA, &GPIO_InitStructure);
AFIO_SPI_NSS_Mode_Set(AFIO_SPI1_NSS, AFIO_SPI_NSS_High_LEVEL);//NSS空闲时的电平
}
void Spi1_Init ( void )
{
SPI_InitType SPI_InitStructure;
SPI_I2S_Reset(SPI1);
Spi1GpioConfig();
RCC_APB2_Peripheral_Clock_Enable(RCC_APB2_PERIPH_SPI1);
/* SPI_MASTER configuration ------------------------------------------------------*/
SPI_Initializes_Structure(&SPI_InitStructure);
SPI_InitStructure.DataDirection = SPI_DIR_DOUBLELINE_FULLDUPLEX;
SPI_InitStructure.SpiMode = SPI_MODE_MASTER;
SPI_InitStructure.DataLen = SPI_DATA_SIZE_8BITS;
SPI_InitStructure.CLKPOL = SPI_CLKPOL_HIGH;
SPI_InitStructure.CLKPHA = SPI_CLKPHA_SECOND_EDGE;
SPI_InitStructure.NSS = SPI_NSS_HARD;
/* It is recommended that the SPI master mode of the C version chips should not exceed 18MHz */
SPI_InitStructure.BaudRatePres = SPI_BR_PRESCALER_32;
SPI_InitStructure.FirstBit = SPI_FB_MSB;
SPI_InitStructure.CRCPoly = 7;
SPI_Initializes(SPI1, &SPI_InitStructure);
SPI_SS_Output_Enable(SPI1);
SPI_ON(SPI1);
}
以下是主循环中的测试部分
while(1)
{
if(Task_TimeFlag_ms(1,100))
{
//100ms运行一次
Run();
// SPI_Set_Nss_Level(SPI1, SPI_NSS_LOW);
SPI_I2S_Data_Transmit(SPI1, 0x5A);
Delay_Us(100);
SPI_I2S_Data_Transmit(SPI1, 0xA5);
// SPI_Set_Nss_Level(SPI1, SPI_NSS_HIGH);
LED_TOGGLE;
}
}
|