rahuljin
Newbie level 6
- Joined
- Mar 10, 2008
- Messages
- 13
- Helped
- 2
- Reputation
- 4
- Reaction score
- 2
- Trophy points
- 1,283
- Activity points
- 1,372
ultrasonic distance circuit
hello,
i am using an atmega32 at 5v with 16mhz crystal. i have written the code for the ultrasonic distance measurement sensor. this is the code ---
In proteus, i get the 2 waves of 24.80 micro seconds using the virtual oscilloscope. this is the output ---
**broken link removed**
also i am planing to use the circuit given on this site ---
i want to make sure that the code and circuit is correct before making the actual physical circuit.
so, please help me and correct my problems ---
1. how can i check the output(i.e. frequency) of atmega32 in oscilloscope in lab ?
2. is the circuit good for making an ultrasonic sensor ?
3. is the code correct and output is correct ? if not, please suggest a solution.
i am using a normal pair of ultrasonic transducer, given on this page ---
also attaching proteus file.
btw, thanks
rahul
hello,
i am using an atmega32 at 5v with 16mhz crystal. i have written the code for the ultrasonic distance measurement sensor. this is the code ---
Code:
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#define lcd_port PORTD
#define LCD_EN 0x80
#define LCD_RS 0x20
unsigned int count, dist, temp, t; // Count the number of 40khz pulses, 1 pulse = 2 count
void command (char cmd)
{
DDRD = 0xFF;
lcd_port = ((cmd >> 4) & 0x0F)|LCD_EN;
lcd_port = ((cmd >> 4) & 0x0F);
_delay_ms(1);
lcd_port = (cmd & 0x0F)|LCD_EN;
lcd_port = (cmd & 0x0F);
_delay_ms(1);
}
void datawr (char dat)
{
lcd_port = (((dat >> 4) & 0x0F)|LCD_EN|LCD_RS);
lcd_port = (((dat >> 4) & 0x0F)|LCD_RS);
_delay_ms(1);
lcd_port = ((dat & 0x0F)|LCD_EN|LCD_RS);
lcd_port = ((dat & 0x0F)|LCD_RS);
_delay_ms(5);
}
void lcd_reset(void)
{
DDRD = 0xFF;
_delay_ms(1);
lcd_port = 0x03+LCD_EN;
lcd_port = 0x03;
_delay_ms(1);
lcd_port = 0x03+LCD_EN;
lcd_port = 0x03;
_delay_ms(1);
lcd_port = 0x03+LCD_EN;
lcd_port = 0x03;
_delay_ms(1);
lcd_port = 0x02+LCD_EN;
lcd_port = 0x02;
_delay_ms(1);
}
void lcd_init (void)
{
lcd_reset();
int j=0;
char b[]={0x28,0x0C,0x06,0x80,'\0'};
while(b[j]!='\0')
{
command(b[j]);
_delay_ms(1);
j++;
}
}
ISR(TIMER2_COMP_vect)
{
if((count < 4)) //send 2 pulse of 40khz
{
PORTB ^= (1<<0); //toggle the output at the PORTB.0
count++;
if(count == 2) //start the timer1 for time between transmission and receive after first pulse
{
TCNT1 = 0;
TCCR1B |= (1 << CS10);
}
}
//x++;
}
int main(void)
{
count = 0;
DDRD = 0xFF;
lcd_init();
unsigned char string[] = "Distance:";
for(int i=0; i<9; i++)
datawr(string[i]);
command(0x04); //To shift the cursor from right to left
command(0x8D); //To jump to 8D position
DDRB = 0x01; //Set PORTB.0 as output
PORTB |= (0<<0);
TCCR2 |= (1 << WGM21); // Configure timer 2 for CTC mode
TIMSK |= (1 << OCIE2); // Enable CTC interrupt
sei(); // Enable global interrupts
OCR2 = 196; // 16000000/40000 = 400, therefore current frequency is 80 khz for timer interrupt
TCCR2 |= (1 << CS20); // Start timer at Fcpu/1
for (;;)
{
if((PINB & 0x02) && (count >= 4)) // if pulse is received after sending 2 pulses
{
t = 4;
temp = TCNT1;
dist = (int)((double)((temp * 0.0000125 * 34000))/2); //calculate the distance in cm and speed of sound is 340 m/s
temp = dist;
while(t!=0) // print the distance in four digits from right to left on lcd
{
datawr((temp%10) + 0x30);
temp = temp/10;
t--;
}
count = 0;
}
if(TCNT1 > 20000) //if signal does not received, reset the count and send pulse again about 1.2ms
{
TCNT1 = 0;
count = 0;
}
}
}
In proteus, i get the 2 waves of 24.80 micro seconds using the virtual oscilloscope. this is the output ---
**broken link removed**
also i am planing to use the circuit given on this site ---
Code:
http://instruct1.cit.cornell.edu/courses/ee476/FinalProjects/s2007/jjl49_mar97/jjl49_mar97/index.htm
i want to make sure that the code and circuit is correct before making the actual physical circuit.
so, please help me and correct my problems ---
1. how can i check the output(i.e. frequency) of atmega32 in oscilloscope in lab ?
2. is the circuit good for making an ultrasonic sensor ?
3. is the code correct and output is correct ? if not, please suggest a solution.
i am using a normal pair of ultrasonic transducer, given on this page ---
Code:
http://robokits.co.in/shop/index.php?main_page=product_info&cPath=11&products_id=100
also attaching proteus file.
btw, thanks
rahul