Arduino
介绍旋转编码器程序
介绍
旋转或编码器是一个角度测量装置
.
他用于准确测量电机的旋转角度或控制轮子
子(可无限旋转,电位器只能旋转到特定位置)
。其中一些也可以安装在轴上
按钮就像音乐播放器的控制按钮。
Some
of
them
are
also
equipped
with
a
pushbutton
whenyoupressontheaxis(liketheonesusedfornavigationonmanymusiccontrollers).
它们的
精度多种多样,有每圈
16
步到
1024
各种步骤,价格也从
2
到
200
欧元不等。
我写了一个小例子来读取旋转编码器,并通过阅读将读数
RS232
显示。很容易实现当编
每走一步,码器更新计数,并通过串口显示在计算机上(通过串口监视器)
。这个程
序在
ALPSSTEC12E08
编码器(每圈都有
24
步骤)运行良好。但我认为当它用于一个时
当电机快速旋转时,更高精度的编码器可能会失效,
或者你扩展这个程序来适应更多
编码器。请先试试他。
我在
Arduinodistribution
(
A
VRLib
部分)
encoder.h
学会了如何操作编码器。
作者:
PascalStang
,感谢他对每个函数的友好和详细的解释。
Example1
/*ReadQuadratureEncoder
*ConnectEncodertoPinsencoder0PinA,encoder0PinB,and 5V.
*
*Sketchbymaxwolf/www.meso.net
*v.0.1-verybasicfunctions-mw20061220
*
*/
intval;
intencoder0PinA=3;
intencoder0PinB=4;
intencoder0Pos=0;
intencoder0PinALast=LOW;
intn=LOW;
voidsetup(){
pinMode(encoder0PinA,INPUT);
pinMode(encoder0PinB,INPUT);
Serial.begin(9600);
}
voidloop(){
n=digitalRead(encoder0PinA);
if((encoder0PinALast==LOW)&&(n==HIGH)){//
上升沿