Thursday, September 9, 2010

PIC16F887/877 programming in C Tutorial 7 (LCD Moving Display n Custom Characters)

LCD; Moving Display & Custom Character:
You can display your custom characters by using mikroc custom character tool. Go to tools>>customer character then make the character you want to display. After this click on 'generate code'. It will generate a customchar function just copy and paste above the main function. customchar has two parameters; row and position in that row. Just pass these parameters where you want to see your custom character on LCD.
You can also move the text, left and right, on LCD screen; by using the LCD commands. _LCD_SHIFT_LEFT and   _LCD_SHIFT_RIGHT are the commands. There are various other commands in mikroc, i forget to tell you in lcd tutorial, these are:
LCD Commands
Now lets write a code to display and move the custom character, back and forth, (of your choice) on LCD screen.

Code:

// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

char txt[] = "MOVING TEXT";
char txt1[] = "EEW";
char i;                              // Loop variable
const char character[] = {0,31,4,10,17,17,14,0};

void CustomChar(char pos_row, char pos_char) {
  char i;
    Lcd_Cmd(64);
    for (i = 0; i<=7; i++) Lcd_Chr_CP(character[i]);
    Lcd_Cmd(_LCD_RETURN_HOME);
    Lcd_Chr(pos_row, pos_char, 0);
}

void main(){
///////// comment for 877////////////////////////////////////////
  ANSEL  = 0;                        // Configure AN pins as digital I/O
  ANSELH = 0;
  C1ON_bit = 0;                      // Disable comparators
  C2ON_bit = 0;
////////////////////////////////////////////////////////////////
  Lcd_Init();                        // Initialize LCD

  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  Lcd_Out(1,8,txt);                 // Write text in first row
  Lcd_Out(2,11,txt1);                 // Write text in second row
  CustomChar(2,14);
  CustomChar(2,10);

  // Moving text
  for(i=0; i<8; i++) {               // Move text to the right 4 times
    Lcd_Cmd(_LCD_SHIFT_RIGHT);
    Delay_ms(200);
  }

  while(1) {                         // Endless loop
    for(i=0; i<8; i++) {             // Move text to the left 7 times
      Lcd_Cmd(_LCD_SHIFT_LEFT);
      Delay_ms(200);
    }

    for(i=0; i<8; i++) {             // Move text to the right 7 times
      Lcd_Cmd(_LCD_SHIFT_RIGHT);
      Delay_ms(200);
    }
  }
}

After you specify the LCD connections paste the code of your custom character you wanna display. Then initialize the LCD, display text (if you want), and display your custom char by passing the positions parameter.  

Now to move text just use for loop and LCD command LEFT/RIGHT, where you want to move. Number of iterations depends on you. Once you have done this, use a endless loop and apply the for loop to move text in back forth. That is it!!.

Schematic:
moving text

4 comments:

  1. Hey.
    You've done excellent job for writing thees tutorials. I am very grateful.
    I was wondering how would you move text up and down?

    ReplyDelete
  2. Thnx buddy; there are no commands in mikroc to move data up and down. You have to do it by yourself; do some experiments use some loop etc; this is the way you can learn more.....

    ReplyDelete
  3. To move up and down. U have to used shift register 74HC595 or higher, etc.

    ReplyDelete