techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: Binary Counter without using shift register such as 74HC

Sun Mar 16, 2014 11:35 am

Better code would setup the pins as outputs ONCE in main (or a function called from main.

Also not use a complicated

Code: Select all

light((m * j) / m)
line just call the light function then use BINARY AND function to scan over the value and mask which bit to determine action, moving mask with >> or << operations.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: Binary Counter without using shift register such as 74HC

Sun Mar 16, 2014 2:08 pm

Untested but should work code to try using binary operations which is less overhead than a modulus operation (%) simple ands and shifts are easier to do.

Also only calls one function, whilst also only setting up GPIO as outputs ONCE

Code: Select all

#!/usr/bin/env python3
#Date: 12th, March 2014
# modified and untested version only sets up pins as output once and
# simplified binary operations for lower overhead
# modified 16th March 2014 Paul Carpenter - PC Services

import RPi.GPIO as gpio
import time

gpio.setmode(gpio.BCM)
gpio.setwarnings(False)

# order of outputs first is LSbit of counter
iopins = [14, 15, 18, 17, 27, 22, 10, 9, 11]

# time delay between light changes
tdelay = 0.125

# Maximum count up to value 
count = 511

def light( val ):
    maxk = 1
    for pin in iopins:
        if( val & mask ):
            gpio.output( pin, gpio.HIGH )
        else:
            gpio.output( pin, gpio.LOW )
        mask <<= 1
    return

if __name__=='__main__':
    # Set all pins as output
    for pin in iopins:
        gpio.setup( pin, gpio.OUT )

    # actual counting
    for j in range ( 0, count + 1 ):
        light( j )
        time.sleep( tdelay )           

    # end tidy up
    gpio.cleanup( )
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

Mahdi
Posts: 13
Joined: Tue Mar 17, 2015 9:36 pm

Re: Binary Counter without using shift register such as 74HC

Wed Apr 22, 2015 7:58 pm

hey man :)
why do you define 9 GPIOs while u are using only 8 ?

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: Binary Counter without using shift register such as 74HC

Wed Apr 22, 2015 9:27 pm

Mahdi wrote:hey man :)
why do you define 9 GPIOs while u are using only 8 ?
Because the original was 9 bit code counting from 0 to 511, that is 512 steps which is 9 bits, that was the original contributors choice to use 9 bits

Using the SAME python construct of

for pin in iopins:

Actually means it always scans the array of 9 GPIO pin values, yes ALL 9
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: Binary Counter without using shift register such as 74HC

Thu Apr 23, 2015 5:25 pm

supra wrote:Thanks!
You could have learned how to read codes. I was only reducing codes instead of big codes. I had to do tips and tricks or mastering. :D :D That why i can saved file sizes.
If I could understand what you are saying I could give a comment, as it seems gibberish

Especially as these comments are a YEAR later
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

Return to “Python”