STM8S003外部中断识别机械式编码器正反转
2020-01-13 来源:eefocus
最近在项目上,用到鼠标上用的编码器,按照厂家给的编程方法,发现容易产生误判,故而自己用外部中断的方式来做,发现这样误判的情况会得到很大的改善,现发上来与大家分享。
#include 'stm8s103f3p.h'
#include 'stm8s_bitsdefine.h'
#include 'typedef.h'
#define CodeA PB_IDR_0
#define CodeB PB_IDR_1
uint8_t CodeB_last,CodeB_current,whe_ch,ADJ;
void UART1_Init(void)
{
UART1_CR2=0x00;
UART1_CR1=0x00;
UART1_CR3=0x00;
UART1_BRR2=0x00;
UART1_BRR1=0x0d;
UART1_CR2=0x2C;
}
void UART1_SendChar(uint8_t ch)
{
while((UART1_SR & 0x80) == 0x00);
UART1_DR = ch;
}
void IO_Init(void)
{
PB_DDR=0x04;
PB_CR1=0x07;
PB_CR2=0x01;
}
void EXTI1_INIT(void)
{
EXTI_CR1=0x08;
}
main()
{
IO_Init();
EXTI1_INIT();
UART1_Init();
_asm('rim');
while(1) ;
}
@far @interrupt void EXTI1_IRQHandler(void)
{
_asm('sim');
whe_ch++;
if(whe_ch==1)
{
CodeB_last=CodeB;
IO_Init();
EXTI_CR1=0x04;
}
if(whe_ch==2)
{
whe_ch=0;
CodeB_current=CodeB;
IO_Init();
EXTI_CR1=0x08;
}
if((CodeB_last==1)&&(CodeB_current==0))
{
ADJ++;
UART1_SendChar(ADJ);
}
if((CodeB_last==0)&&(CodeB_current==1))
{
ADJ--;
UART1_SendChar(ADJ);
}
_asm('rim');
return;
}