旋转编码器.c

来自「用51写的旋转式编码器程序」· C语言 代码 · 共 35 行

C
35
字号
#include "reg51.h"
#define uint unsigned int

sbit CodingsWitch_A=P1_1;
sbit CodingsWitch_B=P1_2;

uint CodingsWitchPolling()// 
{ 
   static Uchar Aold,Bold;   //定义了两个变量用来储蓄上一次调用此方法是编码开关两引脚的电平 
   static Uchar st;         //定义了一个变量用来储蓄以前是否出现了两个引脚都为高电平的状态 
   uint tmp = 0; 
   if(CodingsWitch_A&&CodingsWitch_B) 
   st = 1;      // 
   if(st)               //如果st为1执行下面的步骤 
   { 
      if(CodingsWitch_A==0&&CodingsWitch_B==0)      //如果当前编码开关的两个引脚都为底电平执行下面的步骤 
      { 
         if(Bold)      //为高说明编码开关在向加大的方向转 
         { 
            st = 0;       
            tmp++;      // 
         } 
         if(Aold)      //为高说明编码开关在向减小的方向转 
         { 
            st = 0; 
            tmp--;      //设返回值 
         } 
      } 
   } 
   Aold = CodingsWitch_A;            // 
   Bold = CodingsWitch_B;            //储
   return tmp;            // 
} 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?