Skip to content

Commit

Permalink
SRF02 driver, new functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
zkasmi authored and OlegHahm committed Sep 26, 2013
1 parent 3f539ee commit 8e7b9cd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
2 changes: 2 additions & 0 deletions drivers/include/srf02-ultrasonic-sensor.h
Expand Up @@ -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
Expand All @@ -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,
Expand Down
38 changes: 34 additions & 4 deletions drivers/srf02/srf02-ultrasonic-sensor.c
Expand Up @@ -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);

Expand Down

0 comments on commit 8e7b9cd

Please sign in to comment.