Program 17: LCD Counter 00–99
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;
       LCD_E = 1;
       __delay_ms(2);
       LCD_E = 0;
       __delay_ms(2);
   }

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

   void main(void)
   {
       unsigned char count;

        ADCON1 = 0x0F;                      // make pins digital
        TRISB = 0x00;                       // LCD data
        TRISAbits.TRISA0 = 0;               // RS
        TRISAbits.TRISA1 = 0;               // E

        __delay_ms(20);
        LCD_Command(0x38);                  // 8-bit, 2 lines
        LCD_Command(0x0C);                  // display ON, cursor OFF
        LCD_Command(0x01);                  // clear
        __delay_ms(5);

        LCD_Command(0x80);                  // line 1
        LCD_Data('C');
        LCD_Data('O');
        LCD_Data('U');
        LCD_Data('N');
        LCD_Data('T');

        while(1)
        {
            for(count = 0; count <= 99; count++)
            {
                LCD_Command(0xC0);               // line 2, position 1
                LCD_Data(count / 10 + '0');      // tens digit
                LCD_Data(count % 10 + '0');      // units digit
                __delay_ms(500);
            }
        }
   }
