Sign In

Trouble In Measurement Of Ultrasonic(HY-SRF05)range Finder

elnaz** mm139421@yahoo.com Iran, Islamic Republic of

Hardware: Atmega16 Runing At 8MHz With Frequency Divider 8 For Timer And HY-SRF05 Sensor And Lcd2*16 Software: C Project With Codevision 2.05 Compiler

I changed your codes from atmel studio to codevision but I have errors in 3 line :1) r=GetPulseWidth();
2)if(r==b) 3) if(r==a)

Images

trouble in measurement of ultrasonic(HY-SRF05)range finder

Program

#include <mega16.h>
#include <delay.h>
#include <alcd.h>
#include <stdio.h>
#include <stdlib.h>

#define US_NO_OBSTACLE
#define    US_ERROR    
char x[20];
float r;
void HCSR05Trigger()
{   
    PORTD.0=1;    //high
    delay_us(15);                //wait 15uS
    PORTD.0=0;    //low
}
unsigned int GetPulseWidth()
{
unsigned long int  i,result;

for(i=0;i<600000;i++)
    {
        if(PIND.1==0) 
        continue;    //Line is still low, so wait
        else 
        break;        //High edge detected, so break.
    }

    if(i==600000)
        return US_NO_OBSTACLE;

 TCCR1A=0X00;
 TCCR1B=(1<<CS11);    //Prescaler = Fcpu/8
 TCNT1=0x00;
 
for(i=0;i<600000;i++)
    {
        if(PIND.1==1)
        {
            if(TCNT1 > 60000) break; else continue;
        }
        else
            break;
    }

    if(i==600000)
        return US_ERROR;    //Indicates time out
    result=TCNT1;

    //Stop Timer
    TCCR1B=0x00;

    if(result > 60000)
    return US_ERROR;    //No obstacle
    else
    return (result>>1);
}
void main()
{
typedef unsigned int r;
    
delay_ms(100);
lcd_init(16);
DDRD=0x01;
lcd_clear();
lcd_putsf("Ultra Sonic");
lcd_gotoxy(0,1);
lcd_putsf("Sensor Test");
delay_ms(2500);
lcd_clear();

    while(1)
    {
        
        //Send a trigger pulse
        HCSR05Trigger();

        //Measure the width of pulse
        r=GetPulseWidth();

        //Handle Errors
        if(r==US_NO_OBSTACLE)
        {    
        lcd_gotoxy(0,0);
        lcd_putsf("Error !");
        }
        else
         if(r==US_ERROR)
        {      
        lcd_gotoxy(0,0);
        lcd_putsf("Clear !");
        }
        else
        {
        
            float d;

            d=r/58;    //Convert to cm

           lcd_gotoxy(5,0);ftoa(d,1,x);lcd_puts(x);
           lcd_gotoxy(13,0);lcd_puts("cm");

           delay_ms(500);
        }
    }

}

Replies

R Avinash
2016-07-24 05:44:02
on line 60 remove typedef

Post a reply to elnaz**

Think you can help elnaz** ? Then post your thoughts that might help elnaz**. You will earn a lot of reputation in the technical community.

If your program is in multiple files, then ZIP the entire folder and attach below

Images and Screenshots

These helps other better understand your suggestion.