123123123123123123123123123

Dependencies:   mbed

Committer:
terryLAI
Date:
Thu Jul 03 10:59:37 2014 +0000
Revision:
2:442902ec3aa1
Parent:
1:cbec1283a16a
Child:
3:4306d042af6f
aa

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TonyYI 0:3417ca0a36c0 1 #include "compass.h"
TonyYI 0:3417ca0a36c0 2
terryLAI 1:cbec1283a16a 3 COMPASS::COMPASS(PinName tx, PinName rx)
TonyYI 0:3417ca0a36c0 4 {
terryLAI 1:cbec1283a16a 5 this->serial= new MySerial(tx,rx);
terryLAI 1:cbec1283a16a 6 serial->baud(56000);
terryLAI 1:cbec1283a16a 7 serial->format(8,SerialBase::None, 1);
terryLAI 1:cbec1283a16a 8 init();
TonyYI 0:3417ca0a36c0 9 }
TonyYI 0:3417ca0a36c0 10
TonyYI 0:3417ca0a36c0 11
terryLAI 2:442902ec3aa1 12 uint16_t COMPASS::read()
TonyYI 0:3417ca0a36c0 13 {
terryLAI 1:cbec1283a16a 14 resume();
terryLAI 1:cbec1283a16a 15 buffer[0]=serial->getc();
terryLAI 1:cbec1283a16a 16 buffer[1]=serial->getc();
terryLAI 1:cbec1283a16a 17 stop();
terryLAI 1:cbec1283a16a 18 degree=(uint8_t)(buffer[0])*256+(uint8_t)(buffer[1]);
terryLAI 1:cbec1283a16a 19 return degree;
TonyYI 0:3417ca0a36c0 20 }
TonyYI 0:3417ca0a36c0 21
terryLAI 1:cbec1283a16a 22 void COMPASS::init()
TonyYI 0:3417ca0a36c0 23 {
terryLAI 1:cbec1283a16a 24 run();
terryLAI 1:cbec1283a16a 25 stop();
TonyYI 0:3417ca0a36c0 26 }
TonyYI 0:3417ca0a36c0 27
terryLAI 1:cbec1283a16a 28 void COMPASS::run()
TonyYI 0:3417ca0a36c0 29 {
terryLAI 1:cbec1283a16a 30 write2Bytes(RUN_MSB,RUN_LSB);
TonyYI 0:3417ca0a36c0 31 }
TonyYI 0:3417ca0a36c0 32
TonyYI 0:3417ca0a36c0 33
terryLAI 1:cbec1283a16a 34 void COMPASS::stop()
TonyYI 0:3417ca0a36c0 35 {
terryLAI 1:cbec1283a16a 36 write2Bytes(STOP_MSB,STOP_LSB);
terryLAI 1:cbec1283a16a 37 }
TonyYI 0:3417ca0a36c0 38
TonyYI 0:3417ca0a36c0 39
terryLAI 1:cbec1283a16a 40 void COMPASS::resume()
TonyYI 0:3417ca0a36c0 41 {
terryLAI 1:cbec1283a16a 42 write2Bytes(RESUME_MSB,RESUME_LSB);
terryLAI 1:cbec1283a16a 43 }
terryLAI 1:cbec1283a16a 44
TonyYI 0:3417ca0a36c0 45
terryLAI 1:cbec1283a16a 46 void COMPASS::reset()
terryLAI 1:cbec1283a16a 47 {
terryLAI 1:cbec1283a16a 48 write2Bytes(RST_MSB,RST_LSB);
terryLAI 1:cbec1283a16a 49 }
terryLAI 1:cbec1283a16a 50
terryLAI 1:cbec1283a16a 51 void COMPASS::write2Bytes(char msb, char lsb)
terryLAI 1:cbec1283a16a 52 {
terryLAI 1:cbec1283a16a 53 serial->putc(msb);
terryLAI 1:cbec1283a16a 54 serial->putc(lsb);
terryLAI 1:cbec1283a16a 55
terryLAI 1:cbec1283a16a 56 }
terryLAI 1:cbec1283a16a 57
TonyYI 0:3417ca0a36c0 58