임베디드시스템개론 -...

38
임베디드시스템개론 : Arduino 활용 Lecture #5: Text LCD 출력하기 2012. 4. 6 by 김영주

Transcript of 임베디드시스템개론 -...

임베디드시스템개론: Arduino 활용

Lecture #5: Text LCD 출력하기

2012. 4. 6 by 김 영 주

강의 목차

Text LCD 장치 개요간단한Text LCD 출력 테스트Text LCD 인터페이스 신호 줄이기아두이노 라이브러리 개요LiquidCrystal 라이브러리

2

Text LCD 출력 장치 (1)Text LCD 출력 장치

ASCII 코드를 입력받아 영문자를 출력하는 장치주로 16x2 LCD 사용 -16문자 x 2 줄 출력전력 소모가 적어 임베디드시스템에서 많이 사용

3

Text LCD 출력 장치 (2)Text LCD 내부 모듈 구조

LCD panel과 제어기(controller)가 하나의 모듈로 구성동작방식데이터 버스를 통해 제어기에 명령과 데이터를 전송

제어기는 명령에 따라 문자 코드에 해당하는 글씨체를 LCD panel로 출력

제어기는 LCD panel이 계속 출력상태를 유지하도록 제어(Driving)

4

Text LCD 출력 장치 (3)HD44780의 내부 블록도

5

Text LCD 출력 장치 (4)내부 레지스터

IR (Instruction Register) D.D.RAM과 C.G.RAM에 대한 주소 정보 또는 클리어, 커서이동, 글자 on/off 등LCD 제어에 대한 명령코드를 입력 받는 8-bit 레지스터RS(Register Select) 신호가 ‘L’일 때에 선택

DR(Data Register)D.D.RAM과 C.G.RAM에 데이터를 읽거나 쓸 때 사용되는 8-bit 레지스터RS 신호가 ‘H’ 일 때에 선택

BF(Busy Flag)연속적으로 LCD 모듈 제어 명령이 입력 될 때에 LCD 모듈이 처리할 수 있는지를 나타내는 상태 표시 1-bit 플래그IR 레지스터 읽기를 통해 접근 가능

AC(Address Counter)D.D.RAM과 C.G.RAM 의 주소를 지정할 때 사용현재 문자가 출력될 위치(cursor)를 의미함

6

Text LCD 출력 장치 (5)내부 메모리

D.D.RAM(Display Data RAM)8비트 문자코드의 디스플레이 데이터를 저장하는 메모리

최대용량은 80x8비트로 80문자를 저장 가능

행별로 40 문자의 데이터를 저장

C.G.ROM(Character Generator ROM)ASCII 문자의 글씨체 정보를 저장하고 있는 ROM

C.G.RAM(Character Generator RAM)사용자 정의 글씨체를 다운로드하여 저장하는 RAM

ASCII 문자 이외의 사용자 정의 문자를 출력할 때에 사용

7

Text LCD 출력 장치 (6)외부 인터페이스 신호 사양

8

Text LCD 출력 장치 (7)Text LCD 동작 명령과 인터페이스 신호

9

Text LCD 출력 장치 (8)Text LCD 동작 명령

10

Text LCD 출력 장치 (9)Text LCD 문자 출력

출력 위치를 고려하여 D.D.RAM의 원하는 위치를 지정하고(IR 레지스터), 출력할 문자의 ASCII 코드를 전송한다(DR 레지스터).D.D.RAM은 출력할 문자들의 코드값을 저장하는 버퍼 역할을 수행

최대 80개의 문자를 저장(80 bytes), 라인별로 40 bytes씩 분리하여 할당

예: 16문자 2라인 LCD의 경우

AC 레지스터는 현재 문자를 출력할 D.D.RAM 주소를 저장, 자동 증가

문자 출력 예:1행 16열에 ‘A’ 출력: 0x81 IR, 0x41 DR

2행 1열에 ‘1’ 출력: 0xC0 IR, 0x31 DR

1행 4열부터 “abc” 출력 : 0x83 IR, 0x61 DR, 0x62 DR, 0x63 DR11

열위치-> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

1행 -> 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

2행 -> 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F

D.D.RAM의 주소(Hex)

··········

Text LCD 출력 장치 (10)사용자 정의 문자 출력하기1. 사용자 정의 문자 패턴의 크기 5x7 8 바이트 정보로 표현2. IR 레지스터에 C.G.RAM 주소 설정3. DR 레지스터에 문자 패턴 정보 1 바이트 설정4. 8 바이트 정보 입력이 완료될 때까지 2, 3번 과정 반복5. D.D.RAM에 출력하고자 하는 사용자 정의 문자의 코드(0x00~

0x07)을 설정6. 사용자 정의 문자 출력

12

Text LCD 출력 장치 (11)Text LCD의 초기화 과정

Function Set(이진수:001x xx00).Display ON/OFF Control(0000 1xxx) Setting.Entry Mode Set(0000 01xx).D.D.RAM 주소 Setting.문자 데이터를 연속으로 보낸다

13

Text LCD 출력 장치 (12)Text LCD 제어신호 타이밍

다음 동작 명령을 실행하려면 BF(Busy Flag)가 0이 될 때까지 대기데이터 I/O를 위한 파형 형태 :

E 신호 스트로브 HIGH 구간은 0.5[usec] 이상 유지

14

Text LCD 간단 출력 테스트 (1)실험 개요

Arduino-Mega에 Text LCD 장치를 연결하여 테스트 문장을 출력한다.

사전 요구사항Text LCD 장치의 인터페이스 신호 규격을 정확히 이해한다.Text LCD 장치에서의 문자 출력 방법을 이해한다.Text LCD 장치의 제어 신호의 타이밍도를 이해한다.Arduino-Mega와 Text LCD 장치의 연결 방법을 정한다.

15

Text LCD 간단 출력 테스트 (2)회로도 및 회로 구성

회로도 :

16

Text LCD 간단 출력 테스트 (3)회로도 및 회로 구성

회로 구성:

17

Text LCD 간단 출력 테스트 (4)아두이노 프로그램 : “Hello, World!” 출력 프로그램

18

uint8_t _rs_pin; // LOW: command. HIGH: character.uint8_t _rw_pin; // LOW: write to LCD. HIGH: read from LCD.uint8_t _enable_pin; // activated by a HIGH pulse.uint8_t _data_pins[8];

void lcd_init(int rs, int rw, int enable, int* data, int data_cnt) {int i;

_rs_pin = rs;_rw_pin = rw;_enable_pin = enable;

for (i=0; i<data_cnt; i++)_data_pins[i] = data[i];

while (i < 8) {_data_pins[i] = 0;i++;

}

pinMode(_rs_pin, OUTPUT);pinMode(_rw_pin, OUTPUT);pinMode(_enable_pin, OUTPUT);

// SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!// according to datasheet, we need at least 40ms after power rises above 2.7V// before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50delayMicroseconds(50000);

Text LCD 간단 출력 테스트 (5)아두이노 프로그램 (계속)

19

// Now we pull both RS and R/W low to begin commandsdigitalWrite(_rs_pin, LOW);digitalWrite(_rw_pin, LOW);digitalWrite(_enable_pin, LOW);

// Send function set command sequencelcd_command(0x20 | 0x10 | 0x08);delayMicroseconds(4500); // wait more than 4.1ms

// second trylcd_command(0x20 | 0x10 | 0x08);delayMicroseconds(150);

// third golcd_command(0x20 | 0x10 | 0x08);

// finally, set # lines, font size, etc.lcd_command(0x20 | 0x10 | 0x08);

// turn the display on with no cursor or blinking defaultlcd_command(0x08 | 0x04);

// clear it offlcd_command(0x01); // clear display, set cursor position to zerodelayMicroseconds(2000); // this command takes a long time!

Text LCD 간단 출력 테스트 (6)아두이노 프로그램 (계속)

20

// Initialize to default text direction (for romance languages)lcd_command(0x04 | 0x02);delayMicroseconds(2000); // this command takes a long time!

}void lcd_print(char *str) {

while (*str != NULL) {lcd_data(*str);str++;

} }

void lcd_command(uint8_t value) {lcd_send(value, LOW);

}

void lcd_data(uint8_t value) {lcd_send(value, HIGH);

}

void lcd_send(uint8_t value, uint8_t mode) {digitalWrite(_rs_pin, mode);

digitalWrite(_rw_pin, LOW);

lcd_write8bits(value); }

Text LCD 간단 출력 테스트 (7)아두이노 프로그램 (계속)

21

void lcd_pulseEnable(void) {digitalWrite(_enable_pin, LOW);delayMicroseconds(1); digitalWrite(_enable_pin, HIGH);delayMicroseconds(1); // enable pulse must be >450nsdigitalWrite(_enable_pin, LOW);delayMicroseconds(100); // commands need > 37us to settle

}

void lcd_write4bits(uint8_t value) {for (int i = 0; i < 4; i++) {

pinMode(_data_pins[i], OUTPUT);digitalWrite(_data_pins[i], (value >> i) & 0x01);

}

lcd_pulseEnable();}

void lcd_write8bits(uint8_t value) {for (int i = 0; i < 8; i++) {

pinMode(_data_pins[i], OUTPUT);digitalWrite(_data_pins[i], (value >> i) & 0x01);

}

lcd_pulseEnable();}

Text LCD 간단 출력 테스트 (8)아두이노 프로그램 (계속)

22

void setup() {int d[8] = { 26, 27, 28, 29, 30, 31, 32, 33 };lcd_init(22, 23, 24, d, 8);

lcd_print("Hello, World!...");}

void loop() {// display-on | cursor-on | blink-offlcd_command(0x08 | 0x04 | 0x02);delay(5000);

// display-on | cursor-on | blink-onlcd_command(0x08 | 0x04 | 0x02 | 0x01);delay(5000);

// display-on | cursor-off | blink-offlcd_command(0x08 | 0x04);delay(5000);

}

Text LCD 간단 출력 테스트 (9)실험 결과

Text LCD 장치에 출력되는 내용이 좌 또는 우로 이동(흐름)하도록 수정하여 보아라.

23

Text LCD 간단 출력 테스트 (10)실험 개선

Text LCD 장치의 인터페이스 신호를 줄이는 방법은?4-bit data 통신 모드 사용

출력 전용 동작 지정

24

Text LCD의 Nibble Interface Mode (1)실험 개요

Text LCD 장치의 Nibble 데이터 인터페이스 모드를 사용하여 인터페이스 신호 수를 줄이도록 한다.

사전 요구사항회로 구성은 앞의 실험과 동일하나 인터페이스 신호 연결은 다음과 같은 사항이 바뀐다.

Text LCD Arduino Mega

---------------------------------------------------------------------

RS 22 pin

RW GND (Write Only)

Enable 24 pin

d4 30 pin

d5 31 pin

d6 32 pin

d7 33 pin25

Text LCD의 Nibble Interface Mode (2)사전 요구사항 (계속)

Text LCD 장치는 데이터 인터페이스 모드에 따라 초기화 과정이달라진다.데이터 인터페이스 모드가 Nibble 모드인 경우, 명령이나 데이터를 4-bit씩 2번에 나누어 전송하여야 한다.

26

Text LCD의 Nibble Interface Mode (3)아두이노 프로그램 : “Hello, World!” 출력 프로그램 수정

27

uint8_t _rs_pin; // LOW: command. HIGH: character.uint8_t _rw_pin; // LOW: write to LCD. HIGH: read from LCD.uint8_t _enable_pin; // activated by a HIGH pulse.uint8_t _data_pins[8];

#define BYTE_MODE 0#define NIBBLE_MODE 1uint8_t _bit_if_mode = BYTE_MODE;

void lcd_init(int rs, int rw, int enable, int* data, int data_cnt) {int i;uint8_t cmd;

_rs_pin = rs;_rw_pin = rw;_enable_pin = enable;

for (i=0; i<data_cnt; i++)_data_pins[i] = data[i];

while (i < 8) {_data_pins[i] = 0;i++;

}

if (data_cnt < 8)_bit_if_mode = NIBBLE_MODE;

Text LCD의 Nibble Interface Mode (4)아두이노 프로그램 (계속)

28

pinMode(_rs_pin, OUTPUT);// pinMode(_rw_pin, OUTPUT);

pinMode(_enable_pin, OUTPUT);

// SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!// according to datasheet, we need at least 40ms after power rises above 2.7V// before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50delayMicroseconds(50000);

// Now we pull both RS and R/W low to begin commandsdigitalWrite(_rs_pin, LOW);

// digitalWrite(_rw_pin, LOW);digitalWrite(_enable_pin, LOW);

if (_bit_if_mode == NIBBLE_MODE) { // we start in 8bit mode, try to set 4 bit modelcd_write4bits(0x03);delayMicroseconds(4500); // wait min 4.1ms

// second trylcd_write4bits(0x03);delayMicroseconds(4500); // wait min 4.1ms

// third go!lcd_write4bits(0x03); delayMicroseconds(150);

Text LCD의 Nibble Interface Mode (5)아두이노 프로그램 (계속)

29

// finally, set to 4-bit interfacelcd_write4bits(0x02);

cmd = 0x20 | 0x08; }else {

// Send function set command sequencecmd = 0x20; // function set commandcmd |= (0x10 | 0x08); // 8bit I/F & 2-line & 5x8 font

lcd_command(cmd);delayMicroseconds(4500); // wait more than 4.1ms

// second trylcd_command(cmd);delayMicroseconds(150);

// third golcd_command(cmd);

}

// finally, set # lines, font size, etc.lcd_command(cmd);

:::

Text LCD의 Nibble Interface Mode (6)아두이노 프로그램 (계속)

30

::

void lcd_send(uint8_t value, uint8_t mode) {digitalWrite(_rs_pin, mode);

// digitalWrite(_rw_pin, LOW);

if (_bit_if_mode == BYTE_MODE) {lcd_write8bits(value);

} else {lcd_write4bits(value>>4);lcd_write4bits(value);

}}

::

Text LCD의 Nibble Interface Mode (7)아두이노 프로그램 (계속)

31

:::

void setup() {// int d1[8] = { 26, 27, 28, 29, 30, 31, 32, 33 };

int d2[4] = { 30, 31, 32, 33 };

// lcd_init(22, 23, 24, d1, 8);lcd_init(22, 23, 24, d2, 4);

lcd_print("Hello, World!....");}

::

Text LCD의 Nibble Interface Mode (8)실험 결과 및 개선

출력 속도에는 차이가 있는가?

32

아두이노 라이브러리 (1)아두이노 라이브러리

빠른 프로토타이핑(Prototyping)을 지원하기 위해 일반적으로 많이사용하는 장치에 대한 인터페이스 프로그램을 라이브러리로 제공

http://arduino.cc/en/Reference/Libraries

아두이노 표준 라이브러리:EEPROM - reading and writing to "permanent" storage Ethernet - for connecting to the internet using the Arduino Ethernet Shield Firmata - for communicating with applications on the computer using a standard serial protocol. LiquidCrystal - for controlling liquid crystal displays (LCDs) SD - for reading and writing SD cards Servo - for controlling servo motors SPI - for communicating with devices using the Serial Peripheral Interface (SPI) Bus SoftwareSerial - for serial communication on any digital pins Stepper - for controlling stepper motors Wire - Two Wire Interface (TWI/I2C) for sending and receiving data over a net of devices or sensors.

33

아두이노 라이브러리 (2)아두이노 라이브러리 (계속)

라이브러리는 예제 프로그램을 같이 제공하여 프로그램 작성 시에참고하도록 함.표준 라이브러리 이외의 추가적인 라이브러리는 아두이노 사이트에서 확인할 수 있으며, 라이브러리 제공 사이트에 다운받아 설치하여 사용할 수 있다.

34

아두이노 라이브러리 (3)LyquidCrystal 라이브러리

Text LCD 장치를 지원하는 아두이노 표준 라이브러리Hitachi HD44780 (or a compatible) chipset 장착 LCD 제어4- or 8-bit 데이터 인터페이스 모드 지원

주요 함수:LiquidCrystal() , begin() , clear(), home(), setCursor() , write() , print() , cursor() , noCursor() , blink() , noBlink() , display() , noDisplay() , scrollDisplayLeft() , scrollDisplayRight() , autoscroll() , noAutoscroll() , leftToRight() , rightToLeft() , createChar()

35

아두이노 라이브러리 (4)LyquidCrystal 라이브러리 예제 : HelloWorld

36

// include the library code:#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pinsLiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {// set up the LCD's number of columns and rows: lcd.begin(16, 2);// Print a message to the LCD.lcd.print("hello, world!");

}

void loop() {// set the cursor to column 0, line 1// (note: line 1 is the second row, since counting begins with 0):lcd.setCursor(0, 1);// print the number of seconds since reset:lcd.print(millis()/1000);

}

아두이노 라이브러리 (5)LyquidCrystal 라이브러리 활용

LC 라이브러리에서 제공하는 다양한 예제 프로그램을 테스트하여 사용법을 확인하기 바람.활용 예:

http://tronixstuff.wordpress.com/2011/01/08/tutorial-arduino-and-monochrome-lcds/

37

과제물 #2과제 내용1. 조도 센서 입력를 받아 현재의 조도 크기를Text LCD 장치에 출력

하는 프로그램을 작성하여라.

2. 학기말 과제에 대한 제안서를 제출하여라A. 학기말 과제 아이템 내용

B. 구현 일정

C. 팀원별 역할 분담

제출물회로도, 프로그램 소스, 실행 예(사진)학기말 과제 제안서 – 5분간 발표

제출일차주 수업 시간

38