Program 14: LCD – 'MICROCONTROLLER' / 'LAB ESE'
PIC18F4550 · Embedded C

#include <xc.h>

   #define _XTAL_FREQ 20000000UL

   #define LCD_RS LATAbits.LATA0
   #define LCD_E LATAbits.LATA1

   void LCD_Command(unsigned char cmd)
   {
       LATB = cmd;
       LCD_RS = 0;                 // command mode
       LCD_E = 1;
       __delay_ms(2);
       LCD_E = 0;
       __delay_ms(2);
   }

   void LCD_Data(unsigned char data)
   {
       LATB = data;
       LCD_RS = 1;                 // data mode
       LCD_E = 1;
       __delay_ms(2);
       LCD_E = 0;
       __delay_ms(2);
   }

   void LCD_String(const char *str)
   {
       while(*str != '\0')
       {
           LCD_Data(*str);
           str++;
       }
   }

   void LCD_Start(void)
   {
       __delay_ms(20);
       LCD_Command(0x38);                   // 8-bit, 2 lines
       LCD_Command(0x0C);                   // display ON, cursor OFF
       LCD_Command(0x01);                   // clear display
       __delay_ms(5);
       LCD_Command(0x06);                   // auto-increment cursor
   }

   void main(void)
   {
       ADCON1 = 0x0F;                       // make pins digital

        TRISB = 0x00;                       // LCD data





        TRISAbits.TRISA0 = 0;               // RS
        TRISAbits.TRISA1 = 0;               // E

        LATA = 0x00;
        LATB = 0x00;

        LCD_Start();

        LCD_Command(0x80);          // line 1, position 1
        LCD_String("MICROCONTROLLER");

        LCD_Command(0xC0);                  // line 2, position 1
        LCD_String("LAB ESE");

        while(1);                           // keep display
   }
