1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | import RPi.GPIO as GPIO import time, random """ Display date to LED lights There are four lights, it displays 4 number """ GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) def setp(n, status='on'): if status == 'on': GPIO.output(n, GPIO.HIGH) else: GPIO.output(n, GPIO.LOW) for i in pins + sels: GPIO.setup(i, GPIO.OUT) setp(i, 'off') for i in sels: setp(i, 'on') # # __2_ # | | | 0 -> 011 1111 -> 0x3f # 1 | | 3 | 1 -> 010 0001 -> 0x21 # |__7__| | 2 -> 111 0110 -> 0x76 # | | | 4 -> ... # 6 | | 4 | ... # |__5__| | 9 -> ... -> 0x5f # pins = [27, 17, 22, 10, 25, 24, 11] #GPIO ports sels = [14, 15, 18, 23] #GPIO ports to select led, there are four led lights nums = [0x3f, 0x21, 0x76, 0x5e, 0x4d, 0x5b, 0x7b, 0x0e, 0x7f, 0x5f] def flush(sel, n): setp(sels[sel], 'off') n = nums[n] for i in sels: if i != sels[sel]: setp(i, 'on') for i in range(7): if (n & (1 << i)): setp(pins, 'on') for i in range(7): if (n & (1 << i)): setp(pins, 'off') try: while True: t = time.gmtime() flush(3, t.tm_min / 10) flush(2, t.tm_min % 10) flush(1, t.tm_sec / 10) flush(0, t.tm_sec % 10) except: GPIO.cleanup() |
1 2 3 4 5 6 7 8 | # # __2_ # | | | 0 -> 011 1111 -> 0x3f # 1 | | 3 | 1 -> 010 0001 -> 0x21 # |__7__| | 2 -> 111 0110 -> 0x76 # | | | 4 -> ... # 6 | | 4 | ... # |__5__| | 9 -> ... -> 0x5f |
1 2 3 | pins = [27, 17, 22, 10, 25, 24, 11] #GPIO ports sels = [14, 15, 18, 23] #GPIO ports to select led, there are four led lights nums = [0x3f, 0x21, 0x76, 0x5e, 0x4d, 0x5b, 0x7b, 0x0e, 0x7f, 0x5f] #0,1,2,3~9 |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |