- if(!PinA && PinA_O && PinB) {
- Now++;
- }PinA_O = PinA;
- if(!PinB && PinB_O && PinA) {
- Now--;
- }PinB_O = PinB;
复制代码 只有六行代碼就能用EC11對Now進行加減操作
为什么这样写呢?
上时序图
顺时针转:
逆时针转:
我们看到,当顺时针转时
Pin A会早于Pin B 转低电平,反之亦然
代码解读:
!PinA && PinA_O && PinB//当Pin A 为低电平而之前为高电平(即下降沿)并且Pin B为高电平
这一句就捕捉到顺时针转时序图中箭指着的那一刹那的情况
于是Now加1
!PinB && PinB_O && PinA//当Pin B 为低电平而之前为高电平(即下降沿)并且Pin A为高电平
这一句就捕捉到逆时针转时序图中箭指着的那一刹那的情况
于是Now减1
如果编码器不加电容消抖
就用软件消抖
- if(ScanCount++ > 50) { //其数值按单片机速度加减
- ScanCount = 0;
- if(PinA && !PinA_O && PinB) {
- Now++;
- }PinA_O = PinA;
- if(PinB && !PinB_O && PinA) {
- Now--;
- }PinB_O = PinB;
- Now>9? Now = 0:_nop_();
- Now<0? Now = 9:_nop_();
- }
复制代码 现附上小应用实例一则
基如STC15F104E的EC11软串口六位密码检查程序
如发现顺逆时针相反,对调PinA/PinB 定义脚即可
- #include <STC15F104E.H>
- #include "intrins.h"
- #include <stdio.h>
- #include <string.h>
- #define BAUD 0xFF40 //19200bps @ 11.0592MHz
- typedef bit BOOL;
- typedef unsigned char BYTE;
- typedef unsigned int WORD;
- typedef signed char s8; //–128 to 127
- typedef unsigned char u8; //0 to 255
- typedef signed int s16; //-32768 — 32767
- typedef unsigned int u16; //0 — 65535
- typedef signed long s32; //-2147483648 — 2147483647
- typedef unsigned long u32; //0 — 4294967295
- sbit RXB = P3^0; //define UART TX/RX port
- sbit TXB = P3^1;
- sbit PinA = P3^3;
- sbit PinB = P3^4;
- sbit Enter = P3^2;
- BYTE TBUF,RBUF;
- BYTE TDAT,RDAT;
- BYTE TCNT,RCNT;
- BYTE TBIT,RBIT;
- BOOL TING,RING;
- BOOL TEND,REND;
- void UART_INIT();
- void SendString(char *s);
- void SendData(BYTE dat);
- void Delayms(u16 i);
- u32 PW = 0;
- u16 BuffIndex;
- BYTE t, r;
- char buf[30];
- bit PinA_O= 1;
- bit PinB_O= 1;
- bit EnterOns= 1;
- unsigned int ScanCount = 0;
- void main(void) {
- s16 Now = 5;
- s16 Now_O = 0;
- PinA = 1;
- Enter = 1;
- PinB = 1;
- UART_INIT();
- Delayms(1000);
- while (1)
- { //user's function
- if(ScanCount++ > 50) {
- ScanCount = 0;
- if(!PinA && PinA_O && PinB) {
- Now++;
- }PinA_O = PinA;
- if(!PinB && PinB_O && PinA) {
- Now--;
- }PinB_O = PinB;
- Now>9? Now = 0:_nop_();
- Now<0? Now = 9:_nop_();
- if(!Enter && EnterOns) {
- //EnterOns = 1;
- BuffIndex++;
- sprintf (buf, "PW%u = %d\r\n",BuffIndex & 0xFF, Now);
- SendString(buf);
- PW = PW *10 + Now;
- if(BuffIndex == 6) {
- sprintf (buf, "PW input = %Ld\r\nPW is ",PW);
- SendString(buf);
- PW == 12398?SendString("Correct!\r\n"):SendString("Wrong!\r\n");
- PW = 0;
- BuffIndex = 0;
- }
- Now = 5;
- }
- EnterOns = Enter;
- }
- if(Now != Now_O)
- {
- sprintf (buf, "Now = %d\r\n", Now);
- SendString(buf);
- }
- Now_O = Now;
- }
- }
- void Delayms(u16 i) //@ 11.0592MHz
- {
- u8 j, k;
- do
- {
- j = 11;
- k = 190;
- do
- {
- while (--k);
- } while (--j);
- } while (--i);
- }
- //-----------------------------------------
- //Timer interrupt routine for UART
- void tm0() interrupt 1 using 1
- {
- if (--TCNT == 0)
- {
- TCNT = 3; //reset send baudrate counter
- if (TING) //judge whether sending
- {
- if (TBIT == 0)
- {
- TXB = 0; //send start bit
- TDAT = TBUF; //load data from TBUF to TDAT
- TBIT = 9; //initial send bit number (8 data bits + 1 stop bit)
- }
- else
- {
- TDAT >>= 1; //shift data to CY
- if (--TBIT == 0)
- {
- TXB = 1;
- TING = 0; //stop send
- TEND = 1; //set send completed flag
- }
- else
- {
- TXB = CY; //write CY to TX port
- }
- }
- }
- }
- }
- //-----------------------------------------
- //initial UART module variable
- void UART_INIT()
- {
- TMOD = 0x00; //timer0 in 16-bit auto reload mode
- AUXR = 0x80; //timer0 working at 1T mode
- TL0 = BAUD;
- TH0 = BAUD>>8; //initial timer0 and set reload value
- TR0 = 1; //tiemr0 start running
- ET0 = 1; //enable timer0 interrupt
- PT0 = 1; //improve timer0 interrupt priority
- EA = 1; //open global interrupt switch
- TING = 0;
- RING = 0;
- TEND = 1;
- REND = 0;
- TCNT = 0;
- RCNT = 0;
- }
- void SendData(BYTE dat)
- {
- TEND = 0;
- TBUF = dat;
- TING = 1;
- }
- /*----------------------------
- 发送字符串
- ----------------------------*/
- void SendString(char *s)
- {
- while (*s) //检测字符串结束标志
- { if (TEND)
- {
- SendData(*s++); //发送当前字符
- }
- }
- }
复制代码
【必读】版权免责声明
1、本主题所有言论和内容纯属会员个人意见,与本论坛立场无关。2、本站对所发内容真实性、客观性、可用性不做任何保证也不负任何责任,网友之间仅出于学习目的进行交流。3、对提供的数字内容不拥有任何权利,其版权归原著者拥有。请勿将该数字内容进行商业交易、转载等行为,该内容只为学习所提供,使用后发生的一切问题与本站无关。 4、本网站不保证本站提供的下载资源的准确性、安全性和完整性;同时本网站也不承担用户因使用这些下载资源对自己和他人造成任何形式的损失或伤害。 5、本网站所有软件和资料均为网友推荐收集整理而来,仅供学习用途使用,请务必下载后两小时内删除,禁止商用。6、如有侵犯你版权的,请及时联系我们(电子邮箱1370723259@qq.com)指出,本站将立即改正。
|