Encoding switch test OK Chen

Source: Internet
Author: User
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 ();}}

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.