Sunday, January 25, 2015

MikroC Tutorial LCD Interafacing

The mikroC PRO for PIC provides a library for communication with Lcds (with HD44780 compliant controllers) through the 4-bit interface. An example of Lcd connections is given on the schematic at the bottom of this page

Library Routines

*Lcd_Init    
Lcd_Init();    // Initializes Lcd module. LCD pins must be defined before using this routine
*Lcd_Out   
Lcd_Out(1, 3, "Hello!");                     // Prints text on Lcd starting from specified position.
*Lcd_Out_Cp   
Lcd_Out_Cp("Here!");            // Prints text on Lcd at current cursor position.
*Lcd_Chr     
Lcd_Chr(2, 3, 'i');                    // Prints character on Lcd at specified position.
*Lcd_Chr_Cp
Lcd_Chr_Cp('e');                                      // Prints character on Lcd at current cursor position.
*Lcd_Cmd     
Lcd_Cmd(_LCD_CLEAR);                            // Sends command to Lcd.

Available LCD Commands

Lcd Command
Purpose
_LCD_FIRST_ROW
Move cursor to the 1st row
_LCD_SECOND_ROW
Move cursor to the 2nd row
_LCD_THIRD_ROW
Move cursor to the 3rd row
_LCD_FOURTH_ROW
Move cursor to the 4th row
_LCD_CLEAR
Clear display
_LCD_RETURN_HOME
Return cursor to home position, returns a shifted display to its original position. Display data RAM is unaffected.
_LCD_CURSOR_OFF
Turn off cursor
_LCD_UNDERLINE_ON
Underline cursor on
_LCD_BLINK_CURSOR_ON
Blink cursor on
_LCD_MOVE_CURSOR_LEFT
Move cursor left without changing display data RAM
_LCD_MOVE_CURSOR_RIGHT
Move cursor right without changing display data RAM
_LCD_TURN_ON
Turn Lcd display on
_LCD_TURN_OFF
Turn Lcd display off
_LCD_SHIFT_LEFT
Shift display left without changing display data RAM
_LCD_SHIFT_RIGHT
Shift display right without changing display data RAM








 

Library Example

// 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 txt1[] = "mikroElektronika";    
char txt2[] = "EasyPIC6";
char txt3[] = "Lcd4bit";
char txt4[] = "example";
 
char i;                              // Loop variable
 
void Move_Delay() {                  // Function used for text moving
  Delay_ms(500);                     // You can change the moving speed here
}
 
void main(){
  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,6,txt3);                 // Write text in first row
 
  Lcd_Out(2,6,txt4);                 // Write text in second row
  Delay_ms(2000);
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
 
  Lcd_Out(1,1,txt1);                 // Write text in first row
  Lcd_Out(2,5,txt2);                 // Write text in second row
 
  Delay_ms(2000);
 
  // Moving text
  for(i=0; i<4; i++) {               // Move text to the right 4 times
    Lcd_Cmd(_LCD_SHIFT_RIGHT);
    Move_Delay();
  }
 
  while(1) {                         // Endless loop
    for(i=0; i<8; i++) {             // Move text to the left 7 times
      Lcd_Cmd(_LCD_SHIFT_LEFT);
      Move_Delay();
    }
 
    for(i=0; i<8; i++) {             // Move text to the right 7 times
      Lcd_Cmd(_LCD_SHIFT_RIGHT);
      Move_Delay();
    }
  }
}
 
 


No comments: