1024线编码器arduino uno驱动程序

硬件

编码器:https://item.taobao.com/item.htm?spm=a1z0k.7386009.0.d4919233.28275ed5nlaXMq&id=38701256712&_u=t2dmg8j26111
规格选:带方向输出5v
arduino uno板

接线

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码

//1024线编码器,encoder_B判断旋转方向,encoder_A记录步进脉冲的变化次数,1024线转1圈会有2048次变化,所以每次加0.5
int encoder_A= 2; //检测脉冲
int encoder_B=3;  //检测方向
volatile float encoderCount=0; //用来记下走的步数,顺时针为正,long类型是4个字节 ,为volatile易变变量

void setup() {
  Serial.begin(115200);
  pinMode(encoder_A, INPUT);
  pinMode(encoder_B, INPUT);
  //中断服务程序: 中断Arduino当前正在处理的事情而优先去执行中断服务程序,当中断服务程序完成以后,再回来继续执行刚才执行的事情
  attachInterrupt(digitalPinToInterrupt(encoder_A), ISR_EncoderA, CHANGE); 
//  attachInterrupt(digitalPinToInterrupt(encoder_B), ISR_EncoderB, CHANGE);
}

void ISR_EncoderA() {
    if (digitalRead(encoder_B) == HIGH) 
    {
      encoderCount=encoderCount+0.5; }        //CCW,逆时针
    else 
    {
      encoderCount=encoderCount-0.5; }       //CW, 顺时针
     
 }
void loop()
{
  Serial.println(encoderCount);
}

注意

串口波特率不能小了,小的话会丢失。
中断服务程序参考:http://www.taichi-maker.com/homepage/reference-index/arduino-code-reference/attachinterrupt/
易变变量参考:http://www.taichi-maker.com/homepage/reference-index/arduino-code-reference/volatile/

上传出错

如果出现上传程序报错:串口不存在等等,换一个usb口插试一试,或者上传的时候按一下板子上的reset键;
如果出现上传一直卡住不动的情况,按一下板子上的reset键。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千里飞刀客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值