利用stm32F103的正交编码器接收手动脉冲发生器的脉冲,正交编码器计数没有问题,但是我需要知道手脉转动的方向。利用TIM1->CR1 & 0x10或者判断计数值是否是0或者最大值都会有误判存在,大家有遇到这种问题的吗,是怎么解决的?贴出我的代码。还有一个问题是,当计数值计到零,如果这时候不转手脉,计数值就一直为零,那么总是进中断,所以我手动修改计数值,是否我某个地方设置的不对?
void TIM1_UP_IRQHandler(void)
{
// uint16_t temp;
temp = TIM_GetCounter(TIM1) & 0xfff;
if(temp == 0)
TIM_SetCounter(TIM1,1);
if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
if(auto_ctr == 1)
{
handcctr = handctrScan();
// if(temp ==0)//ÊÖÂöÕýת
if((TIM1->CR1 & 0x10) == 0)
{
//if(predir == 1)
//{
GPIO_SetBits( GPIOC, GPIO_Pin_11 );//µç»úÕýת
switch(handcctr)
{
case 0x01:
TIM_SetAutoreload(TIM2,12); //N = 4800·¢ËÍÂö³å¸öÊý ÊÖÂöһȦ¶ÔÓ¦µç»ú0.16Ȧ
TIM_GenerateEvent(TIM2,TIM_EventSource_Update);
TIM_Cmd(TIM2, ENABLE);
break;
case 0x02:
TIM_SetAutoreload(TIM2,120); //N = 4800·¢ËÍÂö³å¸öÊý ÊÖÂöһȦ¶ÔÓ¦µç»ú1.6Ȧ
TIM_GenerateEvent(TIM2,TIM_EventSource_Update);
TIM_Cmd(TIM2, ENABLE);
break;
case 0x03:
TIM_SetAutoreload(TIM2,1200); //N = 4800·¢ËÍÂö³å¸öÊý ÊÖÂöһȦ¶ÔÓ¦µç»ú16Ȧ
TIM_GenerateEvent(TIM2,TIM_EventSource_Update);
TIM_Cmd(TIM2, ENABLE);
break;
default:
break;
}
//}
//else
//{
// predir = 1;
//}
}
//else if(temp == 400)//ÊÖÂö·´×ª
else if((TIM1->CR1 & 0x10) == 0x10)
{
//if(predir == 0)
{
GPIO_ResetBits( GPIOC, GPIO_Pin_11 );//µç»ú·´×ª
switch(handcctr)
{
case 0x01:
TIM_SetAutoreload(TIM2,12); //N = 4800·¢ËÍÂö³å¸öÊý ÊÖÂöһȦ¶ÔÓ¦µç»ú0.16Ȧ
TIM_GenerateEvent(TIM2,TIM_EventSource_Update);
TIM_Cmd(TIM2, ENABLE);
break;
case 0x02:
TIM_SetAutoreload(TIM2,120); //N = 4800·¢ËÍÂö³å¸öÊý ÊÖÂöһȦ¶ÔÓ¦µç»ú1.6Ȧ
TIM_GenerateEvent(TIM2,TIM_EventSource_Update);
TIM_Cmd(TIM2, ENABLE);
break;
case 0x03:
TIM_SetAutoreload(TIM2,1200); //N = 4800·¢ËÍÂö³å¸öÊý ÊÖÂöһȦ¶ÔÓ¦µç»ú16Ȧ
TIM_GenerateEvent(TIM2,TIM_EventSource_Update);
TIM_Cmd(TIM2, ENABLE);
break;
default:
break;
}
}
//else
//{
// predir = 0;
//}
}
}
}
}
|