Raspberry Pi Platform 4-bit common yin digital tube python programming

Source: Internet
Author: User

Beginner Python , try to change the program that was previously written in C on the Raspberry Pi. Python version of the. Four-bit common yin digital tube and Raspberry Pi Circuit connection method is as follows:


A ~ p-------------------------------GPIO0 ~ GPIO7

D1 ~ d4-----------------------------GPIO11 ~gpio8

Here is the photo after the circuit is connected:


After connecting the circuit to write the program, the program code is as follows:

#!/usr/bin/env pythonimport Rpi.gpio as Gpioimport timeBIT0 = 3 BIT1 = 5bit2 = 24bit3 = Segcode = [0x3f,0x06,0x5b,0x4f, 0X66,0X6D,0X7D,0X07,0X7F,0X6F] #0 ~9pins = [11,12,13,15,16,18,22,7,3,5,24,26]bits = [BIT0, BIT1, BIT2, Bit3]def Print_ MSG ():p rint ' program is running ... ' print ' please press CTRL + C end the program ... ' Def Digitalwritebyte (val): Gpio.output ( One, Val & (0x01 << 0)) Gpio.output (Val & (0x01 << 1)) Gpio.output (Val & (0x01 << 2)) GP Io.output (Val & (0x01 << 3)) Gpio.output (+ val & (0x01 << 4)) Gpio.output (Val & (0x01 < < 5) Gpio.output (+, Val & (0x01 << 6)) Gpio.output (7, Val & (0x01 << 7)) def display_1 (): GPIO.OUTPU T (BIT0, GPIO. Low) Fori in range:d igitalwritebyte (Segcode[i]) Time.sleep (0.5) def display_2 (): For bit in bits:GPIO.output (bit, GPIO.  Low) Fori in range:d igitalwritebyte (Segcode[i]) Time.sleep (0.5) def display_3 (num): B0 = num% 10b1 = num% 100/10 b2 = Num% 1000/100b3= Num/1000if num < 10:gpio.output (BIT0, GPIO. Low) Gpio.output (BIT1, GPIO. High) Gpio.output (BIT2, GPIO. High) Gpio.output (BIT3, GPIO. High) Digitalwritebyte (segcode[b0]) elif num >= and num < 100:gpio.output (BIT0, GPIO. Low) Digitalwritebyte (segcode[b0]) time.sleep (0.002) gpio.output (BIT0, GPIO. High) Gpio.output (BIT1, GPIO. Low) Digitalwritebyte (SEGCODE[B1]) time.sleep (0.002) gpio.output (BIT1, GPIO. High) elif num >= and num < 1000:gpio.output (BIT0, GPIO. Low) Digitalwritebyte (segcode[b0]) time.sleep (0.002) gpio.output (BIT0, GPIO. High) Gpio.output (BIT1, GPIO. Low) Digitalwritebyte (SEGCODE[B1]) time.sleep (0.002) gpio.output (BIT1, GPIO. High) Gpio.output (BIT2, GPIO. Low) Digitalwritebyte (SEGCODE[B2]) time.sleep (0.002) gpio.output (BIT2, GPIO. High) elif num >= and num < 10000:gpio.output (BIT0, GPIO. Low) Digitalwritebyte (segcode[b0]) time.sleep (0.002) gpio.output (BIT0, GPIO. High) Gpio.output (BIT1, GPIO. Low) Digitalwritebyte (SEGCODE[B1]) time.sleep (0.002) gpio.output (BIT1, GPIO. High) Gpio.ouTput (BIT2, GPIO. Low) Digitalwritebyte (SEGCODE[B2]) time.sleep (0.002) gpio.output (BIT2, GPIO. High) Gpio.output (BIT3, GPIO. Low) Digitalwritebyte (Segcode[b3]) time.sleep (0.002) gpio.output (BIT3, GPIO. High) Else:print ' out of range, NUM should is 0~9999! ' def setup (): Gpio.setmode (GPIO. BOARD) #Number GPIOs by its physical locationfor pin in Pins:GPIO.setup (PIN, GPIO. Out) #set All pins ' mode is outputgpio.output (PIN, GPIO. High) #set all pins is high level (3.3V) def Loop (): While True:print_msg () display_1 () time.sleep (1) display_2 () time.sleep (1) tmp = Int (raw_input (' please input a num (0~9999): ')] for I in range:d isplay_3 (tmp) Time.sleep (1) def destroy (): #Whe N program ending, the function is executed. For pin in Pins:GPIO.output (PIN, GPIO. Low) #set All pins is low level (0V) Gpio.setup (PIN, GPIO. IN) #set all pins ' mode is input if __name__ = = ' __main__ ': #Program starting from here Setup () Try:loop () except Keybo   Ardinterrupt:destroy ()

Demo video Download Link:Http://yunpan.cn/cASWDrmujvBxEExtraction Code 68E4

Raspberry Pi Platform 4-bit common yin digital tube python programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.