Bytes to Nibbles: How?

Thread Starter

Markus999

Joined Feb 14, 2018
3
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);
}
 

ericgibbs

Joined Jan 29, 2010
21,439
hi 999,
I program in other languages, not C.

These lines Write the Command code to the LCD
ldata = (ldata & 0x0f) |(0xF0 & cmd); /*Send higher nibble of command first to PORT*/
.....................
ldata = (ldata & 0x0f) | (cmd<<4); /*Send lower nibble of command to PORT */


These lines Write the Data code to the LCD
ldata = (ldata & 0x0f) | (0xF0 & dat); /*Send higher nibble of data first to PORT*/
.........................
ldata = (ldata & 0x0f) | (dat<<4); /*Send lower nibble of data to PORT*/

Can you follow that Code OK.?
E
 
Top