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...

JLink V9 Bootloader

v9里面大家最关心的就是bootloader了吧? [https://www.amobbs.com/thread-5653964-1-1.html]   论坛上有很多前辈都给出了各种办法把这个bootloader弄出来,可是都是片言只语,所以这里我整理一下大概的两个办法。 首先是个最简单的办法,不用拆机,没有风险,原理很简单,很多年前论坛上的大牛就发现并公布了这个办法,不过据说后来论坛浮云过一回,资料丢了。 这个办法利用的是jlink自带的一个命令,这个命令能读取jlink自身的内存,我们只是需要用这个命令把bootloader部分的内容读取出来就可以了。 在进入这个具体命令之前,我们来看一下jlink的操纵方法,比较普遍的做法是调用jlinkarm.dll公开的接口,再有sdk的情况下,调用这些接口并不麻烦, 但是如果没有sdk的话,c/c++语言要调用这些接口显得特别的麻烦,所以这里我们使用更为底层的办法越过jlink的dll,直接和jlink的驱动打交道。 首先,我们需要找到系统里面的jlink这个设备 GUID classGuid = {0x54654E76, 0xdcf7, 0x4a7f, 0x87, 0x8A, 0x4E, 0x8F, 0x0CA, 0x0A, 0x0CC, 0x9A}; auto devInfoSet = SetupDiGetClassDevsW(&classGuid, nullptr, nullptr, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); 用上面的代码先找到jlink的class 然后我们需要枚举这个devInfoSet里面的全部成员,依次取得各种信息,最终拿到jlink驱动提供的设备路径 SP_DEVICE_INTERFACE_DATA interfaceData = {0}; interfaceData.cbSize = sizeof(interfaceData); for(DWORD i = 0; ; i ++) {     if(!SetupDiEnumDeviceInterfaces(devInfoSet, nullptr, &classGuid, i, ...