Text Box Themes November 2017 Session LCD Programming, 1. In the LCD program we
ID: 2249435 • Letter: T
Question
Text Box Themes November 2017 Session LCD Programming, 1. In the LCD program we created a function to initialize the LCD. Since the LCD is connected to Port K on the Dragon Board, we have to program PORT K as output port. Some students have called the initialization function before programming Port K. As a result, their programs have failed. Explain, using only a few words, why the DDRK OxFF instruction has to precede the call to the LCD initialization function. What is the sequence of commands that you have to send to the LCD so that it can be initialized as 4 bit mode, display on cursor on, clear display, shift cursor right. 2. What is the sequence of commands that you have to send to the LCD so that it can be initialized as & bit mode, display on cursor on, clear display, shift cursor right. 3. What is the reason why you have to have a delay every time information is sent to the LCD? 4. 5. If you want to write UNIVERSITY exactly in the middle of the second row of the LCD what must be the address of the first character U1? You have to show your work T 345Explanation / Answer
1.
By setting the bits of DDRK register make the PORTK as Output.
DDRK bits is assign to latch of IOs of port. If DDRK register is not configured than the data trasfered to PORTK will nor reached to external port due to latch.
So, Without making DDRK = 0xFF LCD intialization is not possible.
2.
The sequence command is 0x28 for 4 bit mode of LCD.
2.
The sequence command is 0x38 for 8 bit mode of LCD.
3.
8 bit mode = Command : 0x38
Cursor On = Command : 0x0A
Clear Display = Command : 0x01
Shift Cursor right = Command : 0x05.
C Code:
void Intil_LCD(void)
{
COMWRT4(0x38);
MSDelay(1);
COMWRT4(0x0A);
MSDelay(1);
COMWRT4(0x01);
MSDelay(1);
COMWRT4(0x05);
MSDelay(1);
}
4.
The input at LCD pins need to be for some predefine interval. So, the need some delay in changing the signal to LCD.
5.
MSDelay(1);
COMWRT4(0x9A); //set 2ed row middle posistion
MSDelay(1);
DATWRT4('U');
Code for 8 bit LCD mode :
#include <hidef.h> /* common defines and macros */
#define LCD_DATA PORTK
#define LCD_CTRL PORTK
#define RS 0x01
#define EN 0x02
void COMWRT4(unsigned char);
void DATWRT4(unsigned char);
void MSDelay(unsigned int);
void main(void)
{
DDRK = 0xFF; // Making Port K as Output
COMWRT4(0x33);
MSDelay(1);
COMWRT4(0x32);
MSDelay(1);
COMWRT4(0x38); //Function set to eight bit data length
MSDelay(1);
COMWRT4(0x06); //entry mode set, increment, no shift
MSDelay(1);
COMWRT4(0x0E); //Display set, disp on, cursor on, blink off
MSDelay(1);
COMWRT4(0x01); //Clear display
MSDelay(1);
COMWRT4(0x80); //set start posistion, home position
MSDelay(1);
DATWRT4('H');
MSDelay(1);
DATWRT4('E');
MSDelay(1);
DATWRT4('L');
MSDelay(1);
DATWRT4('L');
MSDelay(1);
DATWRT4('O');
for(;;); //stay here
}
void COMWRT4(unsigned char command)
{
unsigned char x;
LCD_DATA =LCD_DATA & ~0xFF; //clear bits
LCD_DATA = LCD_DATA | x; //sends high nibble to PORTK
MSDelay(1);
LCD_CTRL = LCD_CTRL & ~RS; //set RS to command (RS=0)
MSDelay(1);
LCD_CTRL = LCD_CTRL | EN; //rais enable
MSDelay(5);
LCD_CTRL = LCD_CTRL & ~EN; //Drop enable to capture command
MSDelay(15); //wait
}
void DATWRT4(unsigned char data)
{
unsigned char x;
LCD_DATA =LCD_DATA & ~0xFF;
LCD_DATA = LCD_DATA | x;
MSDelay(1);
LCD_CTRL = LCD_CTRL | RS;
MSDelay(1);
LCD_CTRL = LCD_CTRL | EN;
MSDelay(1);
LCD_CTRL = LCD_CTRL & ~EN;
MSDelay(15);
}
void MSDelay(unsigned int itime)
{
unsigned int i; unsigned int j;
for(i=0;i<itime;i++)
for(j=0;j<4000;j++);
}
Code for 4 bit LCD mode:
#include <hidef.h> /* common defines and macros */
#define LCD_DATA PORTK
#define LCD_CTRL PORTK
#define RS 0x01
#define EN 0x02
void COMWRT4(unsigned char);
void DATWRT4(unsigned char);
void MSDelay(unsigned int);
void main(void)
{
DDRK = 0xFF; // Making Port K as Output
COMWRT4(0x33); //reset sequence
MSDelay(1);
COMWRT4(0x32); //reset sequence
MSDelay(1);
COMWRT4(0x28); //Function set to four bit data length
//2 line, 5 x 7 dot format
MSDelay(1);
COMWRT4(0x06); //entry mode set, increment, no shift
MSDelay(1);
COMWRT4(0x0E); //Display set, disp on, cursor on, blink off
MSDelay(1);
COMWRT4(0x01); //Clear display
MSDelay(1);
COMWRT4(0x80); //set start posistion, home position
MSDelay(1);
DATWRT4('H');
MSDelay(1);
DATWRT4('E');
MSDelay(1);
DATWRT4('L');
MSDelay(1);
DATWRT4('L');
MSDelay(1);
DATWRT4('O');
for(;;); //stay here
}
void COMWRT4(unsigned char command)
{
unsigned char x;
x = (command & 0xF0) >> 2; //shift high nibble to center of byte for Pk5-Pk2
LCD_DATA =LCD_DATA & ~0x3C; //clear bits Pk5-Pk2
LCD_DATA = LCD_DATA | x; //sends high nibble to PORTK
MSDelay(1);
LCD_CTRL = LCD_CTRL & ~RS; //set RS to command (RS=0)
MSDelay(1);
LCD_CTRL = LCD_CTRL | EN; //rais enable
MSDelay(5);
LCD_CTRL = LCD_CTRL & ~EN; //Drop enable to capture command
MSDelay(15); //wait
x = (command & 0x0F)<< 2; // shift low nibble to center of byte for Pk5-Pk2
LCD_DATA =LCD_DATA & ~0x3C; //clear bits Pk5-Pk2
LCD_DATA =LCD_DATA | x; //send low nibble to PORTK
LCD_CTRL = LCD_CTRL | EN; //rais enable
MSDelay(5);
LCD_CTRL = LCD_CTRL & ~EN; //drop enable to capture command
MSDelay(15);
}
void DATWRT4(unsigned char data)
{
unsigned char x;
x = (data & 0xF0) >> 2;
LCD_DATA =LCD_DATA & ~0x3C;
LCD_DATA = LCD_DATA | x;
MSDelay(1);
LCD_CTRL = LCD_CTRL | RS;
MSDelay(1);
LCD_CTRL = LCD_CTRL | EN;
MSDelay(1);
LCD_CTRL = LCD_CTRL & ~EN;
MSDelay(5);
x = (data & 0x0F)<< 2;
LCD_DATA =LCD_DATA & ~0x3C;
LCD_DATA = LCD_DATA | x;
LCD_CTRL = LCD_CTRL | EN;
MSDelay(1);
LCD_CTRL = LCD_CTRL & ~EN;
MSDelay(15);
}
void MSDelay(unsigned int itime)
{
unsigned int i; unsigned int j;
for(i=0;i<itime;i++)
for(j=0;j<4000;j++);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.