20
10 10주차 주차 문자 문자 LCD LCD의 인터페이스 인터페이스 회로 회로 및 구동 구동 함수 함수 Next-Generation Networks Lab.

1010주차주차 …contents.kocw.net/KOCW/document/2015/shinhan/leesangrok/10.pdf문자문자lcdlcd의의인터페이스인터페이스회로회로및및구동구동함수함수 Next-Generation

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 1010주차주차 …contents.kocw.net/KOCW/document/2015/shinhan/leesangrok/10.pdf문자문자lcdlcd의의인터페이스인터페이스회로회로및및구동구동함수함수 Next-Generation

1010주차주차문자문자 LCDLCD의의 인터페이스인터페이스 회로회로 및및 구동구동 함수함수

Next-Generation Networks Lab.

Page 2: 1010주차주차 …contents.kocw.net/KOCW/document/2015/shinhan/leesangrok/10.pdf문자문자lcdlcd의의인터페이스인터페이스회로회로및및구동구동함수함수 Next-Generation

5. 16x2 CLCD 모듈 (HY-1602H-803)

그림 11-18

19

Page 3: 1010주차주차 …contents.kocw.net/KOCW/document/2015/shinhan/leesangrok/10.pdf문자문자lcdlcd의의인터페이스인터페이스회로회로및및구동구동함수함수 Next-Generation

핀 설명 표 11-11

번호 분류 핀 이름 레벨 (V) 기능

1전원

VSS or GND 0 GND전원Power

2 VDD or VCC +5 CLCD 구동전원3 V0 - CLCD 명암 조절4 제어 신호 RS H/L HIGH: 데이터 전송, LOW: 명령어 전송제어 신호

Control Signal

/ 데이터 전송, 명령어 전송5 R/W H/L HIGH: 읽기, LOW: 쓰기6 EN H / H→L Data Enable 명령 입력 시마다 펄스 전송7 DB0 H/L 데이터 버스07

데이터 신호

DB0 H/L 데이터 버스08 DB1 H/L 데이터 버스19 DB2 H/L 데이터 버스2

데이터 신호Data Signal

10 DB3 H/L 데이터 버스311 DB4 H/L 데이터 버스4 / 4 bit 제어 시 사용12 DB5 H/L 데이터 버스5 / 4 bit 제어 시 사용데이터 버 제어 시 사13 DB6 H/L 데이터 버스6 / 4 bit 제어 시 사용14 DB7 H/L 데이터 버스7 / 4 bit 제어 시 사용15 후면 광원 A +5 후면광원 애노드 전원

20

15 후면 광원Back Light

A +5 후면광원 애노드 전원16 K 0 후면광원 캐소드 전원

Page 4: 1010주차주차 …contents.kocw.net/KOCW/document/2015/shinhan/leesangrok/10.pdf문자문자lcdlcd의의인터페이스인터페이스회로회로및및구동구동함수함수 Next-Generation

회로 구성

21

Page 5: 1010주차주차 …contents.kocw.net/KOCW/document/2015/shinhan/leesangrok/10.pdf문자문자lcdlcd의의인터페이스인터페이스회로회로및및구동구동함수함수 Next-Generation

sbisbi와와 cbicbi 레지스터 REG0의 Bit 7,4,1을 Set 한 후에 다시 Bit 4,1을 Clear

REG0 |= 0x92;

REG0 &= 0xED;

// BIT 7,4,1 Set

// BIT 4,1 Clear #define sbi(x,y) (x |= (1<<y)); // , #define sbi(x,y) (x | (1<<y))

#define cbi(x,y) (x &= ~(1<<y))

biREG0 |= 0x01<<7;

REG0 |= 0x01<<4;

// BIT 7 Set

// BIT 4 Set

sbi(REG0, 7);

sbi(REG0, 4);

bi(REG0 1)REG0 |= 0x01<<1;

REG0 &= ~(0x01<<4);

// BIT 1 Set

// BIT 4 Clear

sbi(REG0, 1);

cbi(REG0, 4);

cbi(REG0 1);REG0 &= ~(0x01<<1); // BIT 1 Clear

cbi(REG0, 1);

REG0 |= (0x01<<7) | (0x01<<4) | (0x01<<1)

REG0 &= ((0x01<<4) & (0x01<<1));

// BIT 7,4,1 Set

// BIT 4 1 Clear

22

REG0 &= ~((0x01<<4) & (0x01<<1)); // BIT 4,1 Clear

Page 6: 1010주차주차 …contents.kocw.net/KOCW/document/2015/shinhan/leesangrok/10.pdf문자문자lcdlcd의의인터페이스인터페이스회로회로및및구동구동함수함수 Next-Generation

함수함수 (function) (function) (5(5장장)) Call by value

Call by reference

함수 구조

Call by reference

함수 구조

function name : 함수 이름

argument : 함수로 입력되는 데이터형

return type : 함수가 돌려주는 데이터형

그림 5-6

23

Page 7: 1010주차주차 …contents.kocw.net/KOCW/document/2015/shinhan/leesangrok/10.pdf문자문자lcdlcd의의인터페이스인터페이스회로회로및및구동구동함수함수 Next-Generation

float my_func3 ( int x , char y )

그림 5-7

그림 5-8그림 5-9

void my func1 ( int x )

24

void my_func1 ( int x )float my_func2 ( void )

Page 8: 1010주차주차 …contents.kocw.net/KOCW/document/2015/shinhan/leesangrok/10.pdf문자문자lcdlcd의의인터페이스인터페이스회로회로및및구동구동함수함수 Next-Generation

CLCD CLCD 헤더헤더 파일파일

헤더파일에 포함된 기능

CLCD 인터페이스를 위한 하드웨어 설정 CLCD 인터페이스를 위한 하드웨어 설정

HD44780에서 지원되는 다양한 기능을 설정하기 위한 정의

CLCD 인터페이스를 위한 함수 구현

CLCD 인터페이스를 위한 함수

void clcd port init(void); CPU 포트 초기화 함수 void clcd_port_init(void); CPU 포트 초기화 함수

void clcd_init_8bit(void); CLCD 인터페이스 초기화 함수

void clcd_busy_check(void); CLCD의 상태 확인 함수

void clcd_command(INT8 cmd); 명령어 출력 함수

void clcd_data(INT8 data); 데이터 출력 함수

void clcd str(char *string); 문자열 출력 함수 void clcd_str(char *string); 문자열 출력 함수

void clcd_position(INT8 col, INT8 row); CLCD 위치 이동 함수

void clcd_clear(void); CLCD 화면 클리어 함수

25

Page 9: 1010주차주차 …contents.kocw.net/KOCW/document/2015/shinhan/leesangrok/10.pdf문자문자lcdlcd의의인터페이스인터페이스회로회로및및구동구동함수함수 Next-Generation

CLCD CLCD 헤더헤더 파일파일 (1) (1) -- 하드웨어하드웨어 설정설정

/* Header file for 8 bits Data Bus */

#define EN PC7 // Enable bit of Control Busd fi C6 d i bi f C l#define RW PC6 // Read/Write bit of Control Bus

#define RS PC5 // Reset bit of Control Bus

#define DATA BUS PORTA // 8 bits of Data Busdefine DATA_BUS PORTA // 8 bits of Data Bus#define CONTROL_BUS PORTC // 3 bits of Control Bus#define DATA_BUS_DIRECTION DDRA#define CONTROL_BUS_DIRECTION DDRC_ _

#define DATA_BUS_BUSY PINA // Busy Check at Data Bus#define BUSY_BIT 0x80 // Busy Check Bit --> Bit 7

#define sbi(reg, bit) reg |= (1<<(bit)) // Set "bit"th bit of Register "reg"#define cbi(reg, bit) reg &= ~(1<<(bit))

26

Page 10: 1010주차주차 …contents.kocw.net/KOCW/document/2015/shinhan/leesangrok/10.pdf문자문자lcdlcd의의인터페이스인터페이스회로회로및및구동구동함수함수 Next-Generation

CLCD CLCD 헤더헤더 파일파일 (2) (2) –– 함수함수 선언선언 및및 효과효과 설정설정

// Function Protocol 선언void clcd_port_init(void);void clcd init 8bit(void);void clcd_init_8bit(void);void clcd_busy_check(void);void clcd_command(INT8 cmd);void clcd_data(INT8 data);_ ( );void clcd_str(char *string);void clcd_position(INT8 col, INT8 row);void clcd_clear(void);

#define CMD_CLEAR_DISPLAY 0x01 // Clear All Display#define CMD_RETURN_HOME 0x02 // Cursor Position -> Return Home

#define CMD_ENTRY_MODE_SET_OPTION1 0x04 // 커서 좌측 이동, 화면이동 없음#define CMD_ENTRY_MODE_SET_OPTION2 0x05 // 커서 좌측 이동, 화면 이동#define CMD_ENTRY_MODE_SET_OPTION3 0x06 // * 커서 우측 이동, 화면이동 없음#define CMD_ENTRY_MODE_SET_OPTION4 0x07 // 커서 우측 이동, 화면이동

#define CMD_DISPLAY_OPTION1 0x08 // 화면 OFF, 커서 OFF, 커서 점멸 OFF #define CMD DISPLAY OPTION2 0x09 // 화면 OFF, 커서 OFF, 커서 점멸 ON#define CMD_DISPLAY_OPTION2 0x09 // 화면 OFF, 커서 OFF, 커서 점멸 ON

27

Page 11: 1010주차주차 …contents.kocw.net/KOCW/document/2015/shinhan/leesangrok/10.pdf문자문자lcdlcd의의인터페이스인터페이스회로회로및및구동구동함수함수 Next-Generation

헤더헤더 파일파일 (3) (3) -- 효과효과 설정설정

#define CMD_DISPLAY_OPTION3 0x0A // 화면 OFF, 커서 ON , 커서 점멸 OFF#define CMD_DISPLAY_OPTION4 0x0B // 화면 OFF, 커서 ON , 커서 점멸 ON#define CMD DISPLAY OPTION5 0x0C // 화면 ON , 커서 OFF, 커서 점멸 OFFdefine CMD_DISPLAY_OPTION5 0x0C // 화면 ON , 커서 OFF, 커서 점멸 OFF#define CMD_DISPLAY_OPTION6 0x0D // 화면 ON , 커서 OFF, 커서 점멸 ON#define CMD_DISPLAY_OPTION7 0x0E // 화면 ON , 커서 ON , 커서 점멸 OFF#define CMD_DISPLAY_OPTION8 0x0F // * 화면 ON , 커서 ON , 커서 점멸 ON_ _ // 화면 , 커서 , 커서 점멸

#define CMD_CURSOR_DISPLAY_SHIFT_OPTION1 0x10 // 커서 선택, 커서 좌측 이동#define CMD_CURSOR_DISPLAY_SHIFT_OPTION2 0x14 // 커서 선택, 커서 우측 이동#define CMD CURSOR DISPLAY SHIFT OPTION3 0x18 // 화면 선택 화면 좌측 이동#define CMD_CURSOR_DISPLAY_SHIFT_OPTION3 0x18 // 화면 선택, 화면 좌측 이동#define CMD_CURSOR_DISPLAY_SHIFT_OPTION4 0x1C // 화면 선택, 화면 우측 이동

#define CMD_FUNCTION_SET_OPTION1 0x20 // 4비트, 화면1행, 5x8 Font#define CMD_FUNCTION_SET_OPTION2 0x24 // 4비트, 화면1행, 5x11 Font#define CMD_FUNCTION_SET_OPTION3 0x28 // 4비트, 화면2행, 5x8 Font#define CMD_FUNCTION_SET_OPTION4 0x2C // 4비트, 화면2행, 5x11 Font#define CMD_FUNCTION_SET_OPTION5 0x30 // 8비트, 화면1행, 5x8 Font#define CMD_FUNCTION_SET_OPTION6 0x34 // 8비트, 화면1행, 5x11 Font#define CMD_FUNCTION_SET_OPTION7 0x38 // * 8비트, 화면2행, 5x8 Font#d fi CMD FUNCTION SET OPTION8 0 3C // 8비트 화면2행 5 11 F t#define CMD_FUNCTION_SET_OPTION8 0x3C // 8비트, 화면2행, 5x11 Font

28

Page 12: 1010주차주차 …contents.kocw.net/KOCW/document/2015/shinhan/leesangrok/10.pdf문자문자lcdlcd의의인터페이스인터페이스회로회로및및구동구동함수함수 Next-Generation

CLCD CLCD 초기화초기화 함수함수그림 11-12

Function set

void clcd_init_8bit(void){clcd command(CMD FUNCTION SET OPTION7);_ ( _ _ _ );

_delay_ms(5);

clcd_command(CMD_FUCNTION_SET_OPTION7);

Function set_delay_ms(1);

clcd_command(CMD_FUCNTION_SET_OPTION7);

clcd_command(CMD_FUCNTION_SET_OPTION7);

Function set

Function set

clcd_command(CMD_DISPLAY_OPTION8);

clcd command(CMD CLEAR DISPLAY);Function setDisplay onDisplay clear

clcd_command(CMD_CLEAR_DISPLAY);

clcd_command(CMD_ENTRY_MODE_SET_OPTION3);

29

Entry mode set( )

}

Page 13: 1010주차주차 …contents.kocw.net/KOCW/document/2015/shinhan/leesangrok/10.pdf문자문자lcdlcd의의인터페이스인터페이스회로회로및및구동구동함수함수 Next-Generation

BF BF bit bit 읽기읽기 함수함수, , 명령어명령어 쓰기쓰기 함수함수, , 데이터데이터 쓰기쓰기 함수함수

void clcd_busy_check(void) {

INT8 busy_flag=0;

void clcd_command(INT8 cmd){

clcd_busy_check();INT16 busy_time=0;

DATA_BUS_DIRECTION=0x00;

cbi(CONTROL_BUS, RS); //RS=0cbi(CONTROL_BUS, RW); //RW=0sbi(CONTROL_BUS, EN); //EN=1

do {

cbi(CONTROL_BUS, RS); //RS=0

DATA_BUS=cmd;_delay_us(10);cbi(CONTROL_BUS, EN); //EN=0

}sbi(CONTROL_BUS, RW); //RW=1sbi(CONTROL_BUS, EN); //EN=1_delay_us(10);b fl DATA BUS BUSY

}

void clcd_data(INT8 cmd){

busy_flag=DATA_BUS_BUSY;cbi(CONTROL_BUS, EN); //EN=0busy_time++;

} while ((busy flag & BUSY BIT)

clcd_busy_check();sbi(CONTROL_BUS, RS); //RS=1cbi(CONTROL_BUS, RW); //RW=0

} while ((busy_flag & BUSY_BIT) && (busy_time<500));

DATA BUS DIRECTION=0xFF;

sbi(CONTROL_BUS, EN); //EN=1DATA_BUS=cmd;_delay_us(10);bi CO O S 0DATA_BUS_DIRECTION=0xFF;

}cbi(CONTROL_BUS, EN); //EN=0

}30

Page 14: 1010주차주차 …contents.kocw.net/KOCW/document/2015/shinhan/leesangrok/10.pdf문자문자lcdlcd의의인터페이스인터페이스회로회로및및구동구동함수함수 Next-Generation

문자열문자열 쓰기쓰기 함수함수, , 포트포트 초기화초기화 함수함수, Cursor , Cursor 위치위치 함수함수

// *string: 출력할 문자열void clcd_str(char *string){

void clcd_str_delay(char *string){

hil (* t i ! '\0')while(*string != '\0'){

clcd_data(*string);i

while(*string != '\0'){

clcd_data(*string); _delay_ms(300);string++;string++;

}}

string++; }

}

void clcd_port_init(void) {

CONTROL BUS DIRECTION = 0xF0;

void clcd_position(INT8 col, INT8 row){

clcd command(0x80 | (col * 0x40 + row));CONTROL_BUS_DIRECTION = 0xF0;DATA_BUS_DIRECTION= 0xFF;CONTROL_BUS= 0x00;DATA BUS= 0x00;

clcd_command(0x80 | (col 0x40 + row));}

DATA_BUS= 0x00;} void clcd_clear(void)

{clcd_command(CMD_CLEAR_DISPLAY);( )

}

31

Page 15: 1010주차주차 …contents.kocw.net/KOCW/document/2015/shinhan/leesangrok/10.pdf문자문자lcdlcd의의인터페이스인터페이스회로회로및및구동구동함수함수 Next-Generation

DDDD--RAMRAM의의 주소주소 및및 Cursor Cursor 위치위치 함수함수 ((그림그림 1111--14)14)

// row 0:15 column 0:1// row 0:15 , column 0:1// position: DD-RAM의 주소

id l d iti (INT8 l INT8 )void clcd_position(INT8 col, INT8 row){

clcd_command(0x80 | (col * 0x40 + row));}

32

Page 16: 1010주차주차 …contents.kocw.net/KOCW/document/2015/shinhan/leesangrok/10.pdf문자문자lcdlcd의의인터페이스인터페이스회로회로및및구동구동함수함수 Next-Generation

Display position & DDDisplay position & DD--RAM RAM 주주소소 of 16x2of 16x2모듈모듈 ((그림그림 1111--14)14)

33

Page 17: 1010주차주차 …contents.kocw.net/KOCW/document/2015/shinhan/leesangrok/10.pdf문자문자lcdlcd의의인터페이스인터페이스회로회로및및구동구동함수함수 Next-Generation

예제예제

예제 11.1 ASCII 문자 출력 CLCD를 포트 A와 포트 C로 연결한다.

포트 A: 데이터 버스, 포트 C: 제어 버스

CG-ROM에 저장된 데이터를 활용하여 CLCD 화면에 “Love” 문자를 출력한다한다.

예제 11 2 문자열 출력 예제 11.2 문자열 출력 CLCD를 포트 A와 포트 C로 연결한다.

포트 A: 데이터 버스, 포트 C: 제어 버스데이터 버 , 제어 버

문자열 출력함수를 활용하여 CLCD 화면에 “Shinhan University” 문자열을 출력한다.

34

Page 18: 1010주차주차 …contents.kocw.net/KOCW/document/2015/shinhan/leesangrok/10.pdf문자문자lcdlcd의의인터페이스인터페이스회로회로및및구동구동함수함수 Next-Generation

CLCD 모듈CLCD 모듈(8비트)

Port A(PA0 ~ PA7)

Port C(PC5 ~ PC7)

CPU Board

35

Page 19: 1010주차주차 …contents.kocw.net/KOCW/document/2015/shinhan/leesangrok/10.pdf문자문자lcdlcd의의인터페이스인터페이스회로회로및및구동구동함수함수 Next-Generation

예제예제 1111--1 1 ASCII ASCII 문자문자 출력출력

#define F_CPU 1000000UL

#include <avr/io.h>#include <util/delay h>

while(1){

clcd_position(0,7);#include <util/delay.h>#include "clcd_D8.h"

int main (void){

clcd_data(0x4C);clcd_data(0x6F);clcd_data(0x76);

{clcd_port_init( );

clcd init 8bit( );

clcd_data(0x65);_delay_ms(2000);clcd_position(1,3);l d d t (‘L’)clcd_init_8bit( ); clcd_data(‘L’);

clcd_data(‘o’);clcd_data(‘v’);clcd data(‘e’);clcd_data( e );_delay_ms(2000);clcd_clear( );delay ms(2000);_delay_ms(2000);

}}

36

Page 20: 1010주차주차 …contents.kocw.net/KOCW/document/2015/shinhan/leesangrok/10.pdf문자문자lcdlcd의의인터페이스인터페이스회로회로및및구동구동함수함수 Next-Generation

예제예제 1111--2 2 문자열문자열 출력출력

#define F_CPU 1000000UL

#include <avr/io.h>while(1){include avr/io.h

#include <util/delay.h>#include "clcd_D8.h"

{clcd_position(0,0);clcd_str(data1);_delay_ms(500);

int main (void){

char data1[ ] = “!! Hello !!";

_ y_ ( );clcd_position(1,0);clcd_str(data2);_delay_ms(500);

char data2[ ] = “Shinhan University";

clcd_port_init( );

clcd_clear( );_delay_ms(2000);

}clcd_init_8bit( ); return 0;

}

37