设备是原子哥的F103RC。用HAL库写的程序。由外部触发信号触发ADC采样,调用ADCFunction()。ADCFunction()启动后会启动定时器TIM2,定时一定时间后中断,设置StateBit 为0,想要中断下面粗体显示while循环。
但debug时候发现StateBit 已经为0了,但程序任然死在加粗的那个while循环中,while循环的判据不起作用,Times和ADCA的值不停增加至溢出,然后重新开始。求助这是怎么回事?
//中断服务函数
void EXTI0_IRQHandler(void)
{
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0); //调用中断处理公用函数,由此函数调用中断回调函数
}
//中断服务程序中需要做的事情
//在HAL库中所有的外部中断服务函数都会调用此函数
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) //GPIO_Pin:中断引脚号
{
switch(GPIO_Pin)
{
case GPIO_PIN_0:
ADCFunction(279);
break;
}
}
//定时器2中断服务函数
void TIM2_IRQHandler(void)
{
HAL_TIM_IRQHandler(&TIM2InitStructure);
}
//定时器2中断服务回调函数,由HAL_TIM_IRQHandler每次发生更新中断时调用
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim==(&TIM2InitStructure))
{
StateBit = false;
}
}
void ADCFunction(u16 TimerValue)
{
LoopValue ++;
StateBit = true;
__HAL_TIM_ENABLE_IT(&TIM2InitStructure,TIM_IT_UPDATE);
__HAL_TIM_ENABLE(&TIM2InitStructure);
do
{
ADCA += (&PB1_ADCStructure)->Instance->DR;
Times ++;
}
while(StateBit);
__HAL_TIM_DISABLE(&TIM2InitStructure); //关闭定时器2,到需要时开启
__HAL_TIM_DISABLE_IT(&TIM2InitStructure,TIM_IT_UPDATE); //关闭更新中断,到需要时再开启
TIM2->CNT = TimerValue; //重装载计数寄存器
SumA += (double) ADCA/Times;
ADCA = 0;
Times = 0;
StateBit = true;
__HAL_TIM_ENABLE_IT(&TIM2InitStructure,TIM_IT_UPDATE);
__HAL_TIM_ENABLE(&TIM2InitStructure);
do
{
ADCB += (&PB1_ADCStructure)->Instance->DR;
Times ++;
}
while(StateBit);
__HAL_TIM_DISABLE(&TIM2InitStructure); //关闭定时器2,到需要时开启
__HAL_TIM_DISABLE_IT(&TIM2InitStructure,TIM_IT_UPDATE); //关闭更新中断,到需要时再开启
TIM2->CNT = TimerValue; //重装载计数寄存器
SumB += (double) ADCB/Times;
ADCB = 0;
Times = 0;
StateBit = true;
__HAL_TIM_ENABLE_IT(&TIM2InitStructure,TIM_IT_UPDATE);
__HAL_TIM_ENABLE(&TIM2InitStructure);
do
{
ADCC += (&PB1_ADCStructure)->Instance->DR;
Times ++;
}
while(StateBit);
__HAL_TIM_DISABLE(&TIM2InitStructure); //关闭定时器2,到需要时开启
__HAL_TIM_DISABLE_IT(&TIM2InitStructure,TIM_IT_UPDATE); //关闭更新中断,到需要时再开启
TIM2->CNT = TimerValue; //重装载计数寄存器
SumC += (double) ADCC/Times;
ADCC = 0;
Times = 0;
if(LoopValue == 64)
{
u32 tempvalue = 0;
LoopValue = 0;
SumA = SumA/64*(3.3/4096);
SumB = SumB/64*(3.3/4096);
SumC = SumC/64*(3.3/4096);
tempvalue = SumA;
OLED_ShowString(5,30,"A:",12);
OLED_ShowNum(17,30,tempvalue,1,12);
OLED_ShowString(23,30,".",12);
SumA -= tempvalue;
SumA *= 100000;
tempvalue = SumA;
OLED_ShowNum(29,30,tempvalue,3,12);
tempvalue = SumB;
OLED_ShowString(69,30,"B:",12);
OLED_ShowNum(81,30,tempvalue,1,12);
OLED_ShowString(87,30,".",12);
SumB -= tempvalue;
SumB *= 100000;
tempvalue = SumB;
OLED_ShowNum(93,30,tempvalue,3,12);
tempvalue = SumC;
OLED_ShowString(5,42,"C:",12);
OLED_ShowNum(17,42,tempvalue,1,12);
OLED_ShowString(23,42,".",12);
SumC -= tempvalue;
SumC *= 100000;
tempvalue = SumC;
OLED_ShowNum(29,42,tempvalue,3,12);
OLEDScreen_Refreash();
SumA = 0;
SumB = 0;
SumC = 0;
}
}
一周热门 更多>