[Plain]
BMA = P4 ^ 2; // encoding switch A pin 1 // must be connected with A pulling resistance of 680 Euro
Sbit BMB = P4 ^ 3; // code switch B Pin 3 // must be connected with a pulling resistance of 680 Euro
// Encoding switch pin 2 grounded
Sbit BMC = P1 ^ 6; // code switch c press the pin
Unsigned int counter = 0; // encoder Pulse Count
/*************************************** *******************
Scan encoder sub-function by Chen
Forward (clock direction)
11
10
00
01
11 // start second angle
10
......
Reverse (reverse clock direction)
11
01
00
10
11 // start second angle
01
......
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 also true)
If 01 is received, 1 is added and return
If 10 is received, 1 is reversed and return
If 11 is received, an error is returned.
**************************************** ******************/
Void scan_encoder ()
{
Unsigned int I;
While (1)
{
If (BMA = 0 & BMB = 0) // starts judgment after 0 is received
{
For (I = 0; I <0xfff0; I ++)
{
If (BMA = 0 & BMB = 0) // The status is not changed. Wait
{
Yans (1 );
}
Else if (BMA = 0 & BMB = 1) // forward
{
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 ();
}
}