今天早上怒过了(参见1602帖子),晚上又准备怒——cb开发者们干嘛去了,i2c驱动也没人试一下
那我就来试一下吧
参见http://blog.oscarliang.net/raspberry-pi-arduino-connected-i2c/,这是树梅派的。
我爱python!!!因为这个东西rpi和cb都能跑,而且差不多
我已经安装好python了,所以安装包:- apt-get install python-smbus i2c-tools -y
复制代码 然后参见http://linux-sunxi.org/Cubieboard/ExpansionPorts和http://linux-sunxi.org/A10/PIO发现只引出了PB18 19,<!--[注释,这句别看]cb设计者干什么去了,才弄96Pin,干嘛不把每个脚都引出来……-->
好吧,开始,我用的是arduino mega,引脚配置:
20 -> SDA
21-> SCL
如果是UNO:
A4 -> SDA
A5 -> SCL
我是这么接的:
Arduino Mega USB -> CB USB
Arduino Pin-No.20 -> CB PB19
Arduino Pin-No.21 -> CB PB20
然后扫描一下:不出问题的话:- 0 1 2 3 4 5 6 7 8 9 a b c d e f
- 00: -- 04 -- -- -- -- -- -- -- -- -- -- --
- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
- 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
- 70: -- -- -- -- -- -- -- --
复制代码 Arduino代码:- #include <Wire.h>
- #define SLAVE_ADDRESS 0x04
- int number = 0;
- int state = 0;
- void setup() {
- pinMode(13, OUTPUT);
- Serial.begin(9600); // start serial for output
- // initialize i2c as slave
- Wire.begin(SLAVE_ADDRESS);
- // define callbacks for i2c communication
- Wire.onReceive(receiveData);
- Wire.onRequest(sendData);
- Serial.println("Ready!");
- }
- void loop() {
- delay(100);
- }
- // callback for received data
- void receiveData(int byteCount){
- while(Wire.available()) {
- number = Wire.read();
- Serial.print("data received: ");
- Serial.println(number);
- if (number == 1){
- if (state == 0){
- digitalWrite(13, HIGH); // set the LED on
- state = 1;
- }
- else{
- digitalWrite(13, LOW); // set the LED off
- state = 0;
- }
- }
- }
- }
- // callback for sending data
- void sendData(){
- Wire.write(number);
- }
复制代码 Python代码:
【我就直接抄过来了,不改了】- import smbus
- import time
- # for RPI version 1, use "bus = smbus.SMBus(0)"
- bus = smbus.SMBus(1)
- # This is the address we setup in the Arduino Program
- address = 0x04
- def writeNumber(value):
- bus.write_byte(address, value)
- # bus.write_byte_data(address, 0, value)
- return -1
- def readNumber():
- number = bus.read_byte(address)
- # number = bus.read_byte_data(address, 1)
- return number
- while True:
- var = input("Enter 1 - 9: ")
- if not var:
- continue
- writeNumber(var)
- print "RPI: Hi Arduino, I sent you ", var
- # sleep one second
- time.sleep(1)
- number = readNumber()
- print "Arduino: Hey RPI, I received a digit ", number
- print
复制代码 然后运行:- root@cubieboard:~# python i2c.py
- Enter 1 - 9: 1
- RPI: Hi Arduino, I sent you 1
- Arduino: Hey RPI, I received a digit 1
- Enter 1 - 9: 8
- RPI: Hi Arduino, I sent you 8
- Arduino: Hey RPI, I received a digit 8
- Enter 1 - 9: 9
- RPI: Hi Arduino, I sent you 9
- Arduino: Hey RPI, I received a digit 9
- Enter 1 - 9: 6
- RPI: Hi Arduino, I sent you 6
- Arduino: Hey RPI, I received a digit 6
- Enter 1 - 9: 1
- RPI: Hi Arduino, I sent you 1
- Arduino: Hey RPI, I received a digit 1
- Enter 1 - 9: 1
- RPI: Hi Arduino, I sent you 1
- Arduino: Hey RPI, I received a digit 1
复制代码 输入1的话可以切换板载LED灯的状态
|