将旋转编码器与AVR Micro控制器一起使用 [英] Using a rotary encoder with AVR Micro controller

查看:144
本文介绍了将旋转编码器与AVR Micro控制器一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使旋转编码器与AVR微型控制器一起正常工作.该编码器是机械 ALPS编码器,而我m使用 Atmega168 .

I'm having trouble getting a rotary encoder to work properly with AVR micro controllers. The encoder is a mechanical ALPS encoder, and I'm using Atmega168.

说明

我尝试使用外部中断来监听引脚,但似乎太慢了.当引脚A变为高电平时,中断过程开始,然后检查引脚B是否为高电平.这个想法是,如果在引脚A变为高电平的那一刻,引脚B为高电平,则它会逆时针旋转.如果引脚B为低电平,则它沿顺时针方向旋转.但是看来AVR检查B针的时间太长了,所以它总是读得很高.

I have tried using an External Interrupt to listen to the pins, but it seems like it is too slow. When Pin A goes high, the interrupt procedure starts and then checks if Pin B is high. The idea is that if Pin B is high the moment Pin A went high, then it is rotating counter clock-wise. If Pin B is low, then it is rotating clock-wise. But it seems like the AVR takes too long to check Pin B, so it is always read as high.

我还尝试创建一个程序,该程序只是阻塞直到Pin B或Pin A更改为止.但是旋转编码器时可能会有太多的噪音,因为这也不起作用.我最后一次尝试是使用一个计时器,将最后8个值存储在缓冲区中,并检查它是否从低变高.这也不起作用.

I've also tried to create a program that simply blocks until Pin B or Pin A changes. But it might be that there is too much noise when the encoder is rotated, because this does not work either. My last attempt was to have a timer which stores the last 8 values in a buffer and checks if it is going from low to high. This did not work either.

我尝试对编码器进行范围设定,从第一个Pin更改到另一个Pin更改似乎要用2到4毫秒.

I have tried scoping the encoder, and it seems to use between 2 and 4ms from the first Pin changes till the other Pin changes.

推荐答案

我有一个有关旋转编码器及其使用方法,您会发现它很有用.

I have a webpage about rotary encoders and how to use them, which you might find useful.

很遗憾,没有更多信息,我无法解决您的特定问题.

Unfortunately without more information I can't troubleshoot your particular problem.

哪些微控制器引脚连接到编码器,您当前使用什么代码对脉冲进行解码?

Which microcontroller pins are connected to the encoder, and what is the code you're currently using to decode the pulses?

好吧,您要处理几个不同的问题,第一个问题是这是一个机械编码器,因此您必须处理开关噪声(弹跳,颤动). 数据表表示,零件停止弹跳并可能需要3ms的时间,创建错误的输出.

Ok, you're dealing with a few different issues, the first issue is that this is a mechanical encoder, so you have to deal with switch noise (bounce, chatter). The data sheet indicates that it may take up to 3mS for the parts to stop bouncing and creating false outputs.

您需要创建一个防抖动例程.最简单的方法是连续检查A是否变高.如果是这样,请启动一个计时器,然后在3毫秒内再次检查它.如果它仍然很高,则可以检查B-如果它不很高,则可以忽略杂散脉冲并继续寻找A高.选中B时,您将对其进行查看,启动一个计时器3毫秒,然后再次查看B.如果两次都相同,则可以使用该值-如果它在3毫秒内更改,则必须再次执行该操作(读取B,等待3毫秒,然后再次读取并查看是否匹配).

You need to create a debounce routine. The simplest of which is to continuously check to see if A goes high. If it does, start a timer and check it again in 3 ms. If it's still high, then you can check B - if it's not high then you ignore the spurious pulse and continue looking for A high. When you check B, you look at it, start a timer for 3 ms, and then look at B again. If it was the same both times, then you can use that value - if it changes within 3 ms then you have to do it again (read B, wait 3 ms, then read it again and see if it matches).

atmega足够快,因此您不必担心这些检查会缓慢进行,除非您的时钟速度也很慢.

The atmega is fast enough that you shouldn't have to worry about these checks going slowly, unless you're also running a slow clock speed.

一旦您处理了机械噪声,那么您就想看一个合适的格雷码例程-您要遵循的算法将不起作用,除非您还降低了当B变低时A变高的情况.通常,人们存储两个输入的最后一个值,然后将其与两个输入的新值进行比较,并使用一个小函数基于该值递增或递减. (请查看我在上面提到的网站上的高分辨率阅读"标题表).我将两个读数合并为一个四位数字,并使用一个简单的数组告诉我是增加还是减少计数器,但是有些解决方案甚至更高级,并且可以优化代码大小,速度或简化代码维护.

Once you deal with the mechanical noise, then you want to look at a proper gray code routine - the algorithm you're following won't work unless you also decrement if A is high when B goes low. Generally people store the last value of the two inputs, and then compare it to the new value of the two inputs and use a small function to increment or decrement based on that. (Check out the heading "high resolution reading" on the website I mentioned above for the table). I combine the two readings into a four bit number and use a simple array to tell me whether I increment or decrement the counter, but there are solutions that are even more advanced, and optimize for code size, speed, or ease of code maintenance.

这篇关于将旋转编码器与AVR Micro控制器一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆