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.

microcontroller to Ultrasonic

Status
Not open for further replies.

emperror123

Member level 5
Member level 5
Joined
Dec 11, 2010
Messages
86
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Visit site
Activity points
1,880
hi all
currently i have problem with the code below

Code:
#define XTAL_FREQ 20MHZ

#include <pic.h>
#include <htc.h>
#include "delay.h"
#include "lcd.h"
#include <stdio.h>
#include <stdlib.h>

__CONFIG(0x3f3A); 

void pic_init();
void getRange();

#define trigg RC3
#define echoo RC2
//#define PIR RC1			//INDICATE RA0 as "PIR"


//global variable
unsigned char time;
float width;
float distance = 0;
char s[32];

void main()
{
    pic_init();
    lcd_init();

    while (1)
	{
        getRange();
		while(distance > 15 && distance < 25)
		{
			RB7=1;
			DelayMs(1000);  //1 second for hand to move to IR sensor
//			while(1)
//			{
				getRange(); 
				if(distance > 0 && distance < 10)  //enter
				{
					//PORTA=0x00;
					RA1=1;
					DelayMs(900);
					break;
				}
				while(distance > 25 && distance < 400)  //hand out of operating range
				{	
					if(RD2 == 1)    // up
					{
					//	PORTA=0x00;
						RA2=1;
						DelayMs(900);
						break;
					}
					if(RD3 == 1)    // left
					{
					//	PORTA=0x00;
						RA3=1;
						DelayMs(900);
						break;
					}
					if(RD0 == 1)   // down
					{
					//	PORTA=0x00;
						RC0=1;
						DelayMs(900);
						break;
					}
					if(RD1 == 1)  // right
					{
					//	PORTA=0x00;					
						RA5=1;
						DelayMs(900);
						break;
					}
					else
					{
						PORTA = 0X00;
						PORTC = 0x00;
						break;
					}
				}
			}
			break;
		}	
    }
//}

void pic_init()
{
    TRISA = 0;
	PORTA = 0;

    TRISB = 0;
    PORTB = 0;

	TRISC = 0b00110100;
	PORTC = 0;

	TRISD = 0xff;
    PORTD = 0;

	TRISE = 0x00;
    PORTE = 0;

	CMCON=0x07;
	ADCON1=0;
}

void getRange()
{
    time = 0;
    TMR1H = 0;
    TMR1L = 0;
    trigg = 1;    		// Trigger to receive pulse.
    DelayUs(20); 		// Delay 20us to ensure sensorfully initiate.
    trigg = 0;    		// Turn off trigger.

    while(!echoo){} 	// continue

    T1CON=0b011001; 	// timer1 on (prescale 1:8), pg59

    while(echoo){} 		// continue

    T1CON = 0;        // timer1 off

    width = (( TMR1H << 8 ) | TMR1L );
	width = 0xFFFF - (0xFFFF - width);
	width = width * (0.0000004);
	distance = (width * 1000000)/ 58;

//    sprintf( s , "%f cm" , distance);
//	lcd_clear();
//    lcd_puts(s);
//	DelayMs ( 250 );
//	DelayMs ( 250 );
}

from the above code, i found that my Ultrasonic having problem where sometimes i get 0cm from LCD, but when i have detect certain range, it become 0cm after that, what shud i done inorder to solve the problem

thank
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top