目的
I2Cのテスト
1 PWM
2 VDD
3 GND
4 シリアル出力
8 SWD
7 SWD
6 SDA
5 SCL
//I2C_Servo_V2_031_1
#include <Arduino.h>
#include <Wire.h>
#include <HardwareSerial.h>
#include <Servo.h>
Servo fs90r; // create servo object to control a servo
int val; // variable to read the value from the analog pin
int servopin = 16; // pin used to connect the servo
//初期化
void setup()
{
delay(3000); //not delete
//シリアルの初期化
Serial.setTx(PA2_ALT1);
Serial.setHalfDuplex();
Serial.begin(9600);
//i2cの初期化
Wire.setSDA(PA12);
Wire.setSCL(PA11);
Wire.begin(); //pa12 pa11
Wire.begin(0x40); //I2Cスレーブアドレスの設定
Wire.onReceive(receiveEvent); //データが来ると呼ばれる関数
fs90r.attach(servopin, 900, 2100); // attaches the servo on pin 9 to the servo object
// Be careful to min and max values...
} //setup
//メインループ
void loop(){
} //loop
//レシーブイベント
void receiveEvent(int howMany) {
int x = Wire.read(); //I2C受信データの読み込み
char c_hex[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
char buf[] = { '[',c_hex[(x >> 4) & 0x0f], c_hex[x & 0x0f] ,']', 0 };
//
// char buf[] = { x, 0 };
//I2Cスレーブの受信データの表示
Serial.print( buf );
//Serial.print( (char)x );
val = map(val, 0, 256, 0, 180); // scale it to use it with the servo (value between 0 and 180, at 90,
// motor is stopped)
fs90r.write(val); // sets the servo speed according to the scaled value
}//receiveEvent