1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 04:52:59 +01:00

SRF02 driver, new functionality.

This commit is contained in:
Zakaria Kasmi 2013-08-30 18:42:50 +02:00 committed by Oleg Hahm
parent 3f539ee104
commit 8e7b9cd7dd
2 changed files with 36 additions and 4 deletions

View File

@ -39,6 +39,7 @@
#define SRF02_FAKE_RANGING_MODE_CM 0x57
#define SRF02_FAKE_RANGING_MODE_MICRO_SEC 0x58
/* Define the used I2C Interface */
//#define SRF02_I2C_INTERFACE I2C0 // P0.27 SDA0, P0.28 SCL0
//#define SRF02_I2C_INTERFACE I2C1_0 // P0.0 SDA1, P0.1 SCL1
@ -62,6 +63,7 @@
bool srf02_init(uint8_t i2c_interface, uint32_t baud_rate);
/**
* @brief Get the distance measured from the SRF02 ultrasonic sensor.
* The result of a ranging can be returned in inches,

View File

@ -55,14 +55,44 @@ uint32_t srf02_get_distance(uint8_t ranging_mode)
uint8_t reg_size = 1;
uint8_t range_high_byte = 0;
uint8_t range_low_byte = 0;
uint32_t distance = 0;
uint8_t rx_buff[reg_size];
uint8_t tx_buff[reg_size];
tx_buff[0] = SRF02_REAL_RANGING_MODE_CM;
uint32_t distance = 0;
tx_buff[0] = ranging_mode;
status = i2c_write(SRF02_I2C_INTERFACE, SRF02_DEFAULT_ADDR,
SRF02_COMMAND_REG, tx_buff, reg_size);
if (!status) {
puts("Write the ranging command to the i2c-interface is failed");
distance = UINT32_MAX;
return distance;
}
hwtimer_wait(HWTIMER_TICKS(65000));
status = i2c_read(SRF02_I2C_INTERFACE, SRF02_DEFAULT_ADDR,
SRF02_RANGE_HIGH_BYTE, rx_buff, reg_size);
if (!status) {
puts("Read the distance from the i2c-interface is failed");
distance = UINT32_MAX;
return distance;
}
range_high_byte = rx_buff[0];
status = i2c_read(SRF02_I2C_INTERFACE, SRF02_DEFAULT_ADDR,
SRF02_RANGE_LOW_BYTE, rx_buff, reg_size);
range_low_byte = rx_buff[0];
distance = (range_high_byte << 8) | range_low_byte;
//printf("%u | %u\n", range_high_byte, range_low_byte);
return distance;
}
void srf02_start_ranging(uint16_t ranging_mode)
{
uint32_t distance = 0;
while (1) {
status = i2c_write(SRF02_I2C_INTERFACE, SRF02_DEFAULT_ADDR,
SRF02_COMMAND_REG, tx_buff, reg_size);
distance = srf02_get_distance(ranging_mode);