查看: 1638|回复: 0
收起左侧

树莓派可以为编码器正交解码并计数吗?

2022-3-7 14:59:35 | 显示全部楼层 |阅读模式
[ 本帖最后由 asdfasfa 于 2022-3-7 15:10 编辑 ]\n\n[ 本帖最后由 asdfasfa 于 2022-3-7 15:03 编辑 ]\n\n我用的是omron E6B2-cwz6c 2000,程序如下import RPi.GPIO as GPIO
RoAPin = 11
globalCounter = 0
flag = 0
Last_RoB_Status = 0
Current_RoB_Status = 0

def setup():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(RoAPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    # input mode
    GPIO.setup(RoBPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(RoCPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def rotaryDeal(a):
        global globalCounter
      if GPIO.input(RoBPin):
                globalCounter -= 1
      else:
        globalCounter += 1

def btnISR(channel):
    global globalCounter
    globalCounter = 0

def loop():
    global globalCounter    tmp = 0
    GPIO.add_event_detect(RoCPin, GPIO.RISING, callback=btnISR)
    GPIO.add_event_detect(RoAPin, GPIO.RISING, callback=rotaryDeal)
        while True:
        if abs(tmp - globalCounter) > 10:
            print('globalCounter = %d' % globalCounter)
            tmp = globalCounter

def destroy():
    GPIO.cleanup()
if __name__ == '__main__':
     # Program start from here
    setup()
    try:
        loop()
    except KeyboardInterrupt:
  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
        destroy()
编码器转一圈2000个计数,我只能显示100左右,还是转得很慢的情况下
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

热点推荐

关注我们,了解更多

官方微信

服务时间:10:00-16:00

13714503811

公司地址:深圳市龙岗区南湾街道东门头路8号

Copyright © 2012-2020 Powered by 树莓派论坛 2019.4  粤ICP备15075382号-1
快速回复 返回列表 返回顶部