This repository has been archived on 2025-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
rc2007-soccer/source/Concept/Framework/distance_sensor.cpp
2007-02-17 00:35:01 +00:00

26 lines
604 B
C++

#include "distance_sensor.h"
//-----------------------------------------------------------------------------
float Distance_Sensor::GetDistance()
{
uint32 result = 0;
//Generate pulse
*hardwareDDR |= pin;//Set pin output
*hardwarePort |= pin;//Activate port
usleep(10);//Wait for 10µs
*hardwarePort &= ~pin;//Deactivate port
*hardwareDDR &= ~pin;//Set pin input
//Wait for response
while(!(*hardwarePin & pin));
//Calculate duration of response
while(*hardwarePin & pin)
{
result++;
asm volatile("nop");
}
return (float(result) * DISTANCE_PER_VALUE);
}