1. 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.
    Dismiss Notice

How to shift message from left to right

Discussion in 'Microcontrollers' started by Parth86, Oct 18, 2017.

  1. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    538
    Likes:
    5
    Hi
    I am using 8051 MCU and keil software. How to shift message from left to right and then right to left in program

    Code (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);
        }
    }
     
  2. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,080
    Likes:
    326
    Location:
    Brisbane Australia
    A quick guess to get you started.
    Code (text):

    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: Oct 18, 2017
    • Like Like x 1
  3. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    538
    Likes:
    5
    Thanks for your quick response. Message shift character by character. I want to shift entire message

    upload_2017-10-19_16-37-11.png
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,080
    Likes:
    326
    Location:
    Brisbane Australia

    That's the only way it makes sense - it shifts 1 character every 50mS - the whole message every 0.8 seconds. To shift the whole message would leave it unchanged!

    Mike.
     
  6. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,827
    Likes:
    138
    Location:
    morristown,tn
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,245
    Likes:
    911
    Location:
    Rochdale UK
    You need this function...
    Code (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.
    Code (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**
     
  8. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,080
    Likes:
    326
    Location:
    Brisbane Australia
    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.
     
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,245
    Likes:
    911
    Location:
    Rochdale UK
    No there seems to be a slower part!!!
     
  10. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,080
    Likes:
    326
    Location:
    Brisbane Australia
    The data write routines in the OP use a 1mS delay.

    Mike,
     
  11. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    538
    Likes:
    5
    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 ?
     
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,245
    Likes:
    911
    Location:
    Rochdale UK
    Code (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.
     
    • Like Like x 1
  13. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,827
    Likes:
    138
    Location:
    morristown,tn
    Here the whole ball of wax from the iink I posted
    Code (text):

    #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++;
    }

    }
     

Share This Page