So I came to this part of manipulating the LCD to operate in 4bit mode, however, I don't understand this part of the code where the byte coming from PORTD is divided into two nibbles. Please teach me senpais, thank you so much!
Code:
#define RS LATD2 /*PIN 2 of PORTD is assigned for register select Pin of LCD*/
#define EN LATD3 /*PIN 3 of PORTD is assigned for enable Pin of LCD */
#define ldata LATD /*PORTD(PD4-PD7) is assigned for LCD Data Output*/
#define LCD_Port TRISD /*define macros for PORTD Direction Register*/
void LCD_Command(unsigned char cmd )
{
ldata = (ldata & 0x0f) |(0xF0 & cmd); /*Send higher nibble of command first to PORT*/
RS = 0; /*Command Register is selected i.e.RS=0*/
EN = 1; /*High-to-low pulse on Enable pin to latch data*/
NOP();
EN = 0;
MSdelay(1);
ldata = (ldata & 0x0f) | (cmd<<4); /*Send lower nibble of command to PORT */
EN = 1;
NOP();
EN = 0;
MSdelay(3);
}
void LCD_Char(unsigned char dat)
{
ldata = (ldata & 0x0f) | (0xF0 & dat); /*Send higher nibble of data first to PORT*/
RS = 1; /*Data Register is selected*/
EN = 1; /*High-to-low pulse on Enable pin to latch data*/
NOP();
EN = 0;
MSdelay(1);
ldata = (ldata & 0x0f) | (dat<<4); /*Send lower nibble of data to PORT*/
EN = 1; /*High-to-low pulse on Enable pin to latch data*/
NOP();
EN = 0;
MSdelay(3);
}