ruben91
Junior Member level 3
can i know why my sensor cannot detect more than 230cm? perhaps in data sheet it stated can detect more than 400 cm.. im using pic18f4580 with 20Mhz crystal frequency
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
#include <p18f4580.h>
#include <delays.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#pragma config OSC= HS
#pragma config WDT=OFF
#pragma config LVP=OFF
#define Trig PORTBbits.RB3
#define Echo PORTBbits.RB2
void T0Delay10us(void);
void T0Delay5ms(void);
void uart(void);
void Call_Init_PIC (void);
void sendPulse(void);
unsigned int distance ;
unsigned int time;
unsigned f,b;
void main(void)
{
TRISDbits.TRISD2=0;
TRISDbits.TRISD3=0;
TRISDbits.TRISD1=0;
TRISDbits.TRISD0=0;
Call_Init_PIC ();
uart();
while(1)
{
TMR1H= 0;
TMR1L= 0;
sendPulse();
while(Echo == 0);
T1CONbits.TMR1ON = 1;
while(Echo == 1);
T1CONbits.TMR1ON = 0;
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
time= TMR1H;
time= time<<8;
time= time|TMR1L;
//time = (TMR1L + (TMR1H<<8));
distance = time/28.8;
f=distance/10;
b=distance%10;
printf("\r\nDistance = %d.%d cm",f,b);
PIR1bits.TMR1IF=0;
Delay10KTCYx(255);
T0Delay5ms();
/*
if(distance>=0 && distance<=250)
{
PORTDbits.RD2=1;
//printf("\ndangerous");
}
if(distance>=251 && distance<=500)
{
PORTDbits.RD3=1;
}
if (distance>=501 && distance<=1000)
{
PORTDbits.RD0=1;
}
if(distance>=1001)
{
PORTDbits.RD1=1;
//printf("\nsafe zone");
}
*/
}
}
void Call_Init_PIC (void)
{
TRISBbits.TRISB3=0;
TRISBbits.TRISB2=1;
}
void uart(void)
{
TXSTA=0B00100000;
SPBRG=32;
TRISCbits.RC6=0;
RCSTAbits.SPEN=1;
}
void sendPulse(void)
{
Trig=1;
T0Delay10us();
Trig=0;
}
void T0Delay10us()
{
T0CON=0x08;
TMR0H=0XFF;
TMR0L=0XCE;
T0CONbits.TMR0ON=1;
while(INTCONbits.TMR0IF==0);
T0CONbits.TMR0ON=0;
INTCONbits.TMR0IF=0;
}
void T0Delay5ms()
{
T0CON=0x08;
TMR0H=0X9E;
TMR0L=0X58;
T0CONbits.TMR0ON=1;
while(INTCONbits.TMR0IF==0);
T0CONbits.TMR0ON=0;
INTCONbits.TMR0IF=0;
}
unsigned long Over;
Unsigned long time;
....
while(1)
{
sendPulse();
T1CONbits.TMR1ON = 0;
PIR1bits.TMR1IF=0;
TMR1H= 0;
TMR1L= 0;
Over=0;
time=0;
while(Echo == 0);
T1CONbits.TMR1ON = 1;
while(Echo == 1)
{
if (PIR1bits.TMR1IF==1)
{
Over=Over+65536;
PIR1bits.TMR1IF=0;
}
}
T1CONbits.TMR1ON = 0;
time= TMR1H;
time= time<<8;
time= time|TMR1L;
Distance= (time + Over)/58; // <- don't forget travel of sound in both way forward then reverse
// or you can use floating point calculus to get distance in mm distance/5.88
unsigned long Over;
Unsigned int time;
float Distance;
while(1)
{
sendPulse();
T1CONbits.TMR1ON = 0;
PIR1bits.TMR1IF=0;
TMR1H= 0;
TMR1L= 0;
Over=0;
time=0;
while(Echo == 0);
T1CONbits.TMR1ON = 1;
while(Echo == 1)
{
if (PIR1bits.TMR1IF==1)
{
Over=Over+65536;
PIR1bits.TMR1IF=0;
}
}
T1CONbits.TMR1ON = 0;
time= TMR1H;
time= time<<8;
time= time|TMR1L;
printf("\r\nTime = %d",time);
printf("\r\nOver = %ul",Over);
Distance=(float) time + (float) Over;
printf("\r\nTravel duration %4.1f µS",Distance);
Distance=Distance*0.2/5.88; // counts * 0,2µS / 5.88 => result in mm
printf("\r\nDistance %4.1f mm",Distance);