2
\$\begingroup\$

I am trying to scroll a text on first row of a 16x2 LCD using PIC16F688 (Using MikroC compiler). At the moment I want to make a function that inputs a text and scroll it from right to left, like the first character appears at the right and when the next character comes in, last one moves one column left. Is it possible to make this as a general function(method) which automaticly calculate number of characters? If it was C# or something else I would know it is possible to do so with STRLEN function. But what about doing this on PIC:

void leftScroll(char text[])
{
int chars = strlen(text); //get number of characters in given text
char number[] = chars; // store number of charcters
Lcd_Out(1,1, chars); //show number of characters (DEBUG)
}

Any suggestion would be nice!

Here is my program so far. This scrolls the text from right to left. But I want my message start from left and character by character.

// LCD module connections
sbit LCD_RS at RC4_bit;
sbit LCD_EN at RC5_bit;
sbit LCD_D4 at RC0_bit;
sbit LCD_D5 at RC1_bit;
sbit LCD_D6 at RC2_bit;
sbit LCD_D7 at RC3_bit;
sbit LCD_RS_Direction at TRISC4_bit;
sbit LCD_EN_Direction at TRISC5_bit;
sbit LCD_D4_Direction at TRISC0_bit;
sbit LCD_D5_Direction at TRISC1_bit;
sbit LCD_D6_Direction at TRISC2_bit;
sbit LCD_D7_Direction at TRISC3_bit;
// End LCD module connections

char Message1[] = "Sean Walter"; //Top line message
char Message2[] = "Welcome to my LCD project!";
char i;
void main()
{
  ANSEL = 0b00000100; // RA2/AN2 is analog input
  ADCON0 = 0b00001000; // Analog channel select @ AN2
  ADCON1 = 0x00;
  CMCON0 = 0x07 ; // Disbale comparators
  TRISC = 0b00000000; // PORTC All Outputs
  TRISA = 0b00001100; // PORTA All Outputs, Except RA3 and RA2
  Lcd_Init();        // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);             // CLEAR display
  Lcd_Cmd(_LCD_CURSOR_OFF);        // Cursor off
  Lcd_Out(1,1,Message1);
     Lcd_Out(2,1, Message2);


  do
  {
   delay_ms(50);
     for(i=0; i<2; i++) {               // Move text to the right 4 times
    Lcd_Cmd(_LCD_SHIFT_RIGHT);
  }
   delay_ms(50);
  }
  while(1);

}
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

The simplest way to get a scrolling effect is to treat your input text as a circular buffer. Maintain a read pointer, which is incremented each time you render. To render, read a window of text extending from the read pointer.

If you want snake-like scrolling for your 16x2 display, extend the example below to feature either a 32 character window or two 16 character windows (The 16x2 display I have actually has several bytes of offscreen storage, meaning that a simple 32 byte window may not work for you).

#include <stdio.h>
#include <string.h>

// Get text[offset] if text were an infinitely long repeating loop
char getLoopChar(const char *text, int offset)
{
    return text[offset % strlen(text)];
}

int main(int argc, char *argv[])
{
    const char *text = "This is a scrolling message...";
    int window_start = 0;

    while(1)
    {
        int i;
        for (i=0;i<16;i++)  // loop over characters in the window
            putchar(getLoopChar(text, window_start+i));
        putchar('\n');
        window_start++;
    }

    return 0;
}
\$\endgroup\$
5
  • \$\begingroup\$ Thanks for info. What compiler is based your code? \$\endgroup\$
    – Dumbo
    Aug 10, 2011 at 10:31
  • 1
    \$\begingroup\$ This code should compile with any C99 compiler, eg. GCC. It's just an example to give you an idea how to progress. If you have more detailed questions, post them. \$\endgroup\$ Aug 10, 2011 at 10:35
  • \$\begingroup\$ I'v updated my question with latest progress. would you mind tell me how to implement your code into my own? Thanks. Also if possible how to scroll texts in individual rows? my code scrolls both rows. \$\endgroup\$
    – Dumbo
    Aug 10, 2011 at 13:14
  • \$\begingroup\$ I'm wondering what is the job of putchar('\n'); here? \$\endgroup\$ Mar 14, 2012 at 10:09
  • \$\begingroup\$ It prints a newline, so on a PC you can see the strings being produced. On a real LCD, you may want to clear the screen. \$\endgroup\$ Mar 14, 2012 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.