Skip to main content

Driving Rotary Encoder w/o Interruptiong

本程序用于测试,
编译器采用CodeVisionAVR
 

#include <delay.h>
#include <mega8.h>

#define IN_A PINB.1
#define IN_B PINB.2
unsigned char encoder_now;//本次值
unsigned char encoder_ago;//上次值

flash signed char encoder_tab[4][4]=
{
{0        ,1        ,-1        ,0},
{-1,        0,        0,        1},               
{1        ,0,        0        ,-1        },
{0        ,-1        ,1        ,0        },       
};
signed char encoder_read(void)
{
    signed char a=0;
    unsigned char encoder_filter=0;  //滤波

    while(a<10)//判断次数,进行滤波
    {
        encoder_now=IN_A;
        encoder_now<<=1;
        encoder_now+=IN_B;
        if(encoder_filter==encoder_now)a++;
        else a=0;
        encoder_filter=encoder_now;
    }

    a=encoder_tab[encoder_now][encoder_ago];
    encoder_ago=encoder_now;
    return a;
}

main()
{
while (1)
      {

      switch(encoder_read())
      {
      case 1:
            PORTB.2=1;
          delay_ms(10);
      break;
    
      case -1:
            PORTB.3=1;
          delay_ms(10);
      break;
    
      case 0:
        PORTB.2=0;
        PORTB.3=0;
         
      break;
      }

      };

}



说明一下示波器输入A,B为模拟的编码器的两线输入波形,C,D为解码后的输出正负,和脉冲个数,值得一提的是实际编码器每拨动一格就会有两个脉冲输出,所以把连续的两个脉冲作为一个有效的加减标志可以提高可靠性(本例未体现)
 


原理波形

Popular posts from this blog

Installing Debian on QNAP HS-210, TS-21x and TS-22x devices

QNAP's original firmware has a lot of bloatware. If you want to has a lean NAS with more choices and controls on what are running inside the box, here is a webpage to install Debian, my favorite distro, into TS-212p. [https://www.cyrius.com/debian/kirkwood/qnap/ts-219/install/] Overview In a nutshell, the installation of Debian on your QNAP HS-210, TS-21x or TS-22x works like this: you use the QNAP firmware to write a Debian installer image to flash. When you restart your device, Debian installer starts and allows you to login via SSH to perform the installation. Debian will be installed to disk and a Debian kernel will be put in flash that will start Debian from disk. If you follow this procedure, Debian 9 (stretch) will be installed to your SATA disk and the QNAP firmware on disk and in flash will be replaced with Debian. Debian does not install a web interface to configure your machine, although it's possible to install such software. If this is not what you want, pleas...

Build Marvell ARMv5 toolchain with Crosstool-NG

Notes: under Ubuntu 16.04   0. Prepeare - install required tools 1. Install crosstool-ng # mkdir -p scratch/ct-ng; cd scratch/ct-ng # wget http://crosstool-ng.org/download/crosstool-ng/crosstool-ng-1.18.0.tar.bz2 # tar xf crosstool-ng-1.18.0.tar.bz2 # cd crosstool-ng-1.18.0 # ./configure --prefix=`pwd`/crosstool # make && make install # export PATH="${PATH}:`pwd`/crosstool/bin" # ct-ng help This is crosstool-NG version 1.18.0 2. Configure marvell ARMv5 toolchain # mkdir -p ~/scratch/marvell; cd ~/scratch/marvell # ct-ng list-samples  [G.X]   arm-cortex_a15-linux-gnueabi  [G..]   arm-cortex_a8-linux-gnueabi  [G..]   arm-davinci-linux-gnueabi  [G..]   arm-unknown-eabi  [G..]   arm-unknown-linux-gnueabi  [G.X]   arm-unknown-linux-uclibcgnueabi  [G..]   x86_64-unknown-linux-gnu  [G..]   x86_64-unknown-linux-uclibc  [G.X]   x86_64-unknown-mingw3...

Recovery mode of QNAP HS-210, TS-21x and TS-22x devices

If, somehow, hard drive becomes unreadable, use the method below to restore installation[ https://www.cyrius.com/debian/kirkwood/qnap/ts-219/recovery/] Recovery mode of QNAP HS-210, TS-21x and TS-22x devices QNAP HS-210, TS-21x and TS-22x devices have a recovery mode that can be used when there is a problem with your installation of Debian that renders your device unbootable. The system recovery mode allows you write a recovery image to flash via the network using the TFTP protocol. This pages describes how how to create recovery images and how to use the recovery mode. As an alternative to the instructions on this page, you can use a  Live CD provided by QNAP . Creating recovery images In order to create a recovery image for your QNAP TS-21x/TS-22x, you have to take an exact copy of your flash memory. That is, the recovery image consists of the following parts of your flash in this order:  mtd0 ,  mtd4 ,  mtd5 ,  mtd1 ,  mtd2 ,  mtd3 . ...