Sbit BMA = P4 ^ 2; // code switch a pin 1 // must be connected with a pulling resistance of 680 euro sbit BMB = P4 ^ 3; // encoding Switch B Pin 3 // must be connected with a pulling resistance of 680 euro // encoding switch pin 2 grounded sbit BMC = p1 ^ 6; // encoding switch C press the pin unsigned int counter = 0; // encoder Pulse Count /********************************** * ********************** scan the encoder subfunction by Chen Zheng (clock direction) 1110000111 // start second angle 10 ...... Reverse (reverse clock direction) 1101001011 // start second angle 01 ...... If the program starts from 00, if it is still 00, it will be returned if the delay is so long: 0ffff * yans (1) or 00 (if the conversion speed is very fast, it can be reduced; the opposite is true) if 01 is received, it is added to 1. If 10 is received, it is reversed and subtracted from 1. If 11 is received, the error is returned ****************** **************************************** /void scan_encoder () {unsigned int I; while (1) {If (BMA = 0 & BMB = 0) // After receiving 0 0, start to judge {for (I = 0; I <0xfff0; I ++) {If (BMA = 0 & BMB = 0) // The status has not changed. Wait {yans (1 );} else if (BMA = 0 & BMB = 1) // convert {counter ++; return;} else if (BMA = 1 & BMB = 0) // reverse {counter --; return;} If (BMA = 1 & BMB = 1) // error {return ;}} else if (0 = BMC) {counter = 0; return;} else {return ;}} void xszs () {unsigned int code DB [] = {0, 0,}; fsfont55 (); fsszxy (4, DB); printf ("% 5u \ n", counter); txjs ();} void testmain () {yans (3000); While (1) {// tisy (); xszs (); scan_encoder ();}}