用于读取旋转编码器模块
- //非阻塞编码器模块读取,通过编码器按键使能编码器事件,当编码器使能时分别增减计数值。
- //未用到中断,通过轮询读取编码器,周期5ms,编码器旋钮模块用,更高线数的编码器需要中断读。
- #include "key.h" //按键检测宏
- #include "edge.h" //边沿检测宏
- #include "encoder.h" //编码器检测宏
- #define pinA 2 //编码器A相
- #define pinB 3 //编码器B相
- #define pinK 4 //编码器按键
- int count = 0; //编码器计数值
- bool en = true; //编码器使能标志
-
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(9600);
- pinMode(pinK, INPUT_PULLUP); //编码器按键设为内部上拉
- Serial.println("Ready");
- }
-
- void loop() {
- // put your main code here, to run repeatedly:
- /*readKey(enable,cycleTime,x,no,onTrigger,onPop) (使能,扫描周期,端口,信号是否反向,上升沿执行动作,下降沿执行动作)
- 按键使能,20ms扫描一次,4号端口,不反向,每次按下时把编码器使能标志反向,弹起时不动作*/
- readKey(true, 20, pinK, false, if (en ^= true) {
- Serial.println("Encoder enable");
- } else {
- Serial.println("Encoder unable");
- }, void());
- /*readEncoder(enable,cycleTime,pinA,pinB,incEvent,dincEvent (使能,扫描周期,A相端口,B相端口,计数增执行动作,计数减沿执行动作)
- 编码器根据使能信号en启动,5ms扫描一次,2、3端口,分别增减计数值*/
- readEncoder(en, 5, pinA, pinB, Serial.println(++count), Serial.println(--count));
- }
复制代码
- #ifndef __EDGE_H__
- #define __EDGE_H__
- //逻辑量的四种状态:低,上升,高,低
- enum edgeSta {low, trg, high, pop};
- //宏名:edge。 返回值:逻辑量状态。输入参数:bool值 用途:检测逻辑量的边沿。
- #define edge(ReadData) ({\
- static bool Trg = false;\
- static bool Cont = false;\
- static bool Pop = false;\
- Trg = ReadData & (ReadData ^ Cont); \
- Pop = Cont != ReadData & !Trg; \
- Cont = ReadData; \
- enum edgeSta sta = low;\
- if (Cont) {\
- sta = high;\
- } else {\
- sta = low;\
- }\
- if (Trg) {\
- sta = trg;\
- }\
- if (Pop) {\
- sta = pop;\
- }\
- sta;\
- })
- #endif
复制代码
- #ifndef __ENCODER_H__
- #define __ENCODER_H__
- #include "edge.h"
- #include "key.h"
-
- //readEncoder(enable,cycleTime,pinA,pinB,incEvent,dincEvent (使能,扫描周期,A相端口,B相端口,计数增执行动作,计数减沿执行动作)
- #define readEncoder(enable,cycleTime,pinA,pinB,incEvent,dincEvent)({\
- static bool no=false;\
- if(edge(enable)==trg){\
- pinMode(pinA, INPUT);\
- pinMode(pinB, INPUT);\
- no = !digitalRead(pinA);\
- }\
- readKey(enable,cycleTime,pinA, no, if (digitalRead(pinB)^no) {incEvent;} else {dincEvent;}, if (digitalRead(pinB)^no) {dincEvent;} else {incEvent;});\
- })
-
- #endif
复制代码
- #ifndef __KEY_H__
- #define __KEY_H__
- #include "edge.h"
-
- //readKey(enable,cycleTime,x,no,onTrigger,onPop) (使能,扫描周期,端口,信号是否反向,上升沿执行动作,下降沿执行动作)
- #define readKey(enable,cycleTime,x,no,onTrigger,onPop) do{\
- static bool Trg = false;\
- static bool Cont = false;\
- static bool Pop = false;\
- static unsigned long timdel = millis();\
- switch(edge(enable)){\
- case trg:\
- Trg = false;\
- Cont = false;\
- Pop = false;\
- timdel = millis();\
- break;\
- case high:\
- if (millis() - timdel >= cycleTime) {\
- bool ReadData = !digitalRead(x)^no;\
- Trg = ReadData & (ReadData ^ Cont); \
- Pop = Cont != ReadData & !Trg; \
- Cont = ReadData; \
- if (Trg) {\
- onTrigger;\
- }\
- if (Pop) {\
- onPop; \
- }\
- timdel = millis(); \
- }\
- break;\
- default:\
- break;\
- }\
- } while (0)
- #define keyReadInit(x) do{\
- pinMode(x, INPUT_PULLUP);\
- } while (0)
- #endif
复制代码
|