Create this program

Dependencies:   mbed HCSR04 HMC6352 PID TextLCD

Committer:
KoiShin_Sakana
Date:
Thu Aug 06 00:19:29 2015 +0000
Revision:
2:a8bbf677e774
Child:
3:901d18b901b4
create communication.h, control.h, infrared.h, main.cpp, pinDef.h and ultrasonic.h; update compass.h and motor.h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
KoiShin_Sakana 2:a8bbf677e774 1 /**
KoiShin_Sakana 2:a8bbf677e774 2 * @file : communication.h (1.0)
KoiShin_Sakana 2:a8bbf677e774 3 * @brief : manage communication
KoiShin_Sakana 2:a8bbf677e774 4 * @author : Shinnosuke KOIKE
KoiShin_Sakana 2:a8bbf677e774 5 * @date : 2015/08/06
KoiShin_Sakana 2:a8bbf677e774 6 */
KoiShin_Sakana 2:a8bbf677e774 7
KoiShin_Sakana 2:a8bbf677e774 8 #ifndef COMMUNICATION_H
KoiShin_Sakana 2:a8bbf677e774 9 #define COMMUNICATION_H
KoiShin_Sakana 2:a8bbf677e774 10
KoiShin_Sakana 2:a8bbf677e774 11 #include "mbed.h"
KoiShin_Sakana 2:a8bbf677e774 12
KoiShin_Sakana 2:a8bbf677e774 13 class CommuMaster {
KoiShin_Sakana 2:a8bbf677e774 14 public:
KoiShin_Sakana 2:a8bbf677e774 15 CommuMaster(PinName sda, PinName scl);
KoiShin_Sakana 2:a8bbf677e774 16 void receiveData(int correctFrequency, char addr, char data[], char length);
KoiShin_Sakana 2:a8bbf677e774 17
KoiShin_Sakana 2:a8bbf677e774 18 private:
KoiShin_Sakana 2:a8bbf677e774 19 I2C* i2cmaster;
KoiShin_Sakana 2:a8bbf677e774 20 };
KoiShin_Sakana 2:a8bbf677e774 21
KoiShin_Sakana 2:a8bbf677e774 22 class CommuSlave {
KoiShin_Sakana 2:a8bbf677e774 23 public:
KoiShin_Sakana 2:a8bbf677e774 24 CommuSlave(PinName sda, PinName scl);
KoiShin_Sakana 2:a8bbf677e774 25 void sendData(int correctFrequency, char addr, char data[], char length);
KoiShin_Sakana 2:a8bbf677e774 26
KoiShin_Sakana 2:a8bbf677e774 27 private:
KoiShin_Sakana 2:a8bbf677e774 28 I2CSlave* i2cslave;
KoiShin_Sakana 2:a8bbf677e774 29 };
KoiShin_Sakana 2:a8bbf677e774 30
KoiShin_Sakana 2:a8bbf677e774 31 // initialize
KoiShin_Sakana 2:a8bbf677e774 32 CommuMaster::CommuMaster(PinName sda, PinName scl) {
KoiShin_Sakana 2:a8bbf677e774 33 i2cmaster = new I2C(sda, scl);
KoiShin_Sakana 2:a8bbf677e774 34 }
KoiShin_Sakana 2:a8bbf677e774 35
KoiShin_Sakana 2:a8bbf677e774 36 // receive some data
KoiShin_Sakana 2:a8bbf677e774 37 void CommuMaster::receiveData(int correctFrequency, char addr, char data[], char length) {
KoiShin_Sakana 2:a8bbf677e774 38 i2cmaster->frequency(correctFrequency);
KoiShin_Sakana 2:a8bbf677e774 39 i2cmaster->read(addr | 1, data, length, false);
KoiShin_Sakana 2:a8bbf677e774 40 }
KoiShin_Sakana 2:a8bbf677e774 41
KoiShin_Sakana 2:a8bbf677e774 42 // initialize
KoiShin_Sakana 2:a8bbf677e774 43 CommuSlave::CommuSlave(PinName sda, PinName scl) {
KoiShin_Sakana 2:a8bbf677e774 44 i2cslave = new I2CSlave(sda, scl);
KoiShin_Sakana 2:a8bbf677e774 45 }
KoiShin_Sakana 2:a8bbf677e774 46
KoiShin_Sakana 2:a8bbf677e774 47 // send some data
KoiShin_Sakana 2:a8bbf677e774 48 void CommuSlave::sendData(int correctFrequency, char addr, char data[], char length) {
KoiShin_Sakana 2:a8bbf677e774 49 i2cslave->frequency(correctFrequency);
KoiShin_Sakana 2:a8bbf677e774 50 i2cslave->address(addr);
KoiShin_Sakana 2:a8bbf677e774 51 i2cslave->write(data, length);
KoiShin_Sakana 2:a8bbf677e774 52 }
KoiShin_Sakana 2:a8bbf677e774 53
KoiShin_Sakana 2:a8bbf677e774 54 #endif
KoiShin_Sakana 2:a8bbf677e774 55
KoiShin_Sakana 2:a8bbf677e774 56 /**
KoiShin_Sakana 2:a8bbf677e774 57 * example program(master)
KoiShin_Sakana 2:a8bbf677e774 58
KoiShin_Sakana 2:a8bbf677e774 59 #include "mbed.h"
KoiShin_Sakana 2:a8bbf677e774 60
KoiShin_Sakana 2:a8bbf677e774 61 #define DATA_NUM 8
KoiShin_Sakana 2:a8bbf677e774 62
KoiShin_Sakana 2:a8bbf677e774 63 int main(void) {
KoiShin_Sakana 2:a8bbf677e774 64 CommuMaster master(D5, D6);
KoiShin_Sakana 2:a8bbf677e774 65 char addr = 0xA0;
KoiShin_Sakana 2:a8bbf677e774 66 int freq = 2000;
KoiShin_Sakana 2:a8bbf677e774 67 char data[DATA_NUM];
KoiShin_Sakana 2:a8bbf677e774 68 char length = 8;
KoiShin_Sakana 2:a8bbf677e774 69
KoiShin_Sakana 2:a8bbf677e774 70 while (1) {
KoiShin_Sakana 2:a8bbf677e774 71 master.receiveData(freq, addr, data, length);
KoiShin_Sakana 2:a8bbf677e774 72 for (int i = 0; i < DATA_NUM; i++) {
KoiShin_Sakana 2:a8bbf677e774 73 pc.printf("%d ", data[i]);
KoiShin_Sakana 2:a8bbf677e774 74 }
KoiShin_Sakana 2:a8bbf677e774 75 pc.putchar('\n');
KoiShin_Sakana 2:a8bbf677e774 76 }
KoiShin_Sakana 2:a8bbf677e774 77 }
KoiShin_Sakana 2:a8bbf677e774 78
KoiShin_Sakana 2:a8bbf677e774 79 * example program(slave)
KoiShin_Sakana 2:a8bbf677e774 80
KoiShin_Sakana 2:a8bbf677e774 81 #include "mbed.h"
KoiShin_Sakana 2:a8bbf677e774 82
KoiShin_Sakana 2:a8bbf677e774 83 int main(void) {
KoiShin_Sakana 2:a8bbf677e774 84 CommuSlave slave(D5, D6);
KoiShin_Sakana 2:a8bbf677e774 85 char addr = 0xA0;
KoiShin_Sakana 2:a8bbf677e774 86 int freq = 2000;
KoiShin_Sakana 2:a8bbf677e774 87 char data[8] = {1, 1, 0, 0, 0, 0, 0, 1};
KoiShin_Sakana 2:a8bbf677e774 88 char length;
KoiShin_Sakana 2:a8bbf677e774 89
KoiShin_Sakana 2:a8bbf677e774 90 while (1) {
KoiShin_Sakana 2:a8bbf677e774 91 slave.sendData(freq, addr, data, length);
KoiShin_Sakana 2:a8bbf677e774 92 }
KoiShin_Sakana 2:a8bbf677e774 93 }
KoiShin_Sakana 2:a8bbf677e774 94 */