[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
Visit site
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
 
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…