Sign In

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

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 have wrote this program and this program work but it shows wrong distance and I don't know what's problem?!
it shows 5.8cm when distances are more than 260cm but it shows good when distances are less than 260cm.
please help me to do it.

Images

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

Program


#include <mega16.h>
#include <delay.h>
#include <stdlib.h>
#asm
 .equ __lcd_port=0x1b
#endasm
#include <lcd.h>

int timer;
float d = 0.0;
char str[20];
 
interrupt [TIM0_OVF] void timer0_ovf_isr(void){
TCNT0=0;
timer++;
}
 void main(void)
{
//echo=PINC.1 , triger=PINC.0
 PORTC=0x00;
 DDRC=0x01;
 
 TCCR0=0x02;// Clock value: 8 MHz--Mode: Normal---Clock source: System Clock 
 TCNT0=0; 
 TIMSK=0x01;
 
 lcd_init(16);
 #asm("sei") // Global enable interrupts
 
 while (1)
 {
 PORTC.0=1;
delay_us(20);
PORTC.0=0;
 
 while(PINC.1==0){};
 timer=0;TCNT0=0;
TCCR0=0x02;
 while(PINC.1 == 1){};
 TCCR0=0x00;
 
 if(timer*256.0+TCNT0>30000.0)
{
lcd_clear();
lcd_gotoxy(0,0);
lcd_puts("out of range");
}
 else
 {
 d=0;
d=(timer*256.0+TCNT0)/58; 
 lcd_clear();
 lcd_gotoxy(0,0);
lcd_puts("dis=");
 lcd_gotoxy(5,0);
ftoa(d,2,str);
lcd_puts(str);
 lcd_gotoxy(13,0);
lcd_puts("cm");
 delay_ms(200);
 }
 }
}

Replies

R Avinash Gupta
2016-07-21 07:44:35
In line number 10, try changing

int

to

unsigned long int

or uint32_t
R elnaz**
2016-07-21 13:58:45
thanks for reply and I changed int to unsigned long int but my problem is n't solven.
#include <mega16.h>
#include <delay.h>
#include <stdlib.h>
#asm
 .equ __lcd_port=0x1b
#endasm
#include <lcd.h>

unsigned long int timer;
float d = 0.0;
char str[20];
 
interrupt [TIM0_OVF] void timer0_ovf_isr(void){
TCNT0=0;
timer++;
}
 void main(void)
{
//echo=PINC.1 , triger=PINC.0
 PORTC=0x00;
 DDRC=0x01;
 
 TCCR0=0x02;// Clock value: 8 MHz--Mode: Normal---Clock source: System Clock 
 TCNT0=0; 
 TIMSK=0x01;
 
 lcd_init(16);
 #asm("sei") // Global enable interrupts
 
 while (1)
 {
 PORTC.0=1;
delay_us(20);
PORTC.0=0;
 
 while(PINC.1==0){};
 timer=0;TCNT0=0;
TCCR0=0x02;
 while(PINC.1 == 1){};
 TCCR0=0x00;
 
 if(timer*256.0+TCNT0>30000.0)
{
lcd_clear();
lcd_gotoxy(0,0);
lcd_puts("out of range");
}
 else
 {
 d=0;
d=(timer*256.0+TCNT0)/58; 
 lcd_clear();
 lcd_gotoxy(0,0);
lcd_puts("dis=");
 lcd_gotoxy(5,0);
ftoa(d,2,str);
lcd_puts(str);
 lcd_gotoxy(13,0);
lcd_puts("cm");
 delay_ms(200);
 }
 }
}

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.