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 Measurement Using HC-SR04 Sensor, PIC18F4580 and C18 Compiler

Status
Not open for further replies.

ruben91

Junior Member level 3
Joined
Nov 17, 2014
Messages
29
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
209
hello, i have convert the code to c18 mplab but it don't seem to produce the result, can u help me to solve this? i'm using pic18f4580 with xtal 20Mhz
Code:
#include <p18f4580.h>
#include <usart.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 Call_Init_USART (void);
void sendPulse(void);

unsigned int distance;


void main(void)
{
Call_Init_PIC ();
uart();
while(1)
{

sendPulse();
while(Echo == 1);
TMR1H= 0;
TMR1L= 0;
T1CON = 0x01;
while(Echo == 0);
distance = (TMR1H<<8) + TMR1L;
T1CON = 0x00;                            
distance = (distance/58)*20;
//distance = (distance/58);
printf("Distance = %3d\v\n",distance); 
Delay10KTCYx(255); 
PIR1bits.TMR1IF=0;
T0Delay5ms(); 
}
}

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;
	}
 

Re: Ultrasonic Distance Measurement using HC-SR04 sensor and PIC16f887

Use C18 USART library and write code to send cm data to USART.
 

Re: Ultrasonic Distance Measurement using HC-SR04 sensor and PIC16f887

i have tried putrsUSART to send to hyper terminal..but can cant get any output in the terminal.
 

Re: Ultrasonic Distance Measurement using HC-SR04 sensor and PIC16f887

Zip and post your latest complete mplab C18 project files. Did you initialize USART with OpenUSART() function ?
 

Re: Ultrasonic Distance Measurement using HC-SR04 sensor and PIC16f887

yes i did sir, and this is my complete c18 project files
 

Attachments

  • simple usoniC.zip
    45.5 KB · Views: 105

Attachments

  • hcsr04 uss.txt
    2.8 KB · Views: 93

Re: Ultrasonic Distance Measurement using HC-SR04 sensor and PIC16f887

Hello sir, i have update my coding wit openusart, but i stil cant gt the output, i gt the output in terminal like this
ò+V÷oünð+f÷oÿ~ò#V÷ünð+f÷ÿ~ò+V÷ünð3f÷ÿ~ò+V÷oþnð3f÷ÿ~ò+V÷oþnò3f÷ÿvò+V÷oþnò3V÷ÿvð+f÷o
i have attach my updated coe..hope u can find my mistakes sir..thank you

- - - Updated - - -

uss.png
hello sir, i have tried the given code,this is the output i get in terminal :( :-(
 

Attachments

  • simple usoniC.zip
    47.8 KB · Views: 64

Re: Ultrasonic Distance Measurement using HC-SR04 sensor and PIC16f887

Don't use putrsUSART(), instead use putsUSART().

I am attaching two versions of code. One is MPLAB C18 and another mikroC. The mikroC version is working in Proteus and also hardware and giving somewhat correct values but C18 version is not giving correct values. Don't know why.

- - - Updated - - -

I have written a HC SR04 code which uses Timer1 and INTx pin and it is giving very accurate readings. I will post the code soon. I have tested it both in Proteus and also hardware with PIC16F877A.
 

Attachments

  • simple usoniC working.rar
    61 KB · Views: 121
  • HC SR04.rar
    127.4 KB · Views: 92

hello sir..i have tried both, HC sr04 does not produce output at all..and the simple usonic produces output as the image below, the bad part is, i tried to plug the ground connection off the sensor,yet it still printing result in terminal, meaning the data is not from the sensor.
 

Attachments

  • uss.png
    uss.png
    65.9 KB · Views: 89

HC-SR04 ultrasonic sensor problem.

Why my ultrasonic sensor HC-SR04 sensor cannot detect length more that 230 cm, it can detect up to 230 cm only but in the datasheet. it is stated it can detect up to 400 cm, im using pi18f4580 with crystal 20Mhz. thank you.
 

hello,
don't forget to use LATBbits.LATb3 for trig define
if you are using Pic18F.
Measure max less than 4M ?
Was is the power supply ?
i also tested HC-SR04 and get less than 3M. as maximum.
i think is a problem of power signal on sender part..
needs a hardware change to boost the signal

Materual comming from RPC are low cost and also low quality.All modules can differe in performances values..
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top