2

So i'm currently playing with my Raspberry Pi and 7 segment(actually 8 because there is dot) display and i need help. I know how to operate it using single LEDs(so for example i know which LEDs i need to light up to create "1" so i can basically operate it) but there was code in manual that's using some bitwise logic which is far beyond my understanding.

    #!/usr/bin/env python
import RPi.GPIO as GPIO
import time

pins = [11,12,13,15,16,18,22,7]
dats = [0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x80]

def setup():
    GPIO.setmode(GPIO.BOARD)
    for pin in pins:
        GPIO.setup(pin, GPIO.OUT)   # Set pin mode as output
        GPIO.output(pin, GPIO.LOW)

def writeOneByte(val):
    GPIO.output(11, val & (0x01 << 0))  
    GPIO.output(12, val & (0x01 << 1))  
    GPIO.output(13, val & (0x01 << 2))  
    GPIO.output(15, val & (0x01 << 3))  
    GPIO.output(16, val & (0x01 << 4))  
    GPIO.output(18, val & (0x01 << 5))  
    GPIO.output(22, val & (0x01 << 6))  
    GPIO.output(7,  val & (0x01 << 7)) 

def loop():
    while True:
        for dat in dats:
            writeOneByte(dat)
            time.sleep(0.5)

def destroy():
    for pin in pins:
        GPIO.output(pin, GPIO.LOW)
    GPIO.cleanup()             # Release resource

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()

The code above cycles through 0-9 and A-F and then dot.

Every help/explanation/advice is welcome.

EDIT: I GET IT NOW. Just turned on my brain. CLOSED.

1
  • I posted an answer anyway, just in case somebody stumbles upon this page from Google.
    – zarak
    Aug 17, 2016 at 16:02

1 Answer 1

1

SETUP

I'm going to refer to this image I found on a thread on reddit:

seven segment truth table

So the pins correspond to the segments as follows:

A: 11
B: 12
C: 13
D: 15
E: 16
F: 18
G: 22


Now let's see what happens when we call writeOneByte with the first value in the list named dats, i.e 0x3f.

The first line in the function is

GPIO.output(11, val & (0x01 << 0)) 

val holds the value 0x3f which is what was passed into the function. This value is equivalent to 0b00111111 in binary. Looking again at the diagram, we see that the letters A-G actually correspond to each bit, from the least significant (right most bit) to the second most significant bit. The most significant bit is reserved for the dot.


BITWISE OPERATIONS

The << is the left-shift operator. The code 0x01 << 0 means shift the number 0x01 zero bits to the left. Therefore, the operation leaves the number unchanged, and the result is still 0x01, which is equivalent to 0b00000001.

Next up is &, the bitwise AND operator. This performs a logical AND operation on a bit-by-bit basis. It's easy to see in an example:

0b00111111
0b00000001
----------
0b00000001  

Essentially what we are doing is checking the first (least significant) bit of the number in val and checking to see whether the corresponding pin should have a HIGH or a LOW output. In this case, the value of the first bit is 1. After the AND operation, the value passed to GPIO.output for pin 11 (which corresponds to segment A), is 0b00000001. This outputs a HIGH value, because anything that is not all zeroes translates into a boolean true.

The process is repeated on the remaining lines of code:

GPIO.output(12, val & (0x01 << 1))

This time 0b00000001 is shifted 1 bit to the left, yielding 0b00000010. We perform the bitwise AND again:

0b00111111
0b00000010
----------
0b00000010

which is also a HIGH output for pin 12, and thus segment B is on.

In fact, the only time we get a LOW is with pin 7, in the last line:

GPIO.output(7,  val & (0x01 << 7))

0b00111111
0b01000000
----------
0b00000000

As you can see in the diagram, the bits that drive the pins take on the appropriate values to yield the number 0 in the given display arrangement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.