raji_adapa
Posts: 2
Joined: Sun Feb 10, 2019 10:12 pm

GPIO setup as an INPUT

Sun Feb 10, 2019 10:15 pm

can someone explain me this code?

Code: Select all

# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
def readadc(adcnum, clockpin, mosipin, misopin, cspin):
        if ((adcnum > 7) or (adcnum < 0)):
                return -1
        GPIO.output(cspin, True)
 
        GPIO.output(clockpin, False)  # start clock low
        GPIO.output(cspin, False)     # bring CS low
 
        commandout = adcnum
        commandout |= 0x18  # start bit + single-ended bit
        commandout <<= 3    # we only need to send 5 bits here
        for i in range(5):
                if (commandout & 0x80):
                        GPIO.output(mosipin, True)
                else:
                        GPIO.output(mosipin, False)
                commandout <<= 1
                GPIO.output(clockpin, True)
                GPIO.output(clockpin, False)
 
        adcout = 0
        # read in one empty bit, one null bit and 10 ADC bits
        for i in range(12):
                GPIO.output(clockpin, True)
                GPIO.output(clockpin, False)
                adcout <<= 1
                if (GPIO.input(misopin)):
                        adcout |= 0x1
 
        GPIO.output(cspin, True)
        
        adcout >>= 1       # first bit is 'null' so drop it
        return adcout
[moderator added code tags]

User avatar
Grumpy Mike
Posts: 909
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: GPIO setup as an INPUT

Tue Feb 12, 2019 6:58 pm

can someone explain me this code?
It reads data from an external MCP3008 analogue to digital converter which is attached to some of the GPIO pins.
If you want to look at the data sheet you will see the internal structure of this chip and how it needs to be accessed, this code does that.

The adcnum value passed to the readadc function determins which one of the 8 analogue inputs to this chip is read, these are numbered 0 to 7, any attempt to read a channel number outside this range simply returns a value of -1, a value impossible to get from the chip. So a calling program seeing a value of -1 knows there is something wrong.

The other values passed to the function "clockpin, mosipin, misopin, cspin" define the GPIO pins attached to signals on the chip. They are
clockpin - connected to the serial clock pin of the MCP3008
mosipin - connected to the Master out / serial in pin of the MCP3008
misopin - connected to the Master in / serial out of the MCP3008
cspin - connected to the chip select pin of the MCP3008
Again see the MCP3008 data sheet for these pins.

raji_adapa
Posts: 2
Joined: Sun Feb 10, 2019 10:12 pm

Re: GPIO setup as an INPUT

Thu Feb 14, 2019 2:46 am

thanks for response.

what about rest of code below?

Code: Select all

        commandout |= 0x18  # start bit + single-ended bit
        commandout <<= 3    # we only need to send 5 bits here
        for i in range(5):
                if (commandout & 0x80):
                        GPIO.output(mosipin, True)
                else:
                        GPIO.output(mosipin, False)
                commandout <<= 1
                GPIO.output(clockpin, True)
                GPIO.output(clockpin, False)

        adcout = 0
        # read in one empty bit, one null bit and 10 ADC bits
        for i in range(12):
                GPIO.output(clockpin, True)
                GPIO.output(clockpin, False)
                adcout <<= 1
                if (GPIO.input(misopin)):
                        adcout |= 0x1

        GPIO.output(cspin, True)
        
        adcout >>= 1       # first bit is 'null' so drop it
        return adcout
[moderator added your code tags, next time do it yourself please...]

Return to “Python”