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/modules/input/distance_sensor.c

33 lines
838 B
C
Raw Normal View History

#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<31>s
2007-02-18 00:14:00 +00:00
//*hardwarePort &= ~pin;//Deactivate port
*hardwareDDR &= ~pin;//Set pin input
2007-02-18 00:14:00 +00:00
(parent->GetModule<Display>(IO_DISPLAY_MAIN))->Print("pre 1", 4, 1);
//Wait for response
2007-02-18 00:14:00 +00:00
while(!(PINC & pin)){asm volatile("nop");}
(parent->GetModule<Display>(IO_DISPLAY_MAIN))->Print("pre 2", 4, 1);
//Calculate duration of response
while(*hardwarePin & pin)
{
result++;
asm volatile("nop");
}
2007-02-18 00:14:00 +00:00
(parent->GetModule<Display>(IO_DISPLAY_MAIN))->Print("pre 3", 4, 1);
return (float(result) * DISTANCE_PER_VALUE);
}