我使用STM32F030C8T6作SPI从机,SPI初始化如下: void OLED_SPI_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; SPI_InitTypeDef SPI_InitStructure; /* Enable the SPI periph */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); /* Enable SCK, MOSI, MISO and NSS GPIO clocks */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_0); GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_0); GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_0); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3; /* SPI SCK pin configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_Init(GPIOA, &GPIO_InitStructure); /* SPI MISO pin configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_Init(GPIOA, &GPIO_InitStructure); /* SPI MOSI pin configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_Init(GPIOA, &GPIO_InitStructure); /* SPI configuration -------------------------------------------------------*/ SPI_I2S_DeInit(SPI1); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b; //16位 SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //最高位开始移位 SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); /* Configure the SPI interrupt priority */ NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); RxIndex = 0; TxIndex = 0; } void SPI_Config(void) { SPI_InitTypeDef SPI_InitStructure; /* Initializes the SPI communication */ SPI_I2S_DeInit(SPI1); SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; SPI_Init(SPI1, &SPI_InitStructure); /* Initialize the FIFO threshold */ SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_HF); //16位 /* Enable the Rx buffer not empty interrupt */ SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, ENABLE); /* Enable the SPI peripheral */ SPI_Cmd(SPI1, ENABLE); } 可是在调试界面查看寄存器时却发现: (1)本来要配置数据长度为16位的,也就是这句 SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;寄存器显示的却是8位; (2)本来要配置从最高位开始移位的,也就是这句SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;寄存器显示的却是从最低位(LSB)开始移位; 配置函数都是从ST官方例程中移植过来的;本人刚开始接触STM32,用的不熟,被这个问题困扰很久,希望能得到各位大神的指点! |
感谢回复,我现在不是怀疑库函数是否有问题,而是网上提供的配置都是使用库函数配置的,所以这样配置应该没问题。不知是否哪里没有配置对才会导致这种情况出现,网上说时钟没打开会出现这种情况,但是我的时钟已经打开了啊。