stm32作为从设备,要通过spi2和主设备通讯。
现在是用中断接收数据的,接收完一帧信息后置个标志位。
主程序里有个函数,函数中判断spi接收标志位,如果是1的话,通过串口将接收到的数据发送出来。
以上这部分,磕磕碰碰的算是调通了(画了很久)。主函数里不能只做spi通讯部分吧,然后加上其他程序部分。结果spi通讯这里就不工作了...
while(1) { SPI2_SalveProcess(); printf("\r\n\r\nhahaha); }
主函数里的第一个语句,是spi2通讯部分的函数,如果只保留这个,通讯没问题。然后在其后随便加了个printf语句,结果spi通讯就不工作了...
具体现象是,不再通过串口将接收到的数据发送过来,并且作为spi从设备,接收数据的同时,应该也发送数据,结果,没有数据发出去。
以下为SPI2_SalveProcess()函数:
void SPI2_SalveProcess(){ u8 i; if(COMP_OK()) { LVDS_EN(0); } else { if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7) == 0) { LVDS_EN(1); SPI_I2S_SendData(SPI2, spi2RecvBuff[0]); } } if(spi2RecvOk == 1) { printf(&quot;\r\nSPI2 recive sucess&quot;); for(i = 0; i < 5; i++) printf(&quot;\r\nspi2RecvBuff[%d]: %04x&quot;, i, spi2RecvBuff); spi2RecvOk = 0; spi2RecvNum = 0; }}
以下为spi2中断处理函数:
void SPI2_IRQHandler(void){ u16 spi2Temp; spi2Temp = SPI_I2S_ReceiveData(SPI2); if(spi2RecvOk == 0) { if(spi2RecvNum == 0) { if((spi2Temp & 0xff00) == 0x3a00) { SPI_I2S_SendData(SPI2, spi2SendBuff[1]); spi2RecvBuff[0] = spi2Temp; spi2RecvNum = 1; spi2SendNum = 2; } else SPI_I2S_SendData(SPI2, spi2SendBuff[0]); } else { SPI_I2S_SendData(SPI2, spi2SendBuff[spi2SendNum]); spi2RecvBuff[spi2RecvNum] = spi2Temp; spi2RecvNum++; if(spi2RecvNum > 4) spi2RecvOk = 1; spi2SendNum += 1; spi2SendNum %= 5; } }}
串口只开了接收中断,并且,现在的操作,并没有通过串口向单片机发送数据。
spi2也只是开了接收中断。
以下为中断优先级配置:
void NVIC_Configuration(void){ NVIC_InitTypeDef NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);} |