SRF02 sensor
Fork of SRF02 by
SRF02.cpp@2:97ec40cfbf98, 2016-05-04 (annotated)
- Committer:
- Reniboy
- Date:
- Wed May 04 13:26:55 2016 +0000
- Revision:
- 2:97ec40cfbf98
- Parent:
- 0:b729d2ce03bd
commit 1 ;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eencae | 0:b729d2ce03bd | 1 | /** |
eencae | 0:b729d2ce03bd | 2 | @file SRF02.cpp |
Reniboy | 2:97ec40cfbf98 | 3 | |
eencae | 0:b729d2ce03bd | 4 | @brief Member functions implementations |
Reniboy | 2:97ec40cfbf98 | 5 | |
eencae | 0:b729d2ce03bd | 6 | */ |
eencae | 0:b729d2ce03bd | 7 | #include "mbed.h" |
eencae | 0:b729d2ce03bd | 8 | #include "SRF02.h" |
Reniboy | 2:97ec40cfbf98 | 9 | |
eencae | 0:b729d2ce03bd | 10 | SRF02::SRF02(PinName sdaPin, PinName sclPin) |
eencae | 0:b729d2ce03bd | 11 | { |
eencae | 0:b729d2ce03bd | 12 | i2c = new I2C(sdaPin,sclPin); // create new I2C instance and initialise |
eencae | 0:b729d2ce03bd | 13 | i2c->frequency(400000); // I2C Fast Mode - 400kHz |
eencae | 0:b729d2ce03bd | 14 | leds = new BusOut(LED4,LED3,LED2,LED1); |
Reniboy | 2:97ec40cfbf98 | 15 | |
eencae | 0:b729d2ce03bd | 16 | } |
Reniboy | 2:97ec40cfbf98 | 17 | |
eencae | 0:b729d2ce03bd | 18 | int SRF02::getDistanceCm() |
eencae | 0:b729d2ce03bd | 19 | { |
eencae | 0:b729d2ce03bd | 20 | char data[2]; |
eencae | 0:b729d2ce03bd | 21 | |
eencae | 0:b729d2ce03bd | 22 | // need to send CM command to command register |
eencae | 0:b729d2ce03bd | 23 | data[0] = CMD_REG; |
eencae | 0:b729d2ce03bd | 24 | data[1] = CM_CMD; |
eencae | 0:b729d2ce03bd | 25 | int ack = i2c->write(SRF02_W_ADD,data,2); |
eencae | 0:b729d2ce03bd | 26 | if (ack) |
eencae | 0:b729d2ce03bd | 27 | error(); // if we don't receive acknowledgement, flash error message |
eencae | 0:b729d2ce03bd | 28 | |
eencae | 0:b729d2ce03bd | 29 | // this will start the sensor ranging, the datasheet suggests a delay of at least 65 ms before reading the result |
eencae | 0:b729d2ce03bd | 30 | wait_ms(70); |
eencae | 0:b729d2ce03bd | 31 | |
eencae | 0:b729d2ce03bd | 32 | // we can now read the result - tell the sensor we want the high byte |
eencae | 0:b729d2ce03bd | 33 | char reg = RANGE_H_REG; |
eencae | 0:b729d2ce03bd | 34 | ack = i2c->write(SRF02_W_ADD,®,1); |
eencae | 0:b729d2ce03bd | 35 | if (ack) |
eencae | 0:b729d2ce03bd | 36 | error(); // if we don't receive acknowledgement, flash error message |
eencae | 0:b729d2ce03bd | 37 | |
eencae | 0:b729d2ce03bd | 38 | // if we read two bytes, the register is automatically incremented (H and L) |
eencae | 0:b729d2ce03bd | 39 | ack = i2c->read(SRF02_R_ADD,data,2); |
eencae | 0:b729d2ce03bd | 40 | if (ack) |
eencae | 0:b729d2ce03bd | 41 | error(); // if we don't receive acknowledgement, flash error message |
Reniboy | 2:97ec40cfbf98 | 42 | |
eencae | 0:b729d2ce03bd | 43 | // high byte is first, then low byte, so combine into 16-bit value |
eencae | 0:b729d2ce03bd | 44 | return (data[0] << 8) | data[1]; |
eencae | 0:b729d2ce03bd | 45 | } |
Reniboy | 2:97ec40cfbf98 | 46 | |
eencae | 0:b729d2ce03bd | 47 | void SRF02::error() |
eencae | 0:b729d2ce03bd | 48 | { |
eencae | 0:b729d2ce03bd | 49 | while(1) { |
eencae | 0:b729d2ce03bd | 50 | leds->write(15); |
eencae | 0:b729d2ce03bd | 51 | wait(0.1); |
eencae | 0:b729d2ce03bd | 52 | leds->write(0); |
eencae | 0:b729d2ce03bd | 53 | wait(0.1); |
eencae | 0:b729d2ce03bd | 54 | } |
eencae | 0:b729d2ce03bd | 55 | } |