wwwymq 发表于 2016-6-6 01:32:09

arduino宏的应用实例7--读编码器模块

本帖最后由 wwwymq 于 2016-6-6 11:56 编辑

用于读取旋转编码器模块


//非阻塞编码器模块读取,通过编码器按键使能编码器事件,当编码器使能时分别增减计数值。
//未用到中断,通过轮询读取编码器,周期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

wwwymq 发表于 2016-6-6 16:17:04

超大玉挽尊术。

PINKWALKMAN 发表于 2016-6-7 07:57:53

字写得好。

Hackerpro 发表于 2016-6-7 14:39:50

有时间试试,我前一阵子,用stm32折腾了好久,用的外部中断,总是出错,干扰很严重。
不过stm32官方提供了编码器的专用接口,会好用点

wwwymq 发表于 2016-6-7 16:22:21

本帖最后由 wwwymq 于 2016-6-7 16:23 编辑

Hackerpro 发表于 2016-6-7 14:39 static/image/common/back.gif
有时间试试,我前一阵子,用stm32折腾了好久,用的外部中断,总是出错,干扰很严重。
不过stm32官方提供了 ...

这个用起来也差强人意,手头有个最快300k的编码器,stm32f030硬件编码器接口采样,稍快一点就丢脉冲,实在是不敢用,stm32并不是高级的数采芯片,硬件的编码器接口也只能对付些低速的编码器。
真有需要还是数据采集卡靠谱。

Hackerpro 发表于 2016-6-8 11:50:50

wwwymq 发表于 2016-6-7 16:22 static/image/common/back.gif
这个用起来也差强人意,手头有个最快300k的编码器,stm32f030硬件编码器接口采样,稍快一点就丢脉冲,实 ...

嗯,只是控制个菜单还可以,要求不太高

太行摄狼 发表于 2019-4-5 21:09:48

今天网上翻了半天,只有楼主的程序是可用的
页: [1]
查看完整版本: arduino宏的应用实例7--读编码器模块