임베디드 리눅스 - 텍스트 LCD
실습13 텍스트 LCD 제어 프로그램
* 실습목표
mmap 함수를 이용하여 텍스트 LCD 디바이스를 제어한다.
텍스트 LCD 응용 프로그램으로는 자신의 학번과 이름을 텍스트 LCD에 디스플레이 하고, 좌우로 시프트되는 프로그램을 작성한다.
* 기초지식
텍스트LCD는 DR(Data Register)과 IR(Instruction Register)을 이용하여 LCD에 글자를 표시하고 있다. 그러나 타겟보드의 메인 CPU는 DR/IR 레지스터를 직접 액세스하지 못하고, 텍스트 LCD모듈에서 인터페이스 나온11개 비트(D0~D7,RS,R/W,E)를 이용하여 제어해야한다.
텍스트LCD는 모듈의 물리번지는 0x1070 0000'이므로 mmap함수를 이용한 응용프로그램은 이 번지를 사용하여 TextLcd를 사용할 수 있다.
* 실습순서
1. 텍스트LCD프로그램에서 사용하며 각종 헤더를 첨가하고, 텍스트 LCD 하드웨어 주소를 설정한다.
# define ADDRESSOFTEXTLCD 0x10700000
2. 명령을 받아 텍스트 LCD의 명령값을 전달할 수 있도록 Enable Clock(D10)을 Low> High > Low로 바꾸어 주는 함수 setcommand()를 구현한다.
void setcommand(unsigned short command
{
command &= 0x00FF;
*pTextlcd = command | 0x0000;
*pTextlcd = command|0x0400;
*pTextlcd = command | 0x0000;
usleep(50);
}
3. 제어 명령표의 DataWrite to GG RAM or DD RAM'을 사용하여 텍스트 LCD에 직접 글자를 쓰는 함수
writebyte() 함수를 생성한다.
void wrtiebyte(char ch)
{
unsigned short data;
data = ch & 0x00FF;
*pTextlcd = data|0x100;
*pTextlcd = data|0x500;
*pTextlcd = data|0x100;
usleep(50);
}
4. 텍스트 LCD화면을 지우고 커서를 home으로 옮기는 clear_display()함수와 return_home()함수를 구현한다.
int return_home(){
unsigned short command = 0x02;
setcommand(command);
usleep(2000);
return -1;
}
int clear_display(){
unsigned short command = 0x01;
setcommand(command);
usleep(2000);
return 1;
}
5. Entry Mode Set(0x04)을 설정하고 인수로 받은 값에 따라 커서의 위치를 증/감하거나 화면을 시프트 하는 entry_mode_set()함수를 구현한다.
int entry_mode_set(int increase, int nshift){
unsigned short command =0x04;
command = increase - (command | 0x02) : commad;
command = nshift - (command | 0x01) : command;
setcommand(command);
return 1;
}
6. Display On/Off Control(0x08)을 설정하고 인수로 받은 값에 따라 화면을 On/Off하거나 커서를 깜빡이는 함수display_control()를 구현한다.
int displaY_control(int display_enable, int cursor_enable, int nblink){
unsigned short command = 0x08;
command = display_enable - (command | 0x04) : command;
command = cursor_enable - (command | 0x02): command;
command = nblink - (command | 0x01) : command;
setcommand(command);
return 1;
}
7. Curcor 혹은 Display Shift(0x10)을 설정하고 인수로 받은 값에 따라 화면 또는 커서를 왼쪽, 오른쪽으로 시프트 하는 함수 cursor_shift()를 구현한다.
int cursor_shit(intset_screen, int set_rightshit){
unsigned short command = 0x10;
command = set_screen - (command | 0x08): command;
command = set_rightshit - (command | 0x04) : command;
setcommand(command);
reutnr 1;
}
8. 텍스트LCD를 초기화하는 initialize_text()함수를 아래 순서대로 구현한다.
int function_set(int rows, int nfonts){
unsigned short command = 0x30;
if(rows ==2) command |= 0x08;
else if(rows ==1) command & = 0xf7;
else return -1;
command = nfonts - (command | 0x04) : command;
setcommand(command);
return 1;
}
int set_ddram_address(int pos){
unsigned short command = 0x80;
command+= pos;
setcommand(command);
return 1;
}
void initialize_textlcd(){
function_set(2,0); // Function Set : 8bit, display 21lines 5x7 mod
display_control(1,0,0); // Display on, Cursor off
clear_display(); // Display clear
return home(); // go home
entry_mode_set(1,0); // EntryModeSet: shift right cursor
usleep(2000);
}