TA的每日心情 | 奋斗 2020-9-28 10:10 |
---|
签到天数: 1018 天 连续签到: 1 天 [LV.10]以坛为家III
|
本帖最后由 xinxincaijq 于 2013-3-15 13:35 编辑
Today I connected up a my prototype board for an I2C experiment with the pcDuino. Blinky Lights!
Video of simple I2C parallel port test using Python.
http://player.youku.com/player.php/sid/XNTI3MDk1MTU2/v.swf
Using I2C
If you want to try interfacing with the pcDuino I2C yourself, you will need to install the software for using the I2C bus:- sudo apt-get install i2c-tools
- sudo apt-get install python-smbus
复制代码 i2c-tools provides command line applications for checking and accessing i2c bus devices.
i2cdetect – scans an I2C bus for devices
i2cget – reads registers visible through the I2C bus
i2cset – writes registers visible through the I2C bus
python-smbus provides Python access to I2C (SMBus) devices.
Connecting PCF8574
Connect a PCF8574 I2C parallel port chip to the pcDuino I2C bus. For feedback, connect each of the outputs through resistors to LEDs. Be sure to connect power to 3.3 Vdc and GND. I connected the address select pins to ground (address 0×20). I can post a detailed wiring diagram if anyone is interested.
To verify your connections, use i2cdetect from the command line. It should show a single device at address 0×20 on bus 2 (the I2C bus available on the GPIO connectors).
i2cdetect -y 2
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Python
Python source code for testing the I2C bus will cycle the LEDs as shown in the video.- #! /usr/bin/env python
- #######################################################################
- # Simple I2C parallel port demostration Python class
- # Originally developed for the pcDuino platform
- #
- # This class file may be included in other application to provide
- # simple read and write support for i2c attached 8574 i2c parallel
- # port.
- #
- # The connection will default to I2C Bus 2 - the default external I2C
- # bus on the pcDuino. The default parallel port address is 0x20 - the
- # first standard address for the 8574.
- #
- # If this file is executed, it will run a pattern demo output on the
- # parallel port. Best viewed by having LEDs connected to the outputs.
- #
- # Enjoy!
- # Bill
- #######################################################################
- # Copyright (c) 2013 by William Greathouse, Brecksville, OH, USA
- #
- # Permission is granted to use the source code within this
- # file in whole or in part for any use, personal or commercial,
- # without restriction or limitation.
- #
- # No warranties, either explicit or implied, are made as to the
- # suitability of this code for any purpose. Use at your own risk.
- #######################################################################
-
- import sys
- import smbus
- import time
-
- I2CBUS=2
- PARADR=0x20
- DELAY=0.05
-
- class I2Cparallel:
- # initialize the i2c connection to the parallel port
- def __init__(self, adr=PARADR, bus=I2CBUS):
- self.bus=bus
- self.adr=adr
- self.i2c=smbus.SMBus()
- self.i2c.open(self.bus)
- # Set all outputs to high state (state required for input)
- # If this fails, chip is not responding on i2c bus
- try:
- self.i2c.write_byte(self.adr, 0xff)
- except:
- print "Unable to access parallel port on bus %d, addresss 0x%02x" % (self.bus, self.adr)
- sys.exit(1)
-
- # output value to parallel port
- def out_byte(self,value):
- self.i2c.write_byte(self.adr, value)
-
- # input value from parallel port
- def in_byte(self):
- return self.i2c.read_byte(self.adr)
-
- if __name__=="__main__":
- # initialize the interface, passing just the address as an example
- parport=I2Cparallel(PARADR)
-
- # flash all output lines
- for _ in range(10):
- parport.out_byte(0x00)
- time.sleep(DELAY)
- parport.out_byte(0xff)
- time.sleep(DELAY)
-
- # Do a scanning display, single bit on
- for _ in range(10):
- for i in range(8):
- parport.out_byte(1<<i) time.sleep(delay)="" for="" i="" in="" range(8):="" parport.out_byte(0x80="">>i)
- time.sleep(DELAY)
-
- # Do a scanning display, single bit off
- for _ in range(10):
- for i in range(8):
- parport.out_byte(~(1<<i)) time.sleep(delay)="" for="" i="" in="" range(8):="" parport.out_byte(~(0x80="">>i))
- time.sleep(DELAY)
-
- # Set all outputs to high state (state required for input)
- parport.out_byte(0xff)
复制代码 |
|