1回答

0收藏

pcDuino – I2C Test

其他 其他 5395 人阅读 | 1 人回复 | 2013-03-15

本帖最后由 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:
  1. sudo apt-get install i2c-tools
  2. 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.
  1. #! /usr/bin/env python
  2. #######################################################################
  3. # Simple I2C parallel port demostration Python class
  4. # Originally developed for the pcDuino platform
  5. #
  6. # This class file may be included in other application to provide
  7. # simple read and write support for i2c attached 8574 i2c parallel
  8. # port.
  9. #
  10. # The connection will default to I2C Bus 2 - the default external I2C
  11. # bus on the pcDuino. The default parallel port address is 0x20 - the
  12. # first standard address for the 8574.
  13. #
  14. # If this file is executed, it will run a pattern demo output on the
  15. # parallel port. Best viewed by having LEDs connected to the outputs.
  16. #
  17. # Enjoy!
  18. # Bill
  19. #######################################################################
  20. # Copyright (c) 2013 by William Greathouse, Brecksville, OH, USA
  21. #
  22. # Permission is granted to use the source code within this
  23. # file in whole or in part for any use, personal or commercial,
  24. # without restriction or limitation.
  25. #
  26. # No warranties, either explicit or implied, are made as to the
  27. # suitability of this code for any purpose. Use at your own risk.
  28. #######################################################################

  29. import sys
  30. import smbus
  31. import time

  32. I2CBUS=2
  33. PARADR=0x20
  34. DELAY=0.05

  35. class I2Cparallel:
  36.   # initialize the i2c connection to the parallel port
  37.   def __init__(self, adr=PARADR, bus=I2CBUS):
  38.     self.bus=bus
  39.     self.adr=adr
  40.     self.i2c=smbus.SMBus()
  41.     self.i2c.open(self.bus)
  42.     # Set all outputs to high state (state required for input)
  43.     # If this fails, chip is not responding on i2c bus
  44.     try:
  45.       self.i2c.write_byte(self.adr, 0xff)
  46.     except:
  47.       print "Unable to access parallel port on bus %d, addresss 0x%02x" % (self.bus, self.adr)
  48.       sys.exit(1)

  49.   # output value to parallel port
  50.   def out_byte(self,value):
  51.     self.i2c.write_byte(self.adr, value)

  52.   # input value from parallel port
  53.   def in_byte(self):
  54.     return self.i2c.read_byte(self.adr)

  55. if __name__=="__main__":
  56.   # initialize the interface, passing just the address as an example
  57.   parport=I2Cparallel(PARADR)

  58.   # flash all output lines
  59.   for _ in range(10):
  60.     parport.out_byte(0x00)
  61.     time.sleep(DELAY)
  62.     parport.out_byte(0xff)
  63.     time.sleep(DELAY)

  64.   # Do a scanning display, single bit on
  65.   for _ in range(10):
  66.     for i in range(8):
  67.       parport.out_byte(1<<i) time.sleep(delay)="" for="" i="" in="" range(8):="" parport.out_byte(0x80="">>i)
  68.       time.sleep(DELAY)

  69.   # Do a scanning display, single bit off
  70.   for _ in range(10):
  71.     for i in range(8):
  72.       parport.out_byte(~(1<<i)) time.sleep(delay)="" for="" i="" in="" range(8):="" parport.out_byte(~(0x80="">>i))
  73.       time.sleep(DELAY)

  74.   # Set all outputs to high state (state required for input)
  75.   parport.out_byte(0xff)
复制代码
分享到:
回复

使用道具 举报

回答|共 1 个

倒序浏览

沙发

aaa_3002076

发表于 2013-3-19 13:38:27 | 只看该作者

沙发。。。还是那句话,多看教学视频,屌丝终成高富帅。哈
您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条