采用GPIO外部中断,大概70us就会中断一次,在主循环中有对中断函数中的变量进行判断,所以加了临界区保护,即加开关外部中断操作,
开始通过外部中断寄存器开关实现,即如下宏中的#esle,但发现会有一定几率丢失中断,而换成通过NVIC实现,即如下代码中的#if 1则正常。
可是再仔细查看各种资料,通过外部中断寄存器的方式应该也不会丢才对, 想不明白。
void EXTI2_IRQ_enable(void)
{
#if 1
NVIC_InitTypeDef nvic_init;
nvic_init.NVIC_IRQChannel = EXTI2_IRQn;
nvic_init.NVIC_IRQChannelPreemptionPriority = 0;
nvic_init.NVIC_IRQChannelSubPriority = 0;
nvic_init.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic_init);
#else//这种方法会丢中断;
EXTI->IMR |= EXTI_Line2;
#endif
}
void EXTI2_IRQ_disable(void)
{
#if 1
NVIC_InitTypeDef nvic_init;
nvic_init.NVIC_IRQChannel = EXTI2_IRQn;
nvic_init.NVIC_IRQChannelPreemptionPriority = 0;
nvic_init.NVIC_IRQChannelSubPriority = 0;
nvic_init.NVIC_IRQChannelCmd = DISABLE;
NVIC_Init(&nvic_init);
#else//这种方法会丢中断;
EXTI->IMR &= ~EXTI_Line2;
#endif
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
---------------------------------
...无关的怕影响你们时间,如下是中断函数
u16 spi_read_data(void)
{
/* Wait until the transmit buffer is empty */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
/* Send the byte */
SPI_I2S_SendData(SPI1, 0xFFFF);
/* Wait until a data is received */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
/* Get the received data */
return SPI_I2S_ReceiveData(SPI1);
}
void EXTI2_IRQHandler(void)
{
u8 i;
u8 ucTemp[8];
EXTI->R = EXTI_Line2;
for(i = 0; i < 4; i++)
{
*((u16 *)(&ucTemp[i*2])) = spi_read_data();
}
...(将ucTemp值保存到环形缓存中)
}
主循环中,在读环形缓存前后做外部中断关、开
一周热门 更多>