Encoding switch test OK Chen

Source: Internet
Author: User

[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 ();
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.