#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;                 // RS = 0 -> command
    LCD_E = 1;
    __delay_ms(2);
    LCD_E = 0;                  // high-to-low pulse latches data
    __delay_ms(2);
}

void LCD_Data(unsigned char data)
{
    LATB = data;
    LCD_RS = 1;                 // RS = 1 -> data (character)
    LCD_E = 1;
    __delay_ms(2);
    LCD_E = 0;
    __delay_ms(2);
}

void LCD_String(const char *str)
{
    while(*str != '\0')         // send characters until end of string
    {
        LCD_Data(*str);
        str++;
    }
}

void LCD_Start(void)
{
    __delay_ms(20);             // LCD power-up delay
    LCD_Command(0x38);          // 8-bit mode, 2 lines, 5x7 font
    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;               // PORTB = LCD data (output)
    TRISAbits.TRISA0 = 0;       // RA0 = RS (output)
    TRISAbits.TRISA1 = 0;       // RA1 = E  (output)

    LATA = 0x00;
    LATB = 0x00;

    LCD_Start();

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

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

    while(1);                   // keep display
}
