Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Obstacle_Avoiding_Robot wireless_project_BLE
Fork of HCSR04 by
hcsr04.cpp
- Committer:
- prabhuvd
- Date:
- 2013-03-30
- Revision:
- 5:e70a24a88131
- Parent:
- 4:33938a97d904
- Child:
- 6:4be4fcd0c171
File content as of revision 5:e70a24a88131:
/* Copyright (c) 2013 Prabhu Desai
* pdtechworld@gmail.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "hcsr04.h"
DistMeasure::DistMeasure(PinName TrigPin,PinName EchoPin):
trigger(TrigPin), echo(EchoPin)
{
pulsetime.stop();
pulsetime.reset();
echo.rise(this,&DistMeasure::isr_rise);
echo.fall(this,&DistMeasure::isr_fall);
trigger=0;
}
DistMeasure::~DistMeasure()
{
}
void DistMeasure::isr_rise(void)
{
pulsetime.start();
}
void DistMeasure::start(void)
{
trigger=1;
wait_us(10);
trigger=0;
}
void DistMeasure::isr_fall(void)
{
pulsetime.stop();
pulsedur = pulsetime.read_us();
distance= (pulsedur*343)/20000;
pulsetime.reset();
}
void DistMeasure::rise (void (*fptr)(void))
{
echo.rise(fptr);
}
void DistMeasure::fall (void (*fptr)(void))
{
echo.fall(fptr);
}
unsigned int DistMeasure::get_dist_cm()
{
return distance;
}
unsigned int DistMeasure::get_pulse_us()
{
return pulsedur;
}
/*******************************************************
Here is a sample code usage
*********************************************************
#include "hcsr04.h"
DistMeasure usensor(p25,p6);
int main()
{
unsigned char count=0;
while(1) {
usensor.start();
wait_ms(500);
dist=usensor.get_dist_cm();
lcd.cls();
lcd.locate(0,0);
lcd.printf("cm:%ld",dist );
count++;
lcd.locate(0,1);
lcd.printf("Distance =%d",count);
}
*/
