Displaying Scrolling(Moving) text on 16×2 lcd Using Pic16f877 and Pic18f452 Microcontroller

Displaying moving or scrolling text on lcd(16×2,8×1,16×4,8×2,16×1,20×1,20×2 etc) is very easy using any microcontroller. You just need to know how to efficiently use 16×2 lcd commands. In this project i am using pic 16f877 microcontroller to display text and then scroll it on the 16×2 lcd. The lcd which i am using is 16×2. Where 16 represents number of coulombs and 2 represents number of rows. Text which i am scrolling on the lcd is my website name “microcontroller-project.com“. Code for pic microcontroller is written in c language using mplab-ide with high tech c compiler.

Scrolling/Moving text on lcd with pic microcontroller – Project requirements

  • Pic 16f877 microcontroller
  • 16×2 lcd (I am using.)
  • Potentiometer/variable resistor (To set lcd contrast)
  • crystal(20 MHz)
  • Power supply

If you are newbie and don’t know much about 16×2 lcd working and pin out just go through this simple tutorial. It will explain you about all the pins and functions of 16×2 lcd. 
16×2 lcd pinout and working.
 

Pic microcontroller scrolling text – Project circuit

The circuit of the project is given below. Data pins of 16×2 lcd are directly connected to Port-b of pic microcontroller. The rd(read-write) pin of lcd is connected to port-d pin no 5. The rs(register select) pin of 16×2 lcd is connected to port-d pin 6. The en(enable)pin of 16×2 lcd is connected to port-d pin 7 of pic 16f877 microcontroller. 20 MHz crystal is connected to pin 13 and 14 in parallel to two 33 pf capacitors. Apply 5 volts to pin 1,11&32. Ground pin 12 and 31. Set lcd contrast by rotating potentiometer/variable resistor. 
 
Pic microcontroller scrolling text – Project circuit

Pic microcontroller moving text on lcd – Project code

The code of pic microcontroller scrolling lcd display is written in c language and is easy to understand and modify. First i included the necessary header file #include<htc.h> in code. This header file is necessary to be included if you are using high tech c compiler. Then #define _XTAL_FREQ 20000000 is defining our crystal frequency which is 20 MHz. This crystal frequency is necessary to be defined if you are using __delay_ms() & __delay_us() predefined functions in your code. These predefined functions are used to generate delay in ms() milli seconds and us() microseconds. I made my own delay function so i am not using these predefined functions. Then a character array is initialized. The array size is variable. It contains my website name “microcontroller-project.com“. This is the text which i want to scroll on my 16×2 lcd.  

After the header files now its time to define our functions. First i defined delay function which is used to generate some delay in program execution.Next comes lcd command function.

lcdcmd() function is sending commands to 16×2 lcd. For sending commands first put your command on port-b then select register(to select command register make rs=0). After selecting the command register select which operation you want to perform read or write(for read make rw=1 and write make rw=0). Finally to display character on 16×2 lcd screen give strobe to write command present on data pins of 16×2 lcd. Just make en high(en=1) and after few micro seconds bring it back to low (en=0).

display() functions works in the same way as lcdcmd but with little difference that except command it is writing data on lcd and you just need to select the data register to write data(rs=1 takes you to lcd data register).

lcdint() function is initializing our lcd and pic microcontroller ports. TRISB=0x00 declares pic microcontroller port-b as output port. TRISD5=0,TRISD6=0 & TRISD7=0 are making individual pins of port-d as output. Rest of the commands are initializing our 16×2 lcd. If you want to know about the functions of the commands just go through the tutorial(Link is given at the top after first paragraph). 

In the main function i am displaying text on 16×2 lcd. I start displaying text from first coulomb of row 1 and when my text reaches at the end of the row 1 i start scrolling the text to the left.The statement if(i>=14){lcdcmd(0x18);} is performing this function. When my text reaches to last coulomb of 16×2 lcd which is 15 i start scrolling it to left. The command/statement lcdcmd(0x18) scrolls the text on 16×2 lcd using pic microcontroller.  

I recommend you to please go through the tutorial 16×2 lcd pinout and working if you don’t understand commands and their working. 

   
  #include <htc.h>
  #define _XTAL_FREQ 20000000
  char text[]={microcontroller-project.com };
   
  void delay(unsigned int time) //Time delay function
  {
  unsigned int i,j;
  for(i=0;i< time;i++)
  for(j=0;j< 5;j++);
  }
   
  //Function for sending values to the command register of LCD
  void lcdcmd(unsigned char value)
  {
  PORTB=value;
  RD6= 0; //register select-rs
  RD5 = 0; //read-write-rw
  RD7 = 1; //enable-e
  delay(50);
  RD7=0; //enable-e
  delay(50);
   
  }
  //Function for sending values to the data register of LCD
  void display(unsigned char value)
  {
  PORTB=value;
  RD6= 1; //register select-rs
  RD5= 0; //read-write-rd
  RD7= 1; //enable-e
  delay(500);
  RD7=0; //enable-e
  delay(50);
   
  }
  //function to initialize the registers and pins of LCD
  //always use with every lcd of hitachi
  void lcdint(void)
  {
  TRISB=0x00; //PortB is used as output port
  TRISD5=0;
  TRISD6=0;
  TRISD7=0;
  delay(15000);display(0x30);delay(4500);display(0x30);delay(300);display(0x30);delay(650);
  lcdcmd(0x38); //Character font made is 5×7 matrix
  delay(50);
  lcdcmd(0x0C);
  delay(50);
  lcdcmd(0x01);
  delay(50);
  lcdcmd(0x06);
  delay(50);
  lcdcmd(0x80); //Selects 16×2 lcd coulomb 1 row 1
  delay(50);
  }
   
  void main()
  {
  unsigned int i;
  lcdint();
   
  while(1){
  lcdcmd(0x80);
  lcdcmd(0x01);
  i=0;
  while(text[i]!=\0){
  display(text[i]);
  delay(2000);
  if(i>=14)
  {lcdcmd(0x18);}
  delay(3000);
  i++;
  }
  lcdcmd(0x01);
  delay(5000);
  }
  }
 
Download the project files with code and simulation.The code is written in c language using mplab-ide and high tech c compiler is used to compile the code. Simulation is made in proteaus 8.0. Please give me your feed back on the project. Write your comments below.
In the video below i run the code on 20×4 size lcd. The output is not stable its moving too fast we have to lower the crystal frequency or increase the delay in the code. If you run the same above code on 16×2 lcd the output will be stable and smooth. In-fact the above code is written for 16×2 lcd :D. I just tried to check it on 20×4 to see what happens.   
 

Watch the Project video Here…..

https://www.facebook.com/electronicsgru/videos/701588303304145/?t=0

Moving Scrolling Text on lcd using Pic18f452 Microcontroller

Now how to display scrolling text on lcd (16×2) using Pic18f452 microcontroller. The technique is same as stated above for Pic16f877 microcontroller. Only the change is in the code syntax. Code is written in MpLab-IDE. Project files and Proteaus simulation is given below.

In simulation both pic microcontroller codes hex files are given. You can verify both the codes working accurately according to our requirements by running simulation on both the codes hex files separately.  

#include <p18f452.h>
  //#define _XTAL_FREQ 4000000 //Frequency of Oscillator 4MHz
  //_CONFIG_DECL(0x21,0x01,0x00,0x01,0x10,0x0F,0x30,0x0F,0xE0,0x0F,0xF0);
   
  #pragma config OSC = XT
  #pragma config OSCS = OFF
  #pragma config PWRT = OFF
  #pragma config BOR = OFF
  #pragma config BORV = 27
  #pragma config WDT = OFF
  #pragma config WDTPS = 1
  #pragma config CCP2MUX = ON
  #pragma config STVR = OFF
  #pragma config LVP = ON
  #pragma config DEBUG = OFF
  #pragma config CP0 = OFF
  #pragma config CP1 = OFF
  #pragma config CP2 = OFF
  #pragma config CP3 = OFF
  #pragma config CPB = OFF
  #pragma config CPD = OFF
  #pragma config WRT0 = OFF
  #pragma config WRT1 = OFF
  #pragma config WRT2 = OFF
  #pragma config WRT3 = OFF
  #pragma config WRTB = OFF
  #pragma config WRTC = OFF
  #pragma config WRTD = OFF
  #pragma config EBTR0 = OFF
  #pragma config EBTR1 = OFF
  #pragma config EBTR2 = OFF
  #pragma config EBTR3 = OFF
  #pragma config EBTRB = OFF
   
   
  void delay(unsigned int time) //Time delay function
  {
  unsigned int i=0,j=0;
  for(i=0;i< time;i++);
  //for(j=0;j< 2;j++);
  }
   
  //Function for sending values to the command register of LCD
  void lcdcmd(unsigned char value)
  {
  PORTB=value;
  PORTDbits.RD6= 0; //register select-rs
  PORTDbits.RD5 = 0; //read-write-rd
  PORTDbits.RD7 = 1; //enable-e
  delay(50);
  PORTDbits.RD7=0; //enable-e
  delay(50);
   
  }
  //Function for sending values to the data register of LCD
  void display(unsigned char value)
  {
  PORTB=value;
  PORTDbits.RD6= 1; //register select-rs
  PORTDbits.RD5= 0; //read-write-rd
  PORTDbits.RD7= 1; //enable-e
  delay(50);
  PORTDbits.RD7=0; //enable-e
  delay(50);
   
  }
  //function to initialize the registers and pins of LCD
  //always use with every lcd of hitachi
  void lcdint(void)
  {
  TRISB=0x00; //Port B is used as output port
  TRISD=0x05;
   
   
  delay(15000);
  display(0x30);
  delay(4500);
  display(0x30);
  delay(300);
  display(0x30);
  delay(650);
  lcdcmd(0x38); //5×7 Font text will be displayed on lcd
  delay(50);
  lcdcmd(0x0C); //Display on Cursor off
  delay(50);
  lcdcmd(0x01); //Clear LCD (DDRAM)
  delay(50);
  lcdcmd(0x06); //Entry Mode
  delay(50);
  lcdcmd(0x80); //Put Cursor at first line first character space
  delay(50);
  }
   
  void main()
  {
  unsigned int i=0;
  char text[]={I Love Pakistan!!!!!};
   
  lcdint(); //Initialize lcd
   
  while(1){
  lcdcmd(0x80); //Place cursor on first line first character space of lcd
  i=0;
   
  if(PORTDbits.RD0==0){
  PORTDbits.RD1=1;
  for(i=0;i<1000;i++);
  PORTDbits.RD1=0;
  for(i=0;i<1000;i++);
  }
   
  if(PORTDbits.RD2==0){
  while(text[i]!=\0){ //Displaying Text on LCD
  display(text[i]);
  delay(200);
  if(i>=14)
  {
  lcdcmd(0x18); //Moving Display left
  }
  delay(300);
  i++;
  }
  }
  }
  }
 
Both the above codes are performing same functionality. Only the difference is in code statements and syntax. The same circuit diagram given above is for both the code. Nothing is required to be changed in circuit diagram for both the codes.
Visit more text scrolling/moving projects using other microcontrollers. Each project contains its code and circuit diagram in it. All the codes are open source. You can use and modify it according to your needs.

About The Author

Muhammad Bilal

I am a highly skilled and motivated individual with a Master's degree in Computer Science. I have extensive experience in technical writing and a deep understanding of SEO practices.