Das ist meine 3. Seite.
Analoges Lesen
#!/usr/bin/python
import spidev
import time
# Hubo MCP3208 Demo - simple reading of the MCP3208 analog input.
def Open_SPI_ADC ():
spiADC = spidev.SpiDev()
spiADC.open(0, 0)
spiADC.max_speed_hz = (50000)
return spiADC
def Get_AI_Channel (spiADC, channel):
if ((channel > 7) or (channel < 0)): print 'Illegal channel number (0 through 7)' return 0.0 result = spiADC.xfer2([6 + (channel >> 2), channel << 6, 0])
count = ((result[1] & 0x0F) << 8) + (result[2])
# Use a reference voltage of 2.53V.
voltage = (2.53 * count) / 4096
return voltage
# Read 10 values from analog input 0.
spiADC = Open_SPI_ADC()
for i in range(0, 9):
print Get_AI_Channel (spiADC, 0)
time.sleep(1)
spiADC.close();
Digitales Lesen und Schreiben
#!/usr/bin/python
import smbus
import time
# Hubo MCP23017 Demo - simple reading and writing of the MCP23017 digital IO.
IODIRA = 0x00
IODIRB = 0x01
GPPUB = 0x0D
GPIOA = 0x12
GPIOB = 0x13
global g_ioaddress
g_ioaddress = 0x20 # I2C address - Hubo master modules use 0x20
def Initialize_MCP23017 ():
i2cbus = smbus.SMBus(1) # Use 1 for the model B variants.
i2cbus.write_byte_data (g_ioaddress, IODIRA, 0x00) # Configure port A as output
i2cbus.write_byte_data (g_ioaddress, IODIRB, 0xFF) # Configure port B as input
i2cbus.write_byte_data (g_ioaddress, GPPUB, 0xFF) # Activate port B's internal pull up resistors
return i2cbus
def Get_DI_Channels (i2cbus):
return i2cbus.read_byte_data(g_ioaddress, GPIOB)
def Set_DI_Channels (i2cbus, value):
return i2cbus.write_byte_data(g_ioaddress, GPIOA, value)
# Read values from the digital input port and output the value on the digital output port.
i2cbus = Initialize_MCP23017()
while True:
value = Get_DI_Channels (i2cbus)
Set_DI_Channels(i2cbus, value)
print value
time.sleep(0.1)