/*
Interfacing Ultra Sonic Module (4 PIN HCSR04) with Atmega16
Hardware Pin Configuration: Vcc, Gnd, Trigger input pulse, echo output pulse.
LCD Connected to port A.
Sensor to PORTD.
Sensor |
Trig --> PD0
Echo --> PD1
Vcc --> Vcc
Gnd --> Gnd
Programmer: Raj Prajapati
rajatmelavr@gmail.com
For any query
*/
//Header File
#include <avr/io.h>
#include <util/delay.h>
#include "ilcd.h"
//Macros
#define US_PORT PORTD
#define US_PIN PIND
#define US_DDR DDRD
#define US_TRIG PD0
#define US_ECHO PD1
//Globle Variable
int distance;
//Calculating width of high pulse in micro second.
#define US_Time_out -12
#define US_NO_OBSTACLE -13
void US_init();
void US_trig();
void US_init()
{
US_DDR|=(1<<US_TRIG);
}
void US_trig()
{
//Send a 10uS pulse on trigger line
US_PORT|=(1<<US_TRIG); //high
_delay_us(15); //wait 15uS
US_PORT&=~(1<<US_TRIG); //low
}
uint16_t pulse_width()
{
uint32_t i,result; // Value goes from 0 to 65536.
//Wait for the rising edge
for(i=0;i<600000;i++)
{
if(!(US_PIN & (1<<US_ECHO)))
continue; //Line is still low, so wait
else
break; //High edge detected, so break.
}
if(i==600000)
return US_Time_out; // No Reply found from US sensor, time lapsed
//High Edge Found
//Setup Timer1
TCCR1A=0X00;
TCCR1B=(1<<CS11); //Prescaler = Fcpu/8
TCNT1=0x00; //Again initialising the counter
//Counting the width of pulse.
//Now wait for the falling edge
for(i=0;i<600000;i++)
{
if(US_PIN & (1<<US_ECHO))
{
if(TCNT1 > 60000) break; else continue;
}
else
break;
}
if(i==600000)
return US_NO_OBSTACLE; //Indicates time out
//Falling edge found
result=TCNT1;
//Stop Timer
TCCR1B=0x00;
if(result > 60000)
return US_NO_OBSTACLE; //No obstacle
else
return (result>>1);
}
void main()
{
uint16_t r;
_delay_ms(100); //Let the LCD Module start
//Initialize the LCD Module
lcd_init();
//Set io port direction of sensor
US_init();
lcd_clrscr();
lcd_prints("Welcome ");
lcd_goto(1,1);
lcd_prints("Ultra Sonic Sensor");
_delay_ms(2500);
lcd_clrscr();
while(1)
{
//Send a trigger pulse
US_trig();
//Measure the width of pulse
r=pulse_width();
//Handle Errors
if(r==US_Time_out)
{
lcd_goto(1,1);
lcd_prints("Error !");
}
else if(r==US_NO_OBSTACLE)
{
lcd_goto(1,1);
lcd_prints("Clear !");
}
else
{
lcd_goto(1,1);
distance=((r*2)/58.0); //Convert to cm
lcd_printi(distance);
lcd_prints(" cm");
_delay_ms(200);
}
}
}
Interfacing Ultra Sonic Module (4 PIN HCSR04) with Atmega16
Hardware Pin Configuration: Vcc, Gnd, Trigger input pulse, echo output pulse.
LCD Connected to port A.
Sensor to PORTD.
Sensor |
Trig --> PD0
Echo --> PD1
Vcc --> Vcc
Gnd --> Gnd
Programmer: Raj Prajapati
rajatmelavr@gmail.com
For any query
*/
//Header File
#include <avr/io.h>
#include <util/delay.h>
#include "ilcd.h"
//Macros
#define US_PORT PORTD
#define US_PIN PIND
#define US_DDR DDRD
#define US_TRIG PD0
#define US_ECHO PD1
//Globle Variable
int distance;
//Calculating width of high pulse in micro second.
#define US_Time_out -12
#define US_NO_OBSTACLE -13
void US_init();
void US_trig();
void US_init()
{
US_DDR|=(1<<US_TRIG);
}
void US_trig()
{
//Send a 10uS pulse on trigger line
US_PORT|=(1<<US_TRIG); //high
_delay_us(15); //wait 15uS
US_PORT&=~(1<<US_TRIG); //low
}
uint16_t pulse_width()
{
uint32_t i,result; // Value goes from 0 to 65536.
//Wait for the rising edge
for(i=0;i<600000;i++)
{
if(!(US_PIN & (1<<US_ECHO)))
continue; //Line is still low, so wait
else
break; //High edge detected, so break.
}
if(i==600000)
return US_Time_out; // No Reply found from US sensor, time lapsed
//High Edge Found
//Setup Timer1
TCCR1A=0X00;
TCCR1B=(1<<CS11); //Prescaler = Fcpu/8
TCNT1=0x00; //Again initialising the counter
//Counting the width of pulse.
//Now wait for the falling edge
for(i=0;i<600000;i++)
{
if(US_PIN & (1<<US_ECHO))
{
if(TCNT1 > 60000) break; else continue;
}
else
break;
}
if(i==600000)
return US_NO_OBSTACLE; //Indicates time out
//Falling edge found
result=TCNT1;
//Stop Timer
TCCR1B=0x00;
if(result > 60000)
return US_NO_OBSTACLE; //No obstacle
else
return (result>>1);
}
void main()
{
uint16_t r;
_delay_ms(100); //Let the LCD Module start
//Initialize the LCD Module
lcd_init();
//Set io port direction of sensor
US_init();
lcd_clrscr();
lcd_prints("Welcome ");
lcd_goto(1,1);
lcd_prints("Ultra Sonic Sensor");
_delay_ms(2500);
lcd_clrscr();
while(1)
{
//Send a trigger pulse
US_trig();
//Measure the width of pulse
r=pulse_width();
//Handle Errors
if(r==US_Time_out)
{
lcd_goto(1,1);
lcd_prints("Error !");
}
else if(r==US_NO_OBSTACLE)
{
lcd_goto(1,1);
lcd_prints("Clear !");
}
else
{
lcd_goto(1,1);
distance=((r*2)/58.0); //Convert to cm
lcd_printi(distance);
lcd_prints(" cm");
_delay_ms(200);
}
}
}
where is the ilib.h file...????
ReplyDeletehey, can you give the access to your library "ildc.h" used in this programme? That would be great :)
ReplyDeletefrom where i would get ilcd.h header file
ReplyDelete