NAM S B MDLAB Electronic Engineering Kangwon National
NAM S. B MDLAB. Electronic Engineering, Kangwon National University CH 4. ATMEGA 128 LCD 2015 -03 -16 1
NAM S. B MDLAB. Electronic Engineering, Kangwon National University 5. 1 LCD(Liquid Crystal Display) 1. 1 LCD 구조 • TYPE ① GRAPHIC TYPE ② CHARACTER TYPT ◈ LCD 구조 Glass Liquid crystal Segment electron Backplane electron Mirrored surface 2015 -03 -16 2 2
3 NAM S. B MDLAB. Electronic Engineering, Kangwon National University LCD(Liquid Crystal Display) ü LCD MODULE 구조 +5 V RS R/W’ E 2 4 5 6 D 0 -D 7 Data bus 7 -14 VL GND I/O b u f f e r LCD Instruction Register(IR) D 7 D 6 D 5 D 4 D 3 D 2 D 1 D 0 Data Register(DR) D 7 D 6 D 5 D 4 D 3 D 2 D 1 D 0 CG RAM DD RAM 3 1 Character type LCD 구조 • 5 x 8 dot, 5 x 10 dot display • 80 x 8 bit display RAM(maximum 80 character display • 240 pont, 9, 920 bit character generator ROM(5 x 8: 240, 5 x 10: 32) • 64 x 8 bit character generator RAM(8 character pont(5 x 8), 4 character pont(5 x 10) 2015 -03 -16 3
NAM S. B MDLAB. Electronic Engineering, Kangwon National University LCD(Liquid Crystal Display) ü LCD PIN 기능 Pin name 기능 1 Vdd GND 2 Vcc 5 V 3 Vo LCD 구동전압(휘도조정) 4 RS Register select : CPU에서 LCD 로 보내는 신호가 DATA 인가 혹은 CONTROL 신호인가를 제어. H(1) : data input(DR select), L(0) : Instruction input (IR select) 5 R/W 6 E 7 -14 D 0 -D 7 2015 -03 -16 L : CPU에서 LCD로 Data Write, H : LCD에서 CPU로 Data Read Write Enable 신호 H : Enable, L : Disable Data Bus 4 4
NAM S. B MDLAB. Electronic Engineering, Kangwon National University LCD(Liquid Crystal Display) ü LCD interface Control Pin (2) (1) LCD write, read cycle (CPU에서 LCD로 Write) 2015 -03 -16 5 5
NAM S. B MDLAB. Electronic Engineering, Kangwon National University 2015 -03 -16 6
NAM S. B MDLAB. Electronic Engineering, Kangwon National University 2015 -03 -16 7
8 NAM S. B MDLAB. Electronic Engineering, Kangwon National University LCD(Liquid Crystal Display) ü LCD interface Control (3) LCD Control 1) Register select RS R/W function 0 0 IR : control register write 0 1 Reading busy flag from D 7 1 0 Write data in to the DR 1 1 Read data from DR 2) Display data of RAM ① Matching the display device and the address of D. D. RAM Row num 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 LCD display LCD 1 00 01 02 0 3 04 05 06 07 08 09 0 A 0 B 0 C 0 D 0 E 0 F DD RAM add LCD 2 40 41 42 4 3 44 45 46 47 48 49 4 A 4 B 4 C 4 D 4 E 4 F DD RAM add 2015 -03 -16 8 그림 4 -37. LCD interface
9 NAM S. B MDLAB. Electronic Engineering, Kangwon National University LCD(Liquid Crystal Display) ü LCD interface Control ② Matching the display device and the address of D. D. RAM for SHIFT LEFT Row num 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 LCD display LCD 1 01 02 03 0 4 05 06 07 08 09 0 A 0 B 0 C 0 D 0 E 0 F 10 DD RAM add LCD 2 41 42 43 4 4 45 46 47 48 49 4 A 4 B 4 C 4 D 4 E 4 F 50 DD RAM add ③ Matching the display device and the address of D. D. RAM for SHIFT RIGHT Row num 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 LCD display LCD 1 3 F 00 01 02 03 04 05 06 07 08 09 0 A 0 B 0 C 0 D 0 E DD RAM add LCD 2 7 F 40 41 42 43 44 45 46 47 48 49 4 A 4 B 4 C 4 D 4 E DD RAM add ◈ Character generator ROM • 208개의 5 x 8 dot or 32개의 5 x 10 dot character pattern generation • ASCII code 와 동일함 2015 -03 -16 9
NAM S. B MDLAB. Electronic Engineering, Kangwon National University LCD(Liquid Crystal Display) ü LCD instruction ① LCD Control Word Instruction RS R/ W D 7 D 6 D 5 D 4 D 3 D 2 D 1 D 0 CLR Display 0 0 0 0 0 1 Return Home 0 0 0 0 1 * Entry Mode 0 0 0 0 1 I/D S Display On/Off 0 0 0 1 D C B Cursor Shifter 0 0 0 1 S/C R/L * * FUNTION set 0 0 1 DL N F * * Swt CG RAM Add 0 0 1 WRITE DATA 1 0 Write data READ DATA 1 1 Read data ADDRESS ②Clear display RS R/W D 7 D 6 D 5 D 4 D 3 D 2 D 1 D 0 0 0 0 0 1 Control word = 01 H 2015 -03 -16 10 10
NAM S. B MDLAB. Electronic Engineering, Kangwon National University LCD(Liquid Crystal Display) ü LCD instruction ③ Cursor Home RS R/W D 7 D 6 D 5 D 4 D 3 D 2 D 1 D 0 0 0 0 0 1 * Control word = 02 H ④Entry mode set RS R/W D 7 D 6 D 5 D 4 D 3 D 2 D 1 D 0 0 0 0 1 I/D S I/D : 1: increment , 0: decrement S (shift) : 0: no shift 1(S), 0(I/D): Shift Right from current display point 1(S), 1(I. /D): Shift Left from current display point 2015 -03 -16 11 11
NAM S. B MDLAB. Electronic Engineering, Kangwon National University LCD(Liquid Crystal Display) ü LCD instruction ⑤ Display On/Off Control RS R/W D 7 D 6 D 5 D 4 D 3 D 2 D 1 D 0 0 0 0 1 D C B D : 1: Display On 0: Display Off C : 1: Curser On 0: Curser Off B: 1: Cursor위의 문자가 깜빡 거린다 0: Cursor위의 문자가 깜빡 거지 않는다. 1(S), 0(I/D): Shift Right from current display point 1(S), 1(I. /D): Shift Left from current display point ⑥ Cursor Display Shift RS R/W D 7 D 6 D 5 D 4 D 3 D 2 D 1 D 0 0 0 1 S/C R/L * * S/C=0, ++1 S/C=1, 2015 -03 -16 R/L =0 : Cursor가 왼쪽으로 이동한다, 이때 address counter --1 R/L =1 : Cursor가 오른쪽으로 이동한다, 이때 address counter R/L =0 : 전체 display 와 Cursor가 왼쪽으로 이동한다 R/L =1 : 전체 display 와 Cursor가 오른쪽으로 이동한다 12 12
NAM S. B MDLAB. Electronic Engineering, Kangwon National University LCD(Liquid Crystal Display) ü LCD instruction ⑦ Function Set RS R/W D 7 D 6 D 5 D 4 D 3 D 2 D 1 D 0 0 0 1 DL N F * * DL : 1: 8 Bit D 3 D 2 D 1 D 0 0: 4 Bit (Data bus interface) N : 1: 2 Line 0: 1 Line F : 1: 5*10 dot 0: 5*8 dot ⑧ C. G. RAM address select RS R/W D 7 D 6 0 0 0 1 D 5 D 4 ACG ⑨D. D. RAM address select RS R/W D 7 0 0 1 2015 -03 -16 D 5 D 4 D 3 Address 13 D 2 13
NAM S. B MDLAB. Electronic Engineering, Kangwon National University PORT A: LCD Port design 4 Bit control 방식 이용 PA 0 PA 1 PA 2 PA 4 PA 5 PA 6 PA 7 2015 -03 -16 14
NAM S. B MDLAB. Electronic Engineering, Kangwon National University 2015 -03 -16 15
NAM S. B MDLAB. Electronic Engineering, Kangwon National University Ex 5_1. Kangwon University Nam si Byung /********************** ex 5_1 ***********************/ #include <mega 128. h> #include <delay. h> // Alphanumeric LCD Module functions #asm. equ __lcd_port=0 x 1 B ; PORTA #endasm #include <lcd. h> void main(void) { // LCD module initialization lcd_init(16); 2015 -03 -16 display while (1) { lcd_clear(); delay_us(400); lcd_gotoxy(0, 0); lcd_putsf("Kangwon University"); delay_us(400); lcd_gotoxy(0, 1); lcd_putsf("Nam Si Byung"); delay_us(400); delay_us(3000); } }; 16
NAM S. B MDLAB. Electronic Engineering, Kangwon National University LCD. h #ifndef _LCD_INCLUDED_ #define _LCD_INCLUDED_ #pragma used+ void _lcd_ready(void); void _lcd_write_data(unsigned char data); // write a byte to the LCD character generator or display RAM void lcd_write_byte(unsigned char addr, unsigned char data); // read a byte from the LCD character generator or display RAM unsigned char lcd_read_byte(unsigned char addr); // set the LCD display position x=0. . 39 y=0. . 3 void lcd_gotoxy(unsigned char x, unsigned char y); // clear the LCD void lcd_clear(void); void lcd_putchar(char c); // write the string str located in SRAM to the LCD void lcd_puts(char *str); // write the string str located in FLASH to the LCD void lcd_putsf(char flash *str); // initialize the LCD controller unsigned char lcd_init(unsigned char lcd_columns); #pragma used#pragma library lcd. lib #endif 2015 -03 -16 17
NAM S. B MDLAB. Electronic Engineering, Kangwon National University Delay. h // Code. Vision. AVR C Compiler // (C) 1998 -2000 Pavel Haiduc, HP Info. Tech S. R. L. #ifndef _DELAY_INCLUDED_ #define _DELAY_INCLUDED_ #pragma used+ void delay_us(unsigned int n); void delay_ms(unsigned int n); #pragma used#endif 2015 -03 -16 18
NAM S. B MDLAB. Electronic Engineering, Kangwon National University LCD. LIB /* LCD driver routines Code. Vision. AVR C Compiler (C) 1998 -2004 Pavel Haiduc, HP Info. Tech S. R. L. */ #asm. equ __lcd_direction=__lcd_port-1. equ __lcd_pin=__lcd_port-2. equ __lcd_rs=0. equ __lcd_rd=1. equ __lcd_enable=2. equ __lcd_busy_flag=7 #endasm #pragma used+ static unsigned char _base_y[4]={0 x 80, 0 xc 0}; unsigned char _lcd_x, _lcd_y, _lcd_maxx; #pragma usedstatic void _lcd_delay(void) { #asm ldi r 31, 15 __lcd_delay 0: dec r 31 brne __lcd_delay 0 #endasm } 2015 -03 -16 19
NAM S. B MDLAB. Electronic Engineering, Kangwon National University void _lcd_ready(void) { #asm in r 26, __lcd_direction andi r 26, 0 xf ; set as input out __lcd_direction, r 26 sbi __lcd_port, __lcd_rd ; RD=1 cbi __lcd_port, __lcd_rs ; RS=0 __lcd_busy: #endasm _lcd_delay(); #asm sbi __lcd_port, __lcd_enable ; EN=1 #endasm _lcd_delay(); #asm in r 26, __lcd_pin cbi __lcd_port, __lcd_enable ; EN=0 #endasm _lcd_delay(); #asm sbi __lcd_port, __lcd_enable ; EN=1 #endasm _lcd_delay(); #asm cbi __lcd_port, __lcd_enable ; EN=0 sbrc r 26, __lcd_busy_flag rjmp __lcd_busy #endasm } 2015 -03 -16 20
NAM S. B MDLAB. Electronic Engineering, Kangwon National University static void _lcd_write_nibble(void) { #asm andi r 26, 0 xf 0 or r 26, r 27 out __lcd_port, r 26 ; write sbi __lcd_port, __lcd_enable ; EN=1 #endasm _lcd_delay(); #asm cbi __lcd_port, __lcd_enable ; EN=0 #endasm _lcd_delay(); } void _lcd_write_data(unsigned char data) { #asm cbi __lcd_port, __lcd_rd ; RD=0 in r 26, __lcd_direction ori r 26, 0 xf 0 | (1<<__lcd_rs) | (1<<__lcd_rd) | (1<<__lcd_enable) ; set as output out __lcd_direction, r 26 in r 27, __lcd_port andi r 27, 0 xf ld r 26, y #endasm _lcd_write_nibble(); //RD=0, write MSN #asm ld r 26, y swap r 26 #endasm _lcd_write_nibble(); //write LSN #asm ; RD=1 2015 -03 -16 sbi __lcd_port, __lcd_rd #endasm} 21
NAM S. B MDLAB. Electronic Engineering, Kangwon National University /* write a byte to the LCD character generator or display RAM */ void lcd_write_byte(unsigned char addr, unsigned char data) { _lcd_ready(); _lcd_write_data(addr); _lcd_ready(); #asm sbi __lcd_port, __lcd_rs ; RS=1 #endasm _lcd_write_data(data); } static void _lcd_read_nibble(void) { #asm sbi __lcd_port, __lcd_enable ; EN=1 #endasm _lcd_delay(); #asm in r 30, __lcd_pin ; read cbi __lcd_port, __lcd_enable ; EN=0 #endasm _lcd_delay(); #asm andi r 30, 0 xf 0 #endasm } 2015 -03 -16 22
NAM S. B MDLAB. Electronic Engineering, Kangwon National University static unsigned char lcd_read_byte 0(void) { _lcd_delay(); _lcd_read_nibble(); // read MSN #asm mov r 26, r 30 #endasm _lcd_read_nibble(); // read LSN #asm cbi __lcd_port, __lcd_rd ; RD=0 swap r 30 or r 30, r 26 #endasm } /* read a byte from the LCD character generator or display RAM */ unsigned char lcd_read_byte(unsigned char addr) { _lcd_ready(); _lcd_write_data(addr); _lcd_ready(); #asm in r 26, __lcd_direction andi r 26, 0 xf ; set as input out __lcd_direction, r 26 sbi __lcd_port, __lcd_rs ; RS=1 #endasm return lcd_read_byte 0(); } 2015 -03 -16 23
NAM S. B MDLAB. Electronic Engineering, Kangwon National University /* set the LCD display position x=0. . 39 y=0. . 3 */ void lcd_gotoxy(unsigned char x, unsigned char y) { _lcd_ready(); // RS=0 _lcd_write_data(_base_y[y]+x); _lcd_x=x; _lcd_y=y; } // clear the LCD void lcd_clear(void) { _lcd_ready(); // RS=0 _lcd_write_data(2); // cursor home _lcd_ready(); _lcd_write_data(0 xc); // cursor off _lcd_ready(); _lcd_write_data(1); // clear _lcd_x=_lcd_y=0; } 2015 -03 -16 24
NAM S. B MDLAB. Electronic Engineering, Kangwon National University #pragma keep+ void lcd_putchar(char c) { #asm push r 30 push r 31 ld r 26, y set cpi r 26, 10 breq __lcd_putchar 1 clt #endasm ++_lcd_x; if (_lcd_x>_lcd_maxx) { #asm("__lcd_putchar 1: ") ++_lcd_y; lcd_gotoxy(0, _lcd_y); #asm("brts __lcd_putchar 0") }; #asm rcall __lcd_ready sbi __lcd_port, __lcd_rs ; RS=1 ld r 26, y st -y, r 26 rcall __lcd_write_data __lcd_putchar 0: pop r 31 pop r 30 #endasm } #pragma keep 2015 -03 -16 25
NAM S. B MDLAB. Electronic Engineering, Kangwon National University // write the string str located in SRAM to the LCD void lcd_puts(char *str) { char k; while (k=*str++) lcd_putchar(k); } // write the string str located in FLASH to the LCD void lcd_putsf(char flash *str) { char k; while (k=*str++) lcd_putchar(k); } static void _long_delay(void) { #asm clr r 26 clr r 27 __long_delay 0: sbiw r 26, 1 ; 2 cycles brne __long_delay 0 ; 2 cycles #endasm } 2015 -03 -16 26
NAM S. B MDLAB. Electronic Engineering, Kangwon National University static void _lcd_init_write(unsigned char data) { #asm cbi __lcd_port, __lcd_rd ; RD=0 in r 26, __lcd_direction ori r 26, 0 xf 7 ; set as output out __lcd_direction, r 26 in r 27, __lcd_port andi r 27, 0 xf ld r 26, y #endasm _lcd_write_nibble(); //RD=0, write MSN #asm sbi __lcd_port, __lcd_rd ; RD=1 #endasm } // initialize the LCD controller unsigned char lcd_init(unsigned char lcd_columns) { #asm cbi __lcd_port, __lcd_enable ; EN=0 cbi __lcd_port, __lcd_rs ; RS=0 #endasm 2015 -03 -16 27
NAM S. B MDLAB. Electronic Engineering, Kangwon National University _lcd_maxx=lcd_columns; _base_y[2]=lcd_columns+0 x 80; _base_y[3]=lcd_columns+0 xc 0; _long_delay(); _lcd_init_write(0 x 30); _long_delay(); _lcd_init_write(0 x 20); _long_delay(); _lcd_write_data(0 x 28); _long_delay(); _lcd_write_data(4); _long_delay(); _lcd_write_data(0 x 85); _long_delay(); #asm in r 26, __lcd_direction andi r 26, 0 xf ; set as input out __lcd_direction, r 26 sbi __lcd_port, __lcd_rd ; RD=1 #endasm if (lcd_read_byte 0()!=5) return 0; _lcd_ready(); _lcd_write_data(6); lcd_clear(); return 1; } 2015 -03 -16 28
29 NAM S. B MDLAB. Electronic Engineering, Kangwon National University Ex 5_2_LCD. 초기값 Key: PD 3 Key: PD 2 Key: PD 1 Key: PD 0 0 1 1 0 2015 -03 -16 PORTD (key 값 scan) PORTC (LED Display) PORTA(LCD display 01 Left ring counter XX LEFT ring Counter 현재 LED 값을 표시 0 02 right ring counter XX RIGHT ring Counter 현재 LED 값을 표시 0 0 04 Half Twinkle XX LED control system 현재 LED 값을 표시 0 0 08 Full Twinkle XX LED control system 현재 LED 값을 표시
- Slides: 29