Hello I'm trying to measure the distance with HC-SRO4 ultrasound sensor PIC18F45k50 micro-controller however, I'm not reading any value on the LCD screen. Here is the following code that I have written so far for the PIC


#define RS LATD5   // PORT 0 pin is used for Register Select
#define EN LATD6   // PORT 1 pin is used for Enable
#define ldata LATB   // PORT is used for transmitting data to LCD
#define LCD_Dir1 TRISB
#define LCD_Dir2 TRISD

void LCD_Init();

void LCD_Command(char );

void LCD_Char(char x);

void LCD_String(const char *);

void MSdelay(unsigned int );

void LCD_String_xy(char ,char ,const char*);

void Trigger_Pulse_10us();

void LCD_Init(){

    LCD_Dir1 = 0x00;     // Set PORTB as output PORT for LCD data(B0-B7) pins
    LCD_Dir2 = 0x00;     // Set PORTD as output PORT LCD Control(RS,EN) Pins

    LCD_Command(0x38);    // uses 2 line and initialize 5*7 matrix of LCD
    LCD_Command(0x01);    // clear display screen
    LCD_Command(0x0C);    // display on cursor off
    LCD_Command(0x06);    // increment cursor (shift cursor to right)


void LCD_Clear(){

    LCD_Command(0x01);     // clear display screen

void LCD_Command(char cmd ){

    ldata= cmd;            // Send data to PORT as a command for LCD   
    RS = 0;                // Command Register is selected
    EN = 1;                // High-to-Low pulse on Enable pin to latch data*/ 
    EN = 0;

void LCD_Char(char dat){

    ldata= dat;            // Send data to LCD  
    RS = 1;                // Data Register is selected
    EN=1;                  // High-to-Low pulse on Enable pin to latch data   

void LCD_String(const char *msg){



void LCD_String_xy(char row,char pos,const char *msg){

     char location = 0;

        location=(0x80) | ((pos) & 0x0f); // Print message on 1st row and desired location


        location=(0xC0) | ((pos) & 0x0f); // Print message on 2nd row and desired location


void main(){

    float Distance;
    int Time;
    float Total_distance[10];

    OSCCON=0x72;        // Use internal oscillator frequency of 8 MHz

    INTCON2bits.RBPU = 0; // Enable PORTB Pull-ups
    TRISCbits.TRISC6 = 1; //Set Echo Pin as an input
    TRISCbits.TRISC5 = 0; //Trigger Pin as output
    PORTCbits.RC5 = 0;

    T1CON = 0x06; // Enable 16-bit TMR1 Register,No prescale, internal clock, Timer OFF   
    TMR1IF = 0; // Make Timer1 Overflow Flag to '0'
    TMR1 = 0; // Load Timer1 with 0



        Trigger_Pulse_10us(); // Transmit 10us pulse to HC-SR04 

        while(PORTCbits.RC6 == 0); // Wait for rising edge at Echo pin
        TMR1 = 0; // Load Timer1 register with 0
        TMR1ON = 1;  // Turn ON Timer1

        while(PORTCbits.RC6 ==1 && !TMR1IF); // Wait for falling edge
        Time = TMR1; // Copy Time when echo is received 
        TMR1ON = 0; // Turn OFF Timer1

        Distance = ((float)Time/117.00); // Distance =(velocity x Time)/2 
        sprintf(Total_distance,"%.03f", Distance); //Converting var distance into char and saving it in Total_distance buffer

        LCD_String_xy(1,1, Total_distance);
        LCD_String("  cm");


void Trigger_Pulse_10us(){  // Function to send 10uS pulse

    PORTCbits.RC5 = 1;
    PORTCbits.RC5 = 0;


void MSdelay(unsigned int val){

     unsigned int i,j;
            for(j=0;j<165;j++); //This count Provide delay of 1 ms for 8MHz Frequency

Can anyone please point out where I'm making errors. I have uploaded a picture showing what currently is being displayed

enter image description here

  • \$\begingroup\$ You're sprintf-ing into Total_distance, but you've declared Total_distance as an array of floats. It should be an array or chars. You should also use snprintf for safety to ensure you don't write past the end of the array for any reason. \$\endgroup\$ – brhans Apr 27 at 2:53
  • \$\begingroup\$ And it's been a while since I last used a character LCD, but I believe the command to write to line 2 looks more like location = 0x80 | (40 + (pos & 0x0f));. Note that's a 40, not a 0x40, because line 2 starts at the 40th byte in the LCD's DDRAM. \$\endgroup\$ – brhans Apr 27 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.