#include <reg52.h>
sbit bA_Key01_IoStatus = P1^0;
sbit bB_Key02_IoStatus = P1^1;
/*************************************************************
1us延时子函数
**************************************************************/
void Delay1us(uint32 dly)
{
uint32 i;
for(; dly>0; dly--)
for(i=20; --i;) ;
}
/*
扫描编码器子函数
在编码器引脚A为低电平期间:
编码器引脚B从0到1为正转,编码器引脚B从1到0为反转。
*/
void PWM_key(void)
{
uint8 Curr_encoder_b; //定义一个变量来储存当前B信号
uint8 Last_encoder_b; //定义一个变量来储存上次B脚信号
if( bA_Key01_IoStatus && bB_Key02_IoStatus) //编码器无转动退出
{
return;
}
//-----------------------------------
if(!bB_Key02_IoStatus)
{
Last_encoder_b = bA_Key01_IoStatus; //记录B信号
Delay1us(1000); //延时约 1ms
Curr_encoder_b = bA_Key01_IoStatus; //记录等待期间的B信号(指当前B信号)
if( (Last_encoder_b == 0)&&(Curr_encoder_b== 1) ) //B从0到1为正转
{
;
}
else if( (Last_encoder_b == 1)&&(Curr_encoder_b == 0) ) //B从1到0为反转
{
;
}
}
}
原文:https://www.cnblogs.com/dongxiaoming/p/14751356.html