本帖最后由 any012 于 2016-5-12 15:54 编辑
想用stm32作为spi的从设备,用的是spi2,spi2接收中断后,将接收到的数通过串口发送出来。
结果调不通,请大家帮看下是哪里设置错了?用示波器测量SCK和MOSI管脚,是有波形的。
SPI设置部分:
- void SPI2_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- SPI_InitTypeDef SPI_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_14; //PB12 - SPI2_CS, PB14 - SPI2_MISO
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15; //PB13 - SPI2_SCK, PB15 - SPI2_MOSI
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
- SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
- SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
- SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
- SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
- SPI_InitStructure.SPI_NSS =SPI_NSS_Soft;
- SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
- SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
- SPI_InitStructure.SPI_CRCPolynomial = 7;
- SPI_Init(SPI2, &SPI_InitStructure);
-
- SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);
- SPI_Cmd(SPI2, ENABLE);
- }
- void SPI2_IRQHandler(void)
- {
- u8 spiTemp;
- spiTemp = SPI_I2S_ReceiveData(SPI2);
- printf("spirecive: %d", spiTemp);
- }
复制代码
主函数部分:
- int main(void)
- {
- Init();
- TIM_Cmd(TIM3, ENABLE);
-
- while(1)
- {
- if(flag_1ms)
- {
- flag_1ms = 0;
- printf("\r\n1s¶¨Ê±µ½¡£");
- }
- }
- }
- void Init(void)
- {
- GPIO_Config();
- SPI2_Init();
- USART1_Config();
- TIM3_Config(9999, 7199);
- NVIC_Configuration();
- }
复制代码
中断配置部分:
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
-
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
复制代码
|
SPI时钟开启错误,根本没开启时钟!
你打字打错了 , 请改一下 :RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
评分
查看全部评分
评分
查看全部评分
评分
查看全部评分
三个管脚均有10K上拉电阻。
评分
查看全部评分
需要工作在从模式下。
主设备发送了时钟信号,因为可以在本电路的SCK和MOSI管脚上测到波形。
评分
查看全部评分
SPI中断里最好不要进行printf数据处理。
评分
查看全部评分
评分
查看全部评分
评分
查看全部评分
评分
查看全部评分
评分
查看全部评分