Home > other >  [original] more simple rotary encoder programs (not interrupt)
[original] more simple rotary encoder programs (not interrupt)

Time:01-10

https://www.stmcu.org.cn/module/forum/thread-628556-1-2.html
A few years ago had sent a similar article here, see & lt; https://www.stmcu.org.cn/module/forum/thread-614394-1-1.html> ,
But today still feel is not simple, send a more simple, remove the setting, printing, light, delay after the statement, only two lines of instructions (see//* * *),
The code is as follows:

//test rotary encoder STM32F103 20210106. Ino
Long keyN;
Void setup () {
PinMode (PA13, INPUT_PULLUP);
PinMode (PA14, INPUT_PULLUP);
PinMode (PC13, the OUTPUT);
Serial. The begin (115200);
}
Void loop () {
If (digitalRead (PA13) & amp; DigitalRead (PA14)) {//* * *
DigitalWrite (PC13,! DigitalRead (PC13));//light
Serial. Println (keyN);//print
Delay (2);//delay
KeyN=keyN +! DigitalRead (PA13) -! DigitalRead (PA14).//* * *
}
}
Note:
1, the delay time can be custom size according to the specific demand,
2, the GPIO according to need to choose, here lazy with SWD pin, best to switch to other foot,
3, as if can't lean to one line of instruction can be finished, because there are timing relationships,
4, please feel free to comment,

CodePudding user response:

Have KanTie q: don't understand, how to identify the direction and pulse number?
Explanation:
KeyN=keyN +! DigitalRead (PA13) -! DigitalRead (PA14).//* * *
KeyN is pulse numerical
! DigitalRead (PA13) and! DigitalRead (PA14) successively is direction, they also can have only one in change,
In addition: there are two kinds of rotary encoder
1. Contact is normally open, the above procedures are available,
2. In the market to buy simple rotary encoder module, is likely to be normally closed type,
Please pull on the two 103 module resistance, and the above procedure must be add "//* * *", instead of
If (! DigitalRead (PA13) & amp; ! DigitalRead (PA14)) {//* * *
KeyN=keyN + digitalRead (PA13) - digitalRead (PA14).//* * *
Can,
  • Related