Explain code example - Raspberry Pi Forums
can explain going on in code example? bought kit sunfounder , terrible job of explaining project examples. book has wiring diagrams , code go along it. wrote code knowledge have, , did in 150+ lines of code, did in 50. it's shift register wired 7 segment display, , displays 0 - 9, - f in loop.
don't understand hexadecimal thing in hc595_shift(dat) , loop().
don't understand hexadecimal thing in hc595_shift(dat) , loop().
code: select all
#!/usr/bin/env python import rpi.gpio gpio import time sdi = 11 rclk = 12 srclk = 13 segcode = [0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x80] def print_msg(): print 'program running...' print 'please press ctrl+c end program...' def setup(): gpio.setmode(gpio.board) #number gpios physical location gpio.setup(sdi, gpio.out) gpio.setup(rclk, gpio.out) gpio.setup(srclk, gpio.out) gpio.output(sdi, gpio.low) gpio.output(rclk, gpio.low) gpio.output(srclk, gpio.low) def hc595_shift(dat): bit in range(0, 8): gpio.output(sdi, 0x80 & (dat << bit)) gpio.output(srclk, gpio.high) time.sleep(0.001) gpio.output(srclk, gpio.low) gpio.output(rclk, gpio.high) time.sleep(0.001) gpio.output(rclk, gpio.low) def loop(): while true: in range(0, len(segcode)): hc595_shift(segcode[i]) time.sleep(0.5) def destroy(): #when program ending, function executed. gpio.cleanup() if __name__ == '__main__': #program starting here print_msg() setup() try: loop() except keyboardinterrupt: destroy()
gpio.output(sdi, 0x80 & (dat << bit))
shift , mask operation. dealing here 8 bit byte. << operator performs shift dat shifted left bit times. if bit 7 bottom bit of dat shifted top. & operator performs bitwise and. if bit set in both operands set in result. 0x80 has top bit set result of 0x80&var non 0 of top bit in var set.
not sure why choose test top bit rather bottom iether work.
hope helps.
shift , mask operation. dealing here 8 bit byte. << operator performs shift dat shifted left bit times. if bit 7 bottom bit of dat shifted top. & operator performs bitwise and. if bit set in both operands set in result. 0x80 has top bit set result of 0x80&var non 0 of top bit in var set.
not sure why choose test top bit rather bottom iether work.
hope helps.
raspberrypi
Comments
Post a Comment