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.

[PIC] PIC18F4550 with Ultrasonic Sensor HC-SR04.

Status
Not open for further replies.

SAMO6

Newbie level 5
Joined
Nov 14, 2016
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
139
Hello,

I am interfacing ultrasonic sensor with pic18f4550 and want to display my output(sensed value from ultrasonic sensor) on lcd(16x2).
I am using timer1 and crystal oscillator frequency of 20Mhz.
My code is as below:
Code:
#include<PIC18F4550.h>

#define _XTAL_FREQ 2000000
#include<xc.h>

#define RS PORTCbits.RC0
#define En PORTCbits.RC1

void delay()
{
	unsigned int q;
	for(q=0;q<10000;q++);
}
// LCD Functions
void lcdcmd(unsigned char value)
{
    PORTB=value;
    RS=0;			//Select command register
    En=1;			// High to Low pulse on Enable pin of LCD
    delay();
    En=0;
}
void lcddata(unsigned char value)
{
    PORTB=value;
    RS=1;			//Select data register
    En=1;			// High to Low pulse on Enable pin of LCD
    delay();
    En=0;

}


void lcdinit()
{
     lcdcmd(0x38);	//Call Command subroutine (value=0x38)
    delay();
    lcdcmd(0x0E);
    delay();
    lcdcmd(0x01);
    delay();
   // lcdcmd(0x06);
   // delay();

}

void stringlcd(const char * s)
{
            while(*s)
            lcddata(*s++);
}
void lcd_setcursor(int row,int col)
{
    if(row==0){
        lcdcmd(0x80 | col);
            }
    else if(row==1){
        lcdcmd(0xc0| col);
    }
}

void main()
{
   
    TRISAbits.RA0=0;       // Trig pin of Ultrasonic
    TRISAbits.RA1=1;        // ECHO pin of Ultrasonic
    TRISB=0x00;             // Data bus of lcd
    TRISCbits.RC0=0;        //RS pin of LCD
    TRISCbits.RC1=0;        // EN pin of LCD
    int a;
      
    lcdinit();
    lcdcmd(0x80);
    stringlcd("Water Level");
    /*  Timer Code        */
    T1CON=0x10;
    while(1){
        TMR1H=0;
        TMR1L=0;
      PORTAbits.RA0=1;
      __delay_us(10);
         PORTAbits.RA0=0;
        while(PORTAbits.RA1==0);
        TMR1ON=1;
        while(PORTAbits.RA1==1);
        TMR1ON=0;
      a = (TMR1L | (TMR1H<<8));   //Reads Timer Value
    a = a/147.05;                //Converts Time to Distance
    a = a + 1;
    //Distance Calibration
    if(a>=2 && a<=400)          //Check whether the result is valid or not
    {
        lcdcmd(0x01);
         lcd_setcursor(1,14);
      lcddata(a%10 + 48);

      a = a/10;
      lcd_setcursor(1,13);
      lcddata(a%10 + 48);

      a = a/10;
      lcd_setcursor(1,12);
      lcddata(a%10 + 48);

      lcd_setcursor(1,15);
      lcddata("cm");
    
    }
    else
    {
        lcdcmd(0x01);
        lcd_setcursor(1,1);
      stringlcd("Out of Range");
    }
    __delay_us(40);
  }

}

I tried hard, but no sensed value is getting displayed on lcd. only 'Water level' is getting displayed.
Can you tell me where my code is wrong?
I have done calculations as per the speed,time,distance relation and timer values.


If you have working ultrasonic(HC-SR04) library in proteus then please give me.
I hope you have understood problem.Plz, help.
 

By the look of it you are using the XC8 compiler: if so then don't include the processor-specific file - the 'xc.h' will pick up the correct file from the IDE.
You delay function is probably your problem. There are a number of problems with using the technique you are using with the first being that the whole thing can be optimised away by the compiler if you are not careful.
Also you are very dependent on the actual oscillator speed (and therefore the time required for each instruction) but I cannot see where you are setting the oscillator. Please do not say that you have the _XTAL_FREQ #define there as that means nothing except to tell the internal _delay() macros what you have set the clock to elsewhere. I hope you are setting the oscillator up in the config settings.
The delays for the required for the LCD initialisation are longer than that between each character but you are using the same function. Either you are wasting time between each character or you are not allowing enough time during the initialisation phase.
The most accurate way to have a delay is to use a timer that is either blocking or interrupt driven. The next best way is to use the <delays.h> include and then the _delay_ms() (etc.) macros accepting that these are always blocking. However the __delay_xx macros rely on the _XTAL_FREQ definition being correct (see above)
Susan
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top