stmF1开发,旋转编码器旋钮计数,用数码管显示计数;
产生的问题是旋钮标志不精确,有时正旋会产生反旋中断;
请问如何解决?
我用的是旋钮时产生中断,在中断里设置正旋标志为1、反旋标志为2;在主函数中进行判断;
主函数中
while(1){ showNum(c_value); // 数码管显示函数
if(turn_flag) // 旋转标志;正旋为1、反旋为2;计数完置零
{
delay_ms(100);
switch(turn_flag)
{
case 1: c_value++;
break;
case 2: c_value--;
break;
default:break;
}
turn_flag=0;
t_flag=0;
EXTI->IMR=0xffffffff;
}
}
中断部分:
初始化:
#include "interrupt.h"
#include <stdio.h>
#include "delay.h"
u8 turn_flag;
void int_init(void)
{
EXTI_InitTypeDef EXTI_InitStruct;
GPIO_InitTypeDef GPIO_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_3|GPIO_Pin_1|GPIO_Pin_2;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU;
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource3);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource1);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource2);
EXTI_InitStruct.EXTI_Line=EXTI_Line1|EXTI_Line2;
EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Rising_Falling;
EXTI_InitStruct.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStruct);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStruct.NVIC_IRQChannel=EXTI1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=3;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel=EXTI2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=3;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStruct);
}
void EXTI1_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line1)!=RESET)
{
turn_flag=1;
EXTI->IMR=0xfffffff0;
EXTI_ClearITPendingBit(EXTI_Line1);
}
}
void EXTI2_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line2)!=RESET)
{
turn_flag=2;
EXTI->IMR=0xfffffff0;
EXTI_ClearITPendingBit(EXTI_Line2);
}
}
一周热门 更多>