Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to shift message from left to right

Status
Not open for further replies.

Parth86

Member
Hi
I am using 8051 MCU and keil software. How to shift message from left to right and then right to left in program

C:
// Program for LCD Interfacing with 8051 Microcontroller

#include<reg51.h>
#define display_port P1                    //Data pins connected to port 2 on microcontroller
sbit rs  =  P2^0;                                    //RS pin connected to pin 2 of port 3
sbit rw  =  P2^1;                                   // RW pin connected to pin 3 of port 3
sbit e  =  P2^2;                                    //E pin connected to pin 4 of port 3

void msdelay(unsigned int time)  // Function for creating delay in milliseconds.
{
    unsigned   i,  j ;
    for(i = 0;  i <  time;   i++)
    for(j  =  0; j  <  1275;  j++);
}
void lcd_cmd(unsigned char command)  //Function to send command instruction to LCD
{
    display_port  =  command;
    rs  =  0;
    rw  =  0;
    e  =  1;
    msdelay(1);
    e  =  0;
}

void lcd_data(unsigned char disp_data)  //Function to send display data to LCD
{
    display_port  =  disp_data;
    rs  =  1;
    rw  =  0;
    e  =  1;
    msdelay(1);
    e  =  0;
}

 void lcd_init()                  //Function to prepare the LCD  and get it ready
{
    lcd_cmd(0x38);           // for using 2 lines and 5X7 matrix of LCD
    msdelay(10);
    lcd_cmd(0x0F);          // turn display ON, cursor blinking
    msdelay(10);
    lcd_cmd(0x01);         //clear screen
    msdelay(10);
    lcd_cmd(0x81);        // bring cursor to position 1 of line 1
    msdelay(10);
}
void main()
{
      unsigned  char  a[15] = "ELECTRO-TECH";       //string of 14 characters with a null terminator.
      int  l = 0;
    lcd_init();
    while ( a[l]   !=  '\0')                                                   // searching the null terminator in the sentence
    {
        lcd_data(a[l]);
        l++;
        msdelay(50);
    }
}
 
A quick guess to get you started.
Code:
void main()
{
    unsigned  char  a[16] = "ELECTRO-TECH    ";       //string of 14 characters with a null terminator.
    char i;
    lcd_init();
    while (1){
        char temp;
        //scroll in memory and display
        lcd_cmd(0x80);        //set to start of display
        temp=a[0];
        for(i=0;i<15;i++){
            a[i]=a[i+1];
            lcd_data(a[i]);
        }
        //do final character
        a[15]=temp;
        lcd_data(temp);
        msdelay(50);
    }
}

Mike.
 
Last edited:
Thanks for your quick response. Message shift character by character. I want to shift entire message

upload_2017-10-19_16-37-11.png
 
You need this function...
C:
void Lcdprint(char* str)
   {
   while(*str != 0)
      lcd_data(*str++);
   }

This will draw a line of text on the screen.. Then manipulate the string before the LCD write.
C:
char lcd_buffer[17]  = {"Electro - tech  "};

void rotateleft(void)
   {
   char temp;
   temp = lcdbuffer[0];
   for( x = 1;x<16,x++)
      {
      lcdbuffer[x] = lcdbuffer[x+1];
      }
   lcdbuffer[16]=temp;
   lcdbuffer[17] = 0;

   Lcd_print(lcdbuffer);
   }

using a buffer should minimise flicker!! **THESE ROUTINES ARE UNTESTED**
 
I figured that shifting and then writing would not be necessary as the write to LCD is the slowest part, 1mS in this case.

Mike.
 
You need this function...
C:
void Lcdprint(char* str)
   {
   while(*str != 0)
      lcd_data(*str++);
   }

This will draw a line of text on the screen.. Then manipulate the string before the LCD write.
C:
char lcd_buffer[17]  = {"Electro - tech  "};

void rotateleft(void)
   {
   char temp;
   temp = lcdbuffer[0];
   for( x = 1;x<16,x++)
      {
      lcdbuffer[x] = lcdbuffer[x+1];
      }
   lcdbuffer[16]=temp;
   lcdbuffer[17] = 0;

   Lcd_print(lcdbuffer);
   }

using a buffer should minimise flicker!! **THESE ROUTINES ARE UNTESTED**
You are making two separate routine rotateleft and rotateleft but I already have data routine lcd_data. if I replace my routine with your. I am getting error. What should be in main program ?
 
C:
char lcdbuffer[17]  = {"Electro - tech  "};
void msdelay(unsigned int time)  // Function for creating delay in milliseconds.
{
    unsigned   i,  j ;
    for(i = 0;  i <  time;   i++)
    for(j  =  0; j  <  75;  j++);
}
void lcd_cmd(unsigned char command)  //Function to send command instruction to LCD
{
    display_port  =  command;
    rs  =  0;
    rw  =  0;
    e  =  1;
    msdelay(1);
    e  =  0;
}
void lcd_data(unsigned char disp_data)  //Function to send display data to LCD
{
    display_port  =  disp_data;
    rs  =  1;
    rw  =  0;
    e  =  1;
    msdelay(1);
    e  =  0;
}
void Lcd_print(char* str)
   {
   while(*str != 0)
      lcd_data(*str++);
   }
 void lcd_init()                  //Function to prepare the LCD  and get it ready
{
     msdelay(20);
    lcd_cmd(0x38);           // for using 2 lines and 5X7 matrix of LCD
    msdelay(10);
    lcd_cmd(0x0C);          // turn display ON, cursor blinking
    msdelay(10);
    lcd_cmd(0x06);         //clear screen
    msdelay(10);
    lcd_cmd(0x1);        // bring cursor to position 1 of line 1
    msdelay(10);
}
void rotateRight(void)
   {
   char temp, x;
   temp = lcdbuffer[15];
   for( x = 15;x >0 ;x--)
      {
      lcdbuffer[x] = lcdbuffer[x-1];
      }
   lcdbuffer[0]=temp;
   lcdbuffer[16] = 0;
   lcd_cmd(0x80);
   Lcd_print(lcdbuffer);
   msdelay(200);
   }
void rotateleft(void)
   {
   char temp,x;
   temp = lcdbuffer[0];
   for( x = 0;x<15;x++)
      {
      lcdbuffer[x] = lcdbuffer[x+1];
      }
   lcdbuffer[15]=temp;
   lcdbuffer[16] = 0;
   lcd_cmd(0x80);
   Lcd_print(lcdbuffer);
   msdelay(200);
   }
void main()
   {
   
   int  i ;
   lcd_init();
   Lcd_print(lcdbuffer);  
   while(1) 
      {    
      msdelay(1000);
      i = 16;
      while(i--)   
  rotateleft();                                           // searching the null terminator in the sentence
      msdelay(1000);
      i=16;
      while(i--)   
  rotateRight();
     
      }
   }

Had to mess up your delay function as mine runs at 12Mhz..

Also I have left off the definitions as mine were different.
 
Here the whole ball of wax from the iink I posted
Code:
#include<reg51.h>
sbit rs=P3^5;   //Register select (RS)
sbit en=P3^6;   //Enable (EN) pin
sbit rw=P3^7;   //Read write (RW) pin
char text[]={"microcntroler-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) 
{
P1=value;
rs = 0;
    rw = 0;
    en = 1;
delay(50);
en=0;
delay(50);

}
             //Function for sending values to the data register of LCD
void display(unsigned char value) 
{
P1=value;
rs = 1;
rw = 0;
en = 1;
delay(500);
en=0;
delay(50);

}
             //function to initialize the registers and pins of LCD
             //always use with every lcd of hitachi
void lcdint(void)         
{
P1=0x00;    //Port 1 is used as output port             
P3=0x03;    //Port 3 higher bits from 2 to 7 are used as output and lower two bits are used to activate UART 
     delay(15000);
     display(0x30);
     delay(4500);
     display(0x30);
     delay(300);
     display(0x30);
     delay(650);
lcdcmd(0x38);
delay(50);
lcdcmd(0x0F);
delay(50);
lcdcmd(0x01);
delay(50);
lcdcmd(0x06);
delay(50);
lcdcmd(0x8F);
delay(50);
}

void main()
{
unsigned int i;

lcdint();
 i=0;
while(text[i]!='\0'){
 display(text[i]);
 delay(200);
 lcdcmd(0x18);
 delay(600);
 i++;
}

}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top