ATMEGA128 LCD 한글 출력 - ATMEGA128 LCD hangeul chullyeog

LCD에 문자를 출력하기 위해서는 폰트를 만들어야 한다. 폰트에 기본적으로 알파벳은 들어가 있다. 알파벳은 출력하고자 하는 알파벳을 LCD위치만 설정한다면,

CG ROM에서 폰트를 찾아 화면(LCD)에 출력된다.CG ROM에는 기본문자(아스키코드 알파벳, 도형, 일본어, 기타 등....)가 있어서 LCD에 출력이 가능하다.

하지만, 한글은 없다. 그래서 한글 폰트를 만들어야 한다.

폰트를 새로정의 하는 부분은 CGRAM에서 관장한다.

우리가 쓰는 LCD에 해당하는 도트(.)은 가로5칸, 세로7카인 5*7이다.

  7  6   5   4  3  2   1   0

위 그림은 '소'을 찍어내기 위한 도트 LCD모듈에서의 CG RAM adress부분.

제일 윗줄에는 아무것도 나태내지 않기 때문에 전부 0이다. 2번째 부분부터

도트를 찍는데 2번 자리에 1이 들어간다. 나머지는 모두 0이다.

0x04= 0000 0100이기 때문에 밑에 소스에 각 비트에 해당하는 번호를 

넣었다.

unsigned char font[] = {0x00, 0x04,.........,}

처음부터 8개까지 위의 그림의 '소'에 해당하는 CG RAM adress의 도트이다.

LCD에 찍힐 때는      

   *

 *  *

   *

   *

*****

이런식으로 화면에 나온다. 

LCD_Cmd_WRITE함수는 예전에 만들어 놓았던 명령을 내리는 함수이다. 인자인 0x40은  Set CGram adress이다. 즉, CGram adress로 인자로 받아서, CGRAM에

지정할 문자를 받을 준비를 한다(???)

for(i=0; i<64;i=i+1)

{

LCD_Data_WRITE(font[i]);

}

이 부분을 반복문을 돌려서 font[]배열에 있는 헥사값들을 LCD_Data_WRITE()에 넣어서, 데이터를 입력한다.LCD_Data_WRITE는 실제 데이터를 입력하는 함수이다.

아래는 main()함수와 DD RAM adress

커서의 위치를 설정하는 부분이다. 커서를 해당주소에 위치시키는 것.

여기서 0x80은 첫 번째행 맨 앞에 주소.

이 주소에 커서를 위치시켜서 사용하는 문자를 쓴다. 커서를 변경하고 싶으면,

위의 그림에 보이는 DD RAM ADRESS표를 참고하여 해당 주소만큼 0x80에

더하여 커서를 위치시키면 된다. 

S/W-AVR(mega128)

12. ATmega128 (11)장 LCD 모듈제어 예제4 응용

 LCD 모듈에 한글을 표현하는 프로그램의 응용으로 좌우로 이동

PORTA

PA6

PA5

PA4

PA3

PA2

PA1

PA0

LCD Module

D7

D6

D5

X

EN

R/-W

RS

           
ATMEGA128의 PORTA에 연결된 LCD 모듈은 4비트 방식을 사용한다.

※명령어 입력시 4비트씩 넣는다※


Device    : Atmega128-16AI
Clock     : 16MHz
개발 환경 : CodeVisionAVR

//--------------------------------------------------------------------------------------//

#include "mega128.h"

#define ENABLE  (PORTA.2=1)
#define DISABLE (PORTA.2=0)

void init_PORTA(void)
{
    DDRA = 0xFF;                                  // PORTA 출력
    PORTA=0xFF;                                  // 초기 값
}

void m_delay(unsigned int m)                //딜레이 루틴
{
    unsigned int i, j;
   

    for(i=0;i<m;i++)
        for(j=0;j<2650;j++);                        //16MHz : 1msec
}

void ddelay(unsigned int p)
{
    unsigned int i;

    for(i=0;i<p;i++);
}

void instruction_out(unsigned char b)   //명령어 쓰기 함수
{
    PORTA=b&0xF0;                             //상위 4BIT 출력

   

ENABLE;                                       //ENABLE
    DISABLE;                                      //DISABLE

     PORTA=(b<<4)&0xF0;                     //하위 4BIT 출력
    ENABLE;                                       //ENABLE
    DISABLE;                                      //DISABLE

     m_delay(2);                                   //명령어 수행이 완료될때까지 지연

}

void char_out(unsigned char b)           //LCD에 한문자 출력 함수
{   
    PORTA=(b&0xF0)|0x01;                   //상위 4BIT 출력
    ENABLE;                                       //ENABLE
    DISABLE;                                      //DISABLE

     PORTA=((b<<4)&0xF0)|0x01;           //하위 4BIT 출력
    ENABLE;                                       //ENABLE
    DISABLE;                                      //DISABLE

    m_delay(2);                                   //명령어 수행이 완료될때까지 지연    

}

void string_out(unsigned char b, unsigned char *str)     //문자열 출력 함수
{
    unsigned int i=0;
    instruction_out(b);                                                 //LCD 위치 지정

     do{
    char_out(str[i]);
    }while(str[++i]!='\0');                                           // NULL 문자를 만날 때 까지
}


void init_LCD(void)                //LCD초기화 루틴
{  
    init_PORTA();                   // LCD를 연결한 포트 초기화

   

m_delay(15);

                 ddelay(600);    
    instruction_out(0x28);        //LCD FUNCTION SET(16X2 LINE, 4 BIT, 5X8 DOT)           
    m_delay(2);
    instruction_out(0x28);        //LCD FUNCTION SET(16X2 LINE, 4 BIT, 5X8 DOT)   
    m_delay(2);
    instruction_out(0x0C);       //LCD DISPLAY ON, CURSOR OFF, BLINK OFF
    instruction_out(0x06);        //LCD ENTRY MODE SET
    instruction_out(0x02);        //RETURN HOME
    instruction_out(0x01);        //LCD CLEAR
    instruction_out(0x01);        //LCD CLEAR
}

void main(void)
{
    unsigned char i;
    unsigned char str[]={"HANGUL"};

     unsigned char name_a[]={0x00,0x1f,0x11,0x11,0x11,0x11,0x1f,0x00};    /* ㅁ */
    unsigned char name_b[]={0x10,0x10,0x10,0x1c,0x10,0x10,0x10,0x10};  /* ㅏ */
    unsigned char name_c[]={0x00,0x0e,0x11,0x11,0x11,0x11,0x0e,0x00};  /* ㅇ */
    unsigned char name_d[]={0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10};  /* ㅣ */
    unsigned char name_e[]={0x1f,0x01,0x1f,0x01,0x01,0x00,0x00,0x1f};    /* 크 */
    unsigned char name_f[]={0x1f,0x01,0x1f,0x10,0x1f,0x04,0x04,0x1f};      /* 로 */
    unsigned char name_g[]={0x1f,0x04,0x04,0x04,0x04,0x00,0x00,0x00};   /* ㅜ */
    unsigned char name_h[]={0x00,0x04,0x0a,0x11,0x11,0x00,0x00,0x1f};   /* 스 */

     init_LCD();

   

instruction_out(0x01);      // LCD를 지운다.

     string_out(0x80, str);       // LCD의 첫번째 라인

        instruction_out(0x40);  /* 위에서 만들어 준 한글폰트를 CGRAM에 저장 */
    for(i=0;i<8;i++)
        char_out(name_a[i]);

      instruction_out(0x48);
    for(i=0;i<8;i++)
        char_out(name_b[i]);

      instruction_out(0x50);
    for(i=0;i<8;i++)
        char_out(name_c[i]);

      instruction_out(0x58);
    for(i=0;i<8;i++)
        char_out(name_d[i]);

      instruction_out(0x60);
    for(i=0;i<8;i++)
        char_out(name_e[i]);

      instruction_out(0x68);
    for(i=0;i<8;i++)
        char_out(name_f[i]);

    

instruction_out(0x70);
    for(i=0;i<8;i++)
        char_out(name_g[i]);

      instruction_out(0x78);
    for(i=0;i<8;i++)
       char_out(name_h[i]);

      instruction_out(0xC0); // LCD의 자리에 해당하는 주소값에 따라 각각의 문자들을 출력 
    char_out(0x00);

   

instruction_out(0xC1);
    char_out(0x01);

      instruction_out(0xC2);
    char_out(0x02);

      instruction_out(0xC3);
    char_out(0x03);

     instruction_out(0xC4);
    char_out(0x04);

      instruction_out(0xC5);
    char_out(0x05);

      instruction_out(0xC6);
    char_out(0x00);

      instruction_out(0xC7);
    char_out(0x01);

      instruction_out(0xC8);
    char_out(0x02);

      instruction_out(0xC9);
    char_out(0x06);

      instruction_out(0xCA);
    char_out(0x07);

    while(1)                                   //무한 루프
    {
        for(shift=0;shift<8;shift++)        //16번 shfit시키기 위한 for문
        {
            instruction_out(0x1C);        //LCD SHFIT RIGHT 
            m_delay(300);                   //shfit되는 시간 300msec 지연         
        }
        for(shift=0;shift<16;shift++)      //32번 shfit시키기 위한 for문
        {
            instruction_out(0x18);       //LCD SHFIT LEFT
            m_delay(300);                 //shfit되는 시간         
        }
        for(shift=0;shift<8;shift++)      //16번 shfit시키기 위한 for문
        {
            instruction_out(0x1C);      //LCD SHFIT LEFT
            m_delay(300);                 //shfit되는 시간         
        }

    }

//--------------------------------------------------------------------------------------//

 위 코드는 16문자 2라인 LCD에 한글을 표현하는 프로그램의 응용으로 11장의 예제 1에서 나왔던 좌우로 이동하는 응용프로그램이다.

 위 프로그램에 대한 결과를 동영상으로 찍어보았다.

Toplist

최신 우편물

태그