而Arduino Yun有不一樣的功能展示. 所以就想說 兩種板子 來透過I2C bus 來做連結!
Mega 2560 作為 I2C的 Master 因為之前已經有一些 I2C的功能寫在這裡!
//---------I2C Address------------------------------------------
#define DS90UB953TRHBTQ1_ADDRESS 0x18
#define LIS3MDLTD_ADDRESS 0x1E
#define CAT24C02_ADDRESS 0x50
#define LSM6DSM_ADDRESS 0x6A
#define SI7034_ADDRESS 0x70
#define BMP280_ADDRESS 0x76
#define TCA_ADDRESS 0x71
#define BMP180_ADDRESS 0x77
#define Gyro3Axis_ADDRESS 0x68
#define Gyr_address 0x68
#define SLAVE_ADDRESS 0x01
而就把 Yun 當作Slave來用 位址為 0x01.[注意 ATmega32u4 used in Yun has I2C: on 2 (SDA) and 3 (SCL)]
#include
Wire.begin(); // begin 沒帶參數就為master
//-----Arduino i2c slave
void vI2C_readTask1()
{
Wire.requestFrom(SLAVE_ADDRESS, 6);
Wire.beginTransmission(SLAVE_ADDRESS);
int i = 0;
if(Wire.available())
{
//Serial.print("Data returned: ");
//Serial.print((char) Wire.read());
while(Wire.available())
{
//Serial.print((char) Wire.read());
Uart.line[i++] = Wire.read();
}
//Serial.println();
}
Wire.endTransmission();
Uart.line[ i ] = '\0'; // Terminate string
processCommand(Uart.line);
//Serial.println(Uart.line);
}
以上為接收到 slave 送回的指令處理, 目前為控制servo馬達的轉動角度
Slave端設定如下:
#define SLAVE_ADDRESS 0x01
Wire.begin(SLAVE_ADDRESS); // join I2C bus as a slave with address 1
Wire.onReceive(receiveEvent); // register event
Wire.onRequest(requestEvent); // register event
void requestEvent()
{
if(flag.I2C_Flag==1)
{
//Serial.println( Uart.line );
Wire.write(Uart.line,6);
}
}
//-----servo1 angle---------
if(strstr(data, "S1_") != NULL)
{
for(int i=3;i
Uart.ctemp[i-3]=data[i];
}
Uart.ctemp[ len-3 ] = '\0';
tempDIO=atoi(Uart.ctemp);
Serial.println(tempDIO);
flag.I2C_Flag=1;
}
將UART收到的 "S1_100"的格式拆解 馬達servo1 轉 100度 再轉發給 master 去控制 master的servo1 轉動100度!
將Arduino 的code 編輯完後! 就可以到 c#上的上機介面去做開發!!