void TIM4_IRQHandler(void) //3ms定时器定时中断(自己随意设定2ms~10ms应该都可以)
{
static int a = 0,b = 0,c = 0,d = 0;
if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
{
c = SCENE_KEY1; //读取编码器引脚状态
d = SCENE_KEY2; //读取编码器引脚状态
if(a == 0 && b == 0)
{
if(c == 1 && d == 0)
{
Ec_status = 1;//正转
}
if(c == 0 && d == 1)
{
Ec_status = 2;//反转
}
}
a = c;
b = d; //a,b保存前一次引脚采集到的状态
}
}
}
//实际在main函数中使用时
int main()
{
int Ec_count = 0; //旋转编码器当前的数值(取值范围0~10)
while(1)
{
if(Ec_status == 1)
{
Ec_count++;
if(Ec_count > 10)
{
Ec_count = 10;
}
}
else if(Ec_status == 2)
{
Ec_count--;
if(Ec_count < 0)
{
Ec_count = 0;
}
}
delay_ms(5);
}
}
|