Notice
Recent Posts
Recent Comments
관리 메뉴

안까먹을라고 쓰는 블로그

Text-Lcd 모듈 프로그래밍 본문

Embedded/Atmega128

Text-Lcd 모듈 프로그래밍

YawnsDuzin 2012. 9. 1. 20:24

 

반응형

 #include "c:\AvrEdit\JJ128c\JJ128.h" 안의 함수들

 

void Delay_us(unsigned char time_us)  /* time delay for us */
{ register unsigned char i;

  for(i = 0; i < time_us; i++)   // 4 cycle +
    { asm volatile(" PUSH  R0 ");  // 2 cycle +
      asm volatile(" POP   R0 ");  // 2 cycle +
      asm volatile(" PUSH  R0 ");  // 2 cycle +
      asm volatile(" POP   R0 ");  // 2 cycle +
      asm volatile(" PUSH  R0 ");  // 2 cycle +
      asm volatile(" POP   R0 ");  // 2 cycle = 16 cycle = 1 us for 16MHz
    }
}

 

void Delay_ms(unsigned int time_ms)  /* time delay for ms */
{ register unsigned int i;

  for(i = 0; i < time_ms; i++)
    { Delay_us(250);
      Delay_us(250);
      Delay_us(250);
      Delay_us(250);
    }
}

 

8비트 인터페이스 초기화 프로그래밍순서

 

1. 전원을 투입하서나 리셋스위치를 누른다.

2. 초기화를 수행하기 전에 최소한 30 ms 이상을 기다린다.

3. Function set 명령 (0011xx00b)를 보낸다.

4. Display ON/OFF control 명령(000001xxb)을 보낸다.

5. Entry mode set 명령(000001xxb)을 보낸다.

6. DD RAM 어드레스를 보낸다.

7. 표시할 문자 데이터를 보낸다.

8. 필요할 경우 7~8 과정을 반복한다.

 

void MCU_initialize(void)   /* initialize ATmege128 MCU */

  MCUCR = 0xC0;     // enable external memory and I/O
  XMCRA = 0x00;     // 0x1100-0xFFFF=1 wait
  XMCRB = 0x84;     // enable bus keeper, use PC7-PC4 as I/O port

  DDRB = 0xFF;     // PORTB = output
  PORTB = 0x00;
  DDRC = 0xF0;     // PC7-PC4 = output
  PORTC = 0x00;
  DDRD = 0xFF;     // PORTD = output
  PORTD = 0xE0;
  DDRE = 0x3F;     // PE5-PE0 = output, PE7-PE6 = input
  PORTE = 0x00;
  DDRF = 0x00;     // PORTF = input
  PORTF = 0x00;
  DDRG = 0x1F;     // PORTG = output
  PORTG = 0x00;

  OUT_LATCH = 0x00;    // clear 7-segment LED
}

 

void LCD_command(unsigned char command)  /* write a command(instruction) to text LCD */
{
  LCD_CONTROL = 0x00;    // E = 0, Rs = 0
  LCD_DATABUS = command;   // output command
  LCD_CONTROL = 0x01;    // E = 1
  asm volatile(" PUSH  R0 ");   // delay for about 250 ns
  asm volatile(" POP   R0 ");
  LCD_CONTROL = 0x00;    // E = 0
  Delay_us(50);
}

void LCD_data(unsigned char data)  /* display a character on text LCD */
{
  LCD_CONTROL = 0x02;    // E = 0, Rs = 1
  LCD_DATABUS = data;    // output data
  LCD_CONTROL = 0x03;    // E = 1
  asm volatile(" PUSH  R0 ");   // delay for about 250 ns
  asm volatile(" POP   R0 ");
  LCD_CONTROL = 0x02;    // E = 0
  Delay_us(50);
}

void LCD_string(unsigned char command, unsigned char *string) /* display a string on LCD */
{
  LCD_command(command);    // start position of string
  while(*string != '\0')   // display string
    { LCD_data(*string);
       string++;
    }
}

void LCD_initialize(void)   /* initialize text LCD module */
{
  LCD_CONTROL = 0x03;    // E = 1, Rs = 1 (dummy write)
  LCD_CONTROL = 0x02;    // E = 0, Rs = 1
  Delay_ms(2);

  LCD_command(0x38);    // function set(8 bit, 2 line, 5x7 dot)
  LCD_command(0x0C);    // display control(display ON, cursor OFF)
  LCD_command(0x06);    // entry mode set(increment, not shift)
  LCD_command(0x01);    // clear display
  Delay_ms(2);
}

 

 - 기본 텍스트 lcd 프로그램

 

#include <avr/io.h>
#include "c:\AvrEdit\JJ128c\JJ128.h"

int main(void)
{

  unsigned char i;

  MCU_initialize();                             // initialize MCU
  Delay_ms(50);                                 // wait for system stabilization
  LCD_initialize();                             // initialize text LCD module

  while(1)
    {

      LCD_string(0x80,"  JJ-128  V1.0  ");      // display logo
      LCD_string(0xC0," ATmega128-16AI ");      // display message 1
      Beep();
      Delay_ms(2000);


      LCD_string(0xC0,"   2005/10/01   ");      // display message 2
      Beep();
      Delay_ms(2000);

 

      LCD_string(0x80,"0123456789ABCDEF0123456789ABCDEF"); // display long line
      LCD_string(0xC0,"Go shift left and right 16 times");
      Beep();
      Delay_ms(1000);

 

      for(i=0; i<16; i++)                       // shift left
        { LCD_command(0x18);
          Delay_ms(300);
        }


      Delay_ms(1000);

      for(i=0; i<16; i++)                       // shift right
        { LCD_command(0x1C);
          Delay_ms(300);
        }


      Delay_ms(1000);
    }
}

 

 - 정수 데이터 출력 텍스트 lcd 프로그램

 

#include <avr/io.h>
#include "c:\AvrEdit\JJ128c\JJ128.h"

 

void LCD_2d(unsigned char number)               /* display 2-digit decimal number */
{ unsigned char i;

  i = number/10;                                // 10^1
  if(i == 0) LCD_data(' ');
  else       LCD_data(i + '0');

  i = number % 10;                              // 10^0
  LCD_data(i + '0');
}

 

void LCD_3d(unsigned int number)                /* display 3-digit decimal number */
{ unsigned int i;
  unsigned char flag;

  flag = 0;
  i = number/100;                               // 10^2
  if(i == 0) LCD_data(' ');
  else {     LCD_data(i + '0');
             flag = 1;
       }

  number = number % 100;                        // 10^1
  i = number/10;
  if((i == 0) && (flag == 0))
             LCD_data(' ');
  else {     LCD_data(i + '0');
             flag = 1;
       }

  i = number % 10;                              // 10^0
  LCD_data(i + '0');
}

 

void LCD_4d(unsigned int number)                /* display 4-digit decimal number */
{ unsigned int i;
  unsigned char flag;

  flag = 0;
  i = number/1000;                              // 10^3
  if(i == 0) LCD_data(' ');
  else {     LCD_data(i + '0');
             flag = 1;
       }

  number = number % 1000;                       // 10^2
  i = number/100;
  if((i == 0) && (flag == 0))
             LCD_data(' ');
  else {     LCD_data(i + '0');
             flag = 1;
       }

  number = number % 100;                        // 10^1
  i = number/10;
  if((i == 0) && (flag == 0))
             LCD_data(' ');
  else {     LCD_data(i + '0');
             flag = 1;
       }

  i = number % 10;                              // 10^0
  LCD_data(i + '0');
}

 

void LCD_2hex(unsigned char number)             /* display 2-digit hex number */
{ unsigned char i;

  i = (number >> 4) & 0x0F;                     // 16^1
  if(i <= 9) LCD_data(i + '0');
  else       LCD_data(i - 10 + 'A');

  i = number & 0x0F;                            // 16^0
  if(i <= 9) LCD_data(i + '0');
  else       LCD_data(i - 10 + 'A');
}

 

void LCD_8bin(unsigned char number)             /* display 8-bit binary number */
{
  LCD_data(((number >> 7) & 0x01) + '0');       // 2^7
  LCD_data(((number >> 6) & 0x01) + '0');       // 2^6
  LCD_data(((number >> 5) & 0x01) + '0');       // 2^5
  LCD_data(((number >> 4) & 0x01) + '0');       // 2^4
  LCD_data(((number >> 3) & 0x01) + '0');       // 2^3
  LCD_data(((number >> 2) & 0x01) + '0');       // 2^2
  LCD_data(((number >> 1) & 0x01) + '0');       // 2^1
  LCD_data((number & 0x01) + '0');              // 2^0
}

 

int main(void)
{ unsigned char i, j;

  MCU_initialize();                             // initialize MCU
  Delay_ms(50);                                 // wait for system stabilization
  LCD_initialize();                             // initialize text LCD module

  while(1)
    { LCD_string(0x80," INTEGER = 000  ");      // display 8-bit integer
      LCD_string(0xC0,"= 00H = 00000000");
      Beep();

      for(i=1; i>0; i++)                        // integer number from 1 to 255
        { LCD_command(0x8B);                    // decimal number
          LCD_3d((unsigned int)i);
          LCD_command(0xC2);                    // hexadecimal number
          LCD_2hex(i);
          LCD_command(0xC8);                    // binary number
          LCD_8bin(i);
          Delay_ms(500);
        }

      LCD_string(0x80," MULTI TABLE(1) ");      // display multiplication table(1)
      LCD_string(0xC0,"   0 x 0 = 00   ");
      Beep();

      for(i=2; i<=9; i++)
        for(j=1; j<=9; j++)
          { LCD_command(0xC3); LCD_data(i+'0'); // display multiplicand
            LCD_command(0xC7); LCD_data(j+'0'); // display multiplier
            LCD_command(0xCB); LCD_2d(i*j);     // display multiplication
            Delay_ms(1000);
          }

      LCD_string(0x80," MULTI TABLE(2) ");      // display multiplication table(2)
      LCD_string(0xC0," 00 x 00 = 0000 ");
      Beep();

      for(i=10; i<=90; i += 10)
        for(j=10; j<=90; j += 10)
          { LCD_command(0xC1); LCD_2d(i);       // display multiplicand
            LCD_command(0xC6); LCD_2d(j);       // display multiplier
            LCD_command(0xCB); LCD_4d(i*j);     // display multiplication
            Delay_ms(1000);
          }
    }
}

 

 

 

hc16202ny-ly-duzin.pdf

 

[첨부파일]  현대 lcd의 - HC16202NY-LY (datasheet 첨부)

반응형

'Embedded > Atmega128' 카테고리의 다른 글

c언어에서 printf 함수를 사용한 서식지정 출력  (0) 2012.09.01
128 인터럽트 uart통신소스  (0) 2012.09.01
LED 출력  (0) 2012.09.01
KEY 입력 기술  (0) 2012.09.01
AVR Atmega128 정복[책 소개 및 관련자료]  (0) 2012.09.01
Comments