/*--- Macros ---*/
#define RS LATDbits.LATD8
#define RS_TRIS TRISDbits.TRISD8
#define RW LATDbits.LATD9
#define RW_TRIS TRISDbits.TRISD9
#define E LATDbits.LATD10
#define E_TRIS TRISDbits.TRISD10
/*--- Display commands ---*/
#define CLEAR 0x01U
#define LINE2 0xc0U
#define C_RIGHT 0x14U
#define SHIFT 0x1CU
#define HOME 0x02U
/*--- Local function prototypes ---*/
static void write_command(char cmd);
static void write_data(char data);
static void puts_lcd(char *data);
static void Strobe_E(void);
static Bool BusyFlag(void);
/*--- Initialise lcd display ---*/
void init_lcd(void)
{
delay_mS(15); /* Reset delay */
LATD = 0;
TRISD = 0;
LATD |= 0x0038; /* 1st LCD initialization sequence */
Strobe_E();
delay_mS(5);
Strobe_E(); /* 2nd LCD initialization sequence */
delay_mS(1);
Strobe_E(); /* 3rd LCD initialization sequence */
delay_mS(1);
write_command(0x38);
write_command(0x0c);
write_command(0x06);
}
/*--- Write command to display ---*/
static void write_command(char cmd)
{
LATD &= 0xff00;
LATD |= cmd;
RW = 0;
RS = 0;
Strobe_E();
while(BusyFlag()){
;
}
}
/*--- Write data to display ---*/
static void write_data(char data )
{
RW = 0;
RS = 1;
LATD &= 0xff00;
LATD |= data;
Strobe_E();
RS = 0;
while(BusyFlag()){
;
}
}
/*--- Write string to display ---*/
static void puts_lcd(char *data)
{
while(*data){
write_data(*data++);
}
}
/*--- Read busy flag ---*/
static Bool BusyFlag(void)
{
uint8_t result;
TRISD |= 0x00ff;
RW = 1;
RS = 0;
Strobe_E();
result = PORTD & 0x00ff;
TRISD = 0;
return result & 0x80;
}
/*--- Strobe E ---*/
static void Strobe_E(void)
{
E = 1;
Nop();
Nop();
Nop();
E = 0;
}
/*--- End of File ---*/