Linux Format

8-bit counter

Sean Conway needed a computer hardware display for a high school career symposium, to demonstrat­e binary counting – so he built one.

- Sean D. Conway Retiring after a 37-year career in technology left a void for Sean that has been filled with projects which start with the words “I can do that with a Pi.”

Sean D. Conway needed a hardware display for a high school career symposium to demonstrat­e binary counting.

We’re taking a somewhat different tack for this tutorial. We’ll be learning about the binary numbering system and using Python to build an LED 8-bit binary counter. In digital electronic­s, a binary number is a base-2 numeral system that uses two symbols, zero and one. By comparison, the decimal number system requires ten different symbols or digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. In digital electronic­s this is called the base-10 numeral system.

In either base system, the symbols that are used to denote a number take on a different place value depending upon their position. Using the decimal numeral system, the number 114 represents the sum of (1 x 102) + (1 x 101) + (4 x 100). The digit symbol 1 is in the hundreds position, the next digit symbol 1 is in the tens position, and the digit symbol 4 stands in the ones position. To represent decimal numbers in binary form requires many more of the few binary symbols. With all those ones and zeros strung out on a line, it can quickly

get confusing. So let’s examine some concepts we can use to help us navigate the binary number system. The most significan­t bit (MSB) is always on the left. The least significan­t bit (LSB) in a binary number will always appear on the right. The MSB will have a higher decimal equivalent positional value than the LSB. In the diagram (below left), bit 7 has a decimal equivalent value of 128. If the number was larger than a decimal value of 255, the next bit to the left would have a position value of 256, then 512 and 1024 so on. The positional value doubles as you move from each position right (LSB) to left (MSB).

The LSB in an 8-bit number occupies position bit0. The MSB in an 8-bit number occupies position bit7. Binary number systems start with bit0 and then work their way up. Binary values in the positions of bit0 to bit7 gives us the resulting eight bits.

The decimal number 114 can be represente­d in the binary number system by the sequence of binary bits 01110010 . The decimal equivalent can be confirmed by summing up the positional values: (0 x 27) + (1 x 26) + (1 x 25) + (1 x 24) + (0 x 23) + (0 x 22) + (1 x 21) + (0 x 20)=64+32+16+2=114.

The concept of a bit0 position can be confusing. Just remember that binary systems respect zero so much that they assign it a position. Binary positional theory becomes important later on when we start to use Python. You will experience problems if you want to change the LSB in a binary number and fail to take into account that bit0 is the starting point.

Shifting bits

If all the bits in a binary number are shifted to the left by one position, it’s equivalent to multiplyin­g the number by two. If all the bits in a binary number are shifted to the right by one position, this is equivalent to dividing the number by two. When shifting left or right, the fill bit coming in for the ‘vacant’ position is zero.

Counting in decimal uses the ten symbols by incrementi­ng the rightmost digit and substituti­ng the value: 0+1=1, 1+1=2, 2+1=3… When 0 through 9 symbols are used up for the position, the symbol value returns to 0 and the next higher digit is incremente­d: 9+1=10, 10+1=11, 11+1=12… This method of incrementi­ng, overflowin­g to the next higher position and return to 0 is repeated for each digit.

Counting in binary uses the same process, but with fewer symbols. Starting with a single-digit symbol, the count is incremente­d to the next symbol, overflow changes the next higher position symbol and the value starts again at zero. 0+1=1, 1+1=10, 10+1=11, 11+1=100. Binary has only two symbols, 1 and 0, and this reduces the number of changes in one position.

Let’s examine a few more concepts from the binary count table. For example, 4-bit binary can be used to represent decimal values of 0 to 15: so 1111 in base-2 = 15 in base-10. In other words, there are 16 possible combinatio­ns using the four bits. To represent a decimal number higher than a value of 15, an additional bit would be needed.

So, what is the highest decimal value that can be represente­d using eight binary bits? And how many possible combinatio­ns exist using the eight binary bits? You should have worked out that the answers are 255 and 256 respective­ly. There is always one more combinatio­n value for each decimal number, because the value zero starts the count in binary and is considered a combinatio­n.

The concepts of LSB, MSB, bit0, and combinatio­ns versus values using the binary numeral system are foundation­s for future study. If you decide to explore the topic of Internet Protocol (IP) addressing and the division of hosts and networks created by using a subnet mask, the bit positionin­g and manipulati­on explored so far may come in handy.

Exploring concepts with code

Using the binary knowledge accumulate­d so far, let’s now use the Raspberry Pi’s GPIO outputs connected to a series of LEDS to create a light-changing binary counter, controlled by Python code.

Python is capable of translatin­g values between different number systems. Entering the command bin(114) through a Python IDLE console displays 0b1110010 . The prefix 0b indicates a binary value. Enter 0b1110010 through the IDLE console and 114

is displayed.

In Python, though, it’s not necessary to convert decimal numbers to binary numbers before using the binary operators. The << and >> operators are responsibl­e for performing a binary left- or right-shift, regardless of the number’s system base. Remember that an arithmetic shift-left in binary was equivalent to multiplyin­g by two. Enter y=114<<1 followed by print y

and you will discover 114 was multiplied by 2, resulting in 228 being displayed.

Find the Python code found on the LXFDVD or the Archives section of https://linuxforma­t.com: bit0 = 3 bit1 = 5 bit2 = 7 bit3 = 29 bit4 = 31 bit5 = 26 bit6 = 24 bit7 = 21 count = 0

Callout 1: The bit# variable values are the individual GPIO pins, physically connected though an LED in series with a 1000 ohm resistor to ground. The values bit0 to bit7 make an 8-bit row of LEDS. The last variable count initialise­s the starting value of the

counter to 0.

Gpio.setwarning­s(false) GPIO.SETMODE(GPIO.BOARD) Gpio.setup(bit0, GPIO.OUT) #initialize pins Gpio.setup(bit1, GPIO.OUT) Gpio.setup(bit2, GPIO.OUT) Gpio.setup(bit3, GPIO.OUT) Gpio.setup(bit4, GPIO.OUT) Gpio.setup(bit5, GPIO.OUT) Gpio.setup(bit6, GPIO.OUT) Gpio.setup(bit7, GPIO.OUT)

This establishe­s the GPIOS as output pins.

try: while True:

Callout 3: This is the decimal counter that is incremente­d by one after each iteration. The %

(modulo) operator yields a remainder of 0 from a division if there is no remainder. When the count reaches 255, all the eight binary bits have a value of 1, so the modulo operator resets the value of count back to zero to start another count.

count = (count + 1) % 255

Callout 4: Remember >> is the shift-right operator. The operand that follows is the bit position. The code tests the bit position and returns the value of 1 if the bit in the position is 1. The value returned is used to set the GPIO output. If the bit position is 1 then the LED is on.

Gpio.output(bit0, (count >> 0) & 1) # test binary Gpio.output(bit1, (count >> 1) & 1) Gpio.output(bit2, (count >> 2) & 1) Gpio.output(bit3, (count >> 3) & 1) Gpio.output(bit4, (count >> 4) & 1) Gpio.output(bit5, (count >> 5) & 1) Gpio.output(bit6, (count >> 6) & 1) Gpio.output(bit7, (count >> 7) & 1) time.sleep(.1) except Keyboardin­terrupt: # if ctrl+c exit cleanly

Gpio.cleanup() except: # catch all other exceptions including errors.

Gpio.cleanup() finally: # cleanup GPIO on normal exit Gpio.cleanup()

This Python code running on a Pi with GPIOS connected to LEDS creates a perpetual binary counter that displays the decimal values 0 to 255 as a row of LEDS of eight binary bits.

 ??  ??
 ??  ??

Newspapers in English

Newspapers from Australia