test program of I2CSlave

Dependencies:   Ping mbed

Fork of CatPotI2CSlavetest by Fumiya Fujisawa

Committer:
lilac0112_1
Date:
Mon Dec 15 09:51:15 2014 +0000
Revision:
1:178963fb9f75
Parent:
0:dcc9cf1071f1
Confirmed performance

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lilac0112_1 1:178963fb9f75 1 /*
lilac0112_1 1:178963fb9f75 2 *動作確認済み
lilac0112_1 1:178963fb9f75 3 *超音波センサーのデータをマスターに送信
lilac0112_1 1:178963fb9f75 4 */
ryuna 0:dcc9cf1071f1 5 #include "mbed.h"
ryuna 0:dcc9cf1071f1 6 #include "Ping.h"
ryuna 0:dcc9cf1071f1 7
ryuna 0:dcc9cf1071f1 8 #define SYSAHBCLKDIV_Val 0x00000001 // Reset: 0x001
ryuna 0:dcc9cf1071f1 9 #define CLOCK_SETUP 1 // デフォルト=1
ryuna 0:dcc9cf1071f1 10 #define SYSPLLCTRL_Val 0x00000023 // デフォルト=23 Reset: 0x000
ryuna 0:dcc9cf1071f1 11 #define SYSPLLCLKSEL_Val 0x00000001 // デフォルト=0 Reset: 0x000
ryuna 0:dcc9cf1071f1 12 #define MAINCLKSEL_Val 0x00000003 // デフォルト=3 Reset: 0x000
ryuna 0:dcc9cf1071f1 13
ryuna 0:dcc9cf1071f1 14
ryuna 0:dcc9cf1071f1 15 #define ADDRESS 0xA0
ryuna 0:dcc9cf1071f1 16 #define DATA_NUM 5
ryuna 0:dcc9cf1071f1 17
ryuna 0:dcc9cf1071f1 18 DigitalOut myled(LED1);
ryuna 0:dcc9cf1071f1 19 I2CSlave Tiny(dp5, dp27);
ryuna 0:dcc9cf1071f1 20 Ping Sensor(dp1,dp2);
ryuna 0:dcc9cf1071f1 21 Serial pc(USBTX, USBRX);
ryuna 0:dcc9cf1071f1 22
ryuna 0:dcc9cf1071f1 23 int main() {
ryuna 0:dcc9cf1071f1 24
ryuna 0:dcc9cf1071f1 25 char Export[DATA_NUM]={0};
ryuna 0:dcc9cf1071f1 26 uint8_t cycle=0;
ryuna 0:dcc9cf1071f1 27
lilac0112_1 1:178963fb9f75 28 int i;
lilac0112_1 1:178963fb9f75 29
lilac0112_1 1:178963fb9f75 30 Tiny.address(ADDRESS);//アドレスの定義
ryuna 0:dcc9cf1071f1 31
ryuna 0:dcc9cf1071f1 32 while(1) {
ryuna 0:dcc9cf1071f1 33
ryuna 0:dcc9cf1071f1 34
ryuna 0:dcc9cf1071f1 35 Sensor.Send();
ryuna 0:dcc9cf1071f1 36 wait_ms(30);
lilac0112_1 1:178963fb9f75 37 Export[cycle%DATA_NUM] = Sensor.Read_cm();
lilac0112_1 1:178963fb9f75 38
lilac0112_1 1:178963fb9f75 39 cycle++;//配列移動
ryuna 0:dcc9cf1071f1 40
lilac0112_1 1:178963fb9f75 41 myled = (Tiny.receive() == I2CSlave::ReadAddressed);//成功すれば点滅
ryuna 0:dcc9cf1071f1 42
lilac0112_1 1:178963fb9f75 43 i = Tiny.receive();
lilac0112_1 1:178963fb9f75 44
lilac0112_1 1:178963fb9f75 45 switch(i){//変数を介するとうまく動作した.
ryuna 0:dcc9cf1071f1 46
ryuna 0:dcc9cf1071f1 47 case I2CSlave::ReadAddressed:
ryuna 0:dcc9cf1071f1 48 Tiny.write(Export, DATA_NUM); // Includes null char
ryuna 0:dcc9cf1071f1 49 break;
ryuna 0:dcc9cf1071f1 50 case I2CSlave::WriteGeneral:
ryuna 0:dcc9cf1071f1 51 break;
ryuna 0:dcc9cf1071f1 52 case I2CSlave::WriteAddressed:
ryuna 0:dcc9cf1071f1 53 break;
ryuna 0:dcc9cf1071f1 54 }
ryuna 0:dcc9cf1071f1 55
ryuna 0:dcc9cf1071f1 56 }
ryuna 0:dcc9cf1071f1 57 }