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.

[SOLVED] ultrasonic reading error

Status
Not open for further replies.

arunbharathi.arasu

Full Member level 2
Joined
Feb 28, 2013
Messages
134
Helped
7
Reputation
14
Reaction score
6
Trophy points
1,298
Location
Chennai, Tamil Nadu, India
Activity points
2,151
Hi friends,

I tried ultrasonic coding. But I cannot receive data from ultrasonic.
I am using PIC18F4550 controller.

Here is code:
Code:
#include<stdio.h>
#include <string.h>
#include"uart.h"


#define _XTAL_FREQ 20000000
#define trigger RD2
#define echo RD3


void port_init()
{
   TRISA1=0;
   TRISD3=0XFF; //echo
   TRISD2=0X00; //trigger
   T1CON = 0x80; 
   TMR1IF = 0;
   TMR1=0; 
}
void delay(unsigned int de)
{
    unsigned int maxde,minde;
    for(maxde=0;maxde<de;maxde++)
        for(minde=0;minde<453;minde++);
}
void Delay10Us()
{
    int dCnt;
    for(dCnt=0;dCnt<3;dCnt++);
}
void trigger_US()
{
    trigger=1;
      __delay_us(10);
    trigger=0;
    __delay_us(10);
    while(echo == 0);
    TMR1=0;
    TMR1ON=1;
    while(echo == 1 && !TMR1IF);
    Time=TMR1;
    TMR1ON=0;
    Distance = ((float)Time/117.00); 
}




void main()
{
    port_init();
    port1_init();
    UART_init();


   while(1)
   {
       RA1=1;  //led on
       delay(2000);
       RA1=0;    // led off
       delay(2000);
       trans_string("\n test");   // test uart
       trigger_US();
       sprintf(Total_distance,"%.03f",Distance);
       trans_string(Total_distance);
   }
}

please tell me if any error / correction.
 

hello,

What compiler are you using ?

You can not affect a Byte to a bit ..

in MikroC syntaxe :

TRISA1_bit=0;
TRISD3_bit=1; //echo (inpout)
TRISD2_bit=0; //trigger (out put)


and use LATA instead of PORT for output

#define trigger LATD2_bit
#define echo PORTD3_bit
 
hi,

I am using Hi-tech C compiler.

#define trigger LATD2_bit
#define echo PORTD3_bit

So I have changed in to

#define trigger LATD2
#define echo RD3

Now it is working. thank you very much.

But why we have to mention LATD2, Is it consider as trigger?
 

But why we have to mention LATD2, Is it consider as trigger?

YOU defined this as trigger !

You didn't show us your schematic,
but i suppose that this output is the commande to start the ultrasonic Burst of a HC-SR04 module (trigger input)
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top