Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

ultrasonic distance measuring sensor using atmega32

Status
Not open for further replies.

rahuljin

Newbie level 6
Newbie level 6
Joined
Mar 10, 2008
Messages
13
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,283
Visit site
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 ---

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
 

Hi Rahuljin..
I'm newby..
I will make the similar system as yours..
But Ican't found the ultrasonic sensor in Proteus software..
Can you help me? thanks

Can i ask for your email address please? Thank you
 

i´m sory, but i´m very newbiee

how can I simulate this system on Proteus ?
I put the code(.hex) in the atmega32 and after I run the projet. But doesn´t happen anything !
It doesn´t appears anything in the LCD.

how can I simulate several distances ?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top