pythonbitwise-operatorsmotordriver

What is happening during this operation 0x99<<j & (0x80>>i))


i've been tinkering around, and i have googled and searched about bitwise operations. and i think i understand some of it, however i have a piece of code i have copied from someone online which allows it to move a 28byj-48 5v motor and driver in python. its actually the only code i've found in python that it would allow it to work.

however, i tried breaking it down with my own code and cant seem to figure out why mine wont work but this one does.

here it is, the working one:

import RPi.GPIO as GPIO
from time import sleep

#configuring pins and motors
motorPin = (18,23,24,25)     #GPIO pin in BCM mode refer to map
rolePerMinute =13
stepsPerRevolution = 2048
stepSpeed = (60/rolePerMinute)/stepsPerRevolution


#setup the pins to the motor
def setup():
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)

    for i in motorPin:
        GPIO.setup(i, GPIO.OUT)



def rotary1():
    n =1
    while n < 10000:
        for j in range(4):
            for i in range(4):
                GPIO.output(motorPin[i],0x99>>j & (0x08>>i))
            sleep(stepSpeed)
            n +=1

and here is mine:


import RPi.GPIO as GPIO
from time import sleep

#configuring pins and motors
motorPin = (18,23,24,25)     #GPIO pin in BCM mode refer to map
rolePerMinute =13
stepsPerRevolution = 2048
stepSpeed = (60/rolePerMinute)/stepsPerRevolution


#setup the pins to the motor
def setup():
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)

    for i in motorPin:
        GPIO.setup(i, GPIO.OUT)

def rotary():
    n = 1
    while n < 10000:

        GPIO.output(18,0x99>>1 & (0x08>>1))
        sleep(stepSpeed)
        GPIO.output(23,0x99>>2 & (0x08>>2))
        sleep(stepSpeed)
        GPIO.output(24,0x99>>3 & (0x08>>3))
        sleep(stepSpeed)
        GPIO.output(25,0x99>>4 & (0x08>>4))
        sleep(stepSpeed)
        n+= 1

if im understanding this correctly, 0X99 is transformed into a bit "10011001" and its ">>" bit wise operator is pushing it by 1 to the left?

this is where im confused, and also why wouldn't my stepped version work versus the first version.

if someone could help me understand this, i would be greatful.

many thanks.

i've tried googling how bitwise work and watched a very informative video, however i still couldnt understand how << or >> works in this case with Hex values. I also couldn't get my stepped version to work, versus the other.


Solution

  • The working code uses nested loops, so it's processing the cartesian product of 0 through 3. Unrolled, it's equivalent to

            GPIO.output(18,0x99>>0 & (0x08>>0))
            sleep(stepSpeed)
            GPIO.output(18,0x99>>0 & (0x08>>1))
            sleep(stepSpeed)
            GPIO.output(18,0x99>>0 & (0x08>>2))
            sleep(stepSpeed)
            GPIO.output(18,0x99>>0 & (0x08>>3))
            sleep(stepSpeed)
            GPIO.output(18,0x99>>1 & (0x08>>0))
            sleep(stepSpeed)
            GPIO.output(18,0x99>>1 & (0x08>>1))
            sleep(stepSpeed)
            GPIO.output(18,0x99>>1 & (0x08>>2))
            sleep(stepSpeed)
            GPIO.output(18,0x99>>1 & (0x08>>3))
            sleep(stepSpeed)
            GPIO.output(18,0x99>>2 & (0x08>>0))
            sleep(stepSpeed)
            GPIO.output(18,0x99>>2 & (0x08>>1))
            sleep(stepSpeed)
            GPIO.output(18,0x99>>2 & (0x08>>2))
            sleep(stepSpeed)
            GPIO.output(18,0x99>>2 & (0x08>>3))
            sleep(stepSpeed)
            GPIO.output(18,0x99>>3 & (0x08>>0))
            sleep(stepSpeed)
            GPIO.output(18,0x99>>3 & (0x08>>1))
            sleep(stepSpeed)
            GPIO.output(18,0x99>>3 & (0x08>>2))
            sleep(stepSpeed)
            GPIO.output(18,0x99>>3 & (0x08>>3))
            sleep(stepSpeed)
    

    Your version only performs the steps where i == j. It's also going from 1 to 4 instead of 0 to 3.