Can you help my coding?because my coding successfully but the result I didn't get it

Thread Starter


Joined Jun 8, 2021
#define _XTAL_FREQ 20000000
#define RS RD2
#define EN RD3
#define D4 RD4
#define D5 RD5
#define D6 RD6
#define D7 RD7
#define Trigger RB1 //34 is Trigger
#define Echo RB2//35 is Echo
#include <xc.h>

#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

//LCD Functions Developed by Circuit Digest.
void Lcd_SetBit(char data_bit) //Based on the Hex value Set the Bits of the Data Lines
if(data_bit& 1)
D4 = 1;
D4 = 0;

if(data_bit& 2)
D5 = 1;
D5 = 0;

if(data_bit& 4)
D6 = 1;
D6 = 0;

if(data_bit& 8)
D7 = 1;
D7 = 0;

void Lcd_Cmd(char a)
RS = 0;          
Lcd_SetBit(a); //Incoming Hex value
EN  = 1;        
        EN  = 0;        

void Lcd_Clear()
Lcd_Cmd(0); //Clear the LCD
Lcd_Cmd(1); //Move the curser to first position

void Lcd_Set_Cursor(char a, char b)
char temp,z,y;
if(a== 1)
temp = 0x80 + b - 1; //80H is used to move the curser
z = temp>>4; //Lower 8-bits
y = temp & 0x0F; //Upper 8-bits
Lcd_Cmd(z); //Set Row
Lcd_Cmd(y); //Set Column
else if(a== 2)
temp = 0xC0 + b - 1;
z = temp>>4; //Lower 8-bits
y = temp & 0x0F; //Upper 8-bits
Lcd_Cmd(z); //Set Row
Lcd_Cmd(y); //Set Column

void Lcd_Start()
  for(int i=1065244; i<=0; i--)  NOP(); 
  Lcd_Cmd(0x02); //02H is used for Return home -> Clears the RAM and initializes the LCD
  Lcd_Cmd(0x02); //02H is used for Return home -> Clears the RAM and initializes the LCD
  Lcd_Cmd(0x08); //Select Row 1
  Lcd_Cmd(0x00); //Clear Row 1 Display
  Lcd_Cmd(0x0C); //Select Row 2
  Lcd_Cmd(0x00); //Clear Row 2 Display

void Lcd_Print_Char(char data)  //Send 8-bits through 4-bit mode
   char Lower_Nibble,Upper_Nibble;
   Lower_Nibble = data&0x0F;
   Upper_Nibble = data&0xF0;
   RS = 1;             // => RS = 1
   Lcd_SetBit(Upper_Nibble>>4);             //Send upper half by shifting by 4
   EN = 1;
   for(int i=2130483; i<=0; i--)  NOP();
   EN = 0;
   Lcd_SetBit(Lower_Nibble); //Send Lower half
   EN = 1;
   for(int i=2130483; i<=0; i--)  NOP();
   EN = 0;

void Lcd_Print_String(char *a)
int i;
  Lcd_Print_Char(a);  //Split the string using pointers and call the Char function 
/*****End of LCD Functions*****/

int time_taken;
int distance;
char t1,t2,t3,t4,t5;
char d1,d2,d3;

int main()
    TRISD = 0x00; //PORTD declared as output for interfacing LCD
    TRISB0 = 1;        //DEfine the RB0 pin as input to use as interrupt pin
    TRISB1 = 0; //Trigger pin of US sensor is sent as output pin
    TRISB2 = 1; //Echo pin of US sensor is set as input pin       
    TRISB3 = 0; //RB3 is output pin for LED

    Lcd_Print_String("Ultrasonic sensor");
    Lcd_Print_String("with PIC16F877A");
        TMR1H =0; TMR1L =0; //clear the timer bits
        Trigger = 1; 
        Trigger = 0;  
        while (Echo==0);
            TMR1ON = 1;
        while (Echo==1);
            TMR1ON = 0;
        time_taken = (TMR1L | (TMR1H<<8)); 
        distance= (0.0272*time_taken)/2;
        time_taken = time_taken * 0.8;
        t1 = (time_taken/1000)%10;
        t2 = (time_taken/1000)%10;
        t3 = (time_taken/100)%10;
        t4 = (time_taken/10)%10;
        t5 = (time_taken/1)%10;
        d1 = (distance/100)%10;
        d2 = (distance/10)%10;
        d3 = (distance/1)%10;
    return 0;
Mod edit: code tags, formatting. JohnInTX


Last edited by a moderator:


Joined Jun 29, 2019
If your coding is successful then you must get a result. If compiler does not give compile error does not mean your code logic is correct. I don't know what result you expecting from your program but i suggest make a flow diagram before starting writing the program.


Joined Jul 1, 2009
@Dannyel Questions come to mind:

  1. What LCD Display are you using? Maybe this code isn't for it.
  2. Did you get a data sheet for the display and see how you talk to it?
  3. Do you have a schematic?
  4. Can you provide photos?
  5. Have you flow-charted your logic to be sure it actually does something as you wish?

This will help you:

Title: Understanding Basic Electronics, 1st Ed.
Publisher: The American Radio Relay League
ISBN: 0-87259-398-3

Title: Standard C [Quick Ref]
Author(s): P.J.Plauger, Jim Brodie
ISBN: 1-55615-158-6

Title: C: A Reference Manual, 4th Ed.
Author: Samuel P. Harbison & Guy L. Steel, Jr.
ISBN: 0-13-326224-3

Title: Algorithms in C, 3rd Ed. [Parts 1-4, Fundamentals, Data Structures, Sorting, Searching]
Author: Robert Sedgewick
ISBN: 0-201-31452-5


Joined Jun 4, 2014
If you adjust the contrast control you should be able to get a row of rectangles to show up. If you can't then there is a problem with either the power or contrast pin (Vo) voltages.


Joined Sep 22, 2020
What PIC are you using?

You would generally write to the latch and read from the pins

You usually have to turn off analogue inputs, as they are on by default

The best way to debug this would be to have an oscilloscope and capture the outputs

As for the LCD, start by seeing if you see the screen on (as Albert said), and see if you can print a single char to the screen, and then by "hello world" - You should have done this before adding any other code, so that you could debug that in isolation


Joined Jun 29, 2019
First thing you can do is make sure that the MCU is running. Put somewhere into main loop a LED toggling routine and see it flashing. If it is not, then put this LED routine step-by-step toward program beginning until the LED is lit. If this does not help then revise all MCU connections and settings and try again.
Next (if you see the MCU is running) test LCD routines by putting some text to LCD. If all this are OK then you can investigate sensors routines. Isolate problems to smaller pieces by writing smaller test programs an you will succeed at your project ;)