개발자/Raspberry Pi

라즈베리파이 LCD 1602 사용하기 16x2 LCD Hello World using C

지구빵집 2016. 10. 20. 11:35
반응형

 

 

 

기상 관측소(Weather Station)을 만들기 위해 캐릭터 LCD 1602 를 라즈베리파이와 사용하기 위한 사용법입니다. 

 

16 by 2 Character LCD 의 기본적인 사양은 아래와 같습니다. 아두이노 실습에 사용한 프리젠테이션 이미지를 참고하시기 바랍니다.

 

 

16 * 2  - 1602 LCD

 

 

LCD 의 다양한 사용

위 그림에 사용한 명령어는 아두이노 명령어임. 

 

 

 

16x2 LCD pinout: 핀 할당은 아래와 같다.

 

1   = VSS (GND)

2   = VDD (VCC,5V)

3   = VE (connect with variable resistor)

4   = RS, Register Select

5   = RW, Read/Write

6   = E, Enable

7   = Data0, D0

8   = Data1, D1

9   = Data2, D2

10 = Data3, D3

11 = Data4, D4

12 = Data5, D5

13 = Data6, D6

14 = Data7, D7

15 = Backlight Anode (VCC)

16 = Backlight Cathode (GND)

 

보통 16x2 LCD 는 데이터 8개의 데이터 라인을 사용하지만 4 bit 모드로 4개의 데이터 라인을 사용하여 캐릭터를 표시할 수 있다. GPIO 사용 핀을 줄인 역할을 한다.

 

LCD 를 사용하기 전에 이미  GPIO 제어 라이브러리인 wiringPi 가 설치되어 있어야 한다. 와이어링 파이에 대한 자세한 설명과 사용법, 라이브러리 설치방법은

여기에서 확인한다. http://fishpoint.tistory.com/1814

 

 

PIN 연결은 아래와 같이 한다. 아래 라즈베리 파이는 초기 버전인데 라즈베리 파이2, 3 모두 잘 작동함을 확인했다.

 

 

 

 다 연결했다면 아래 명령으로 연결된 핀을 확인할 수 있다.

 

$gpio readall

 

 

 

 

LCD에 대한 GPIO 핀 매핑은 아래와 같다. 다른 핀을 연결해서 사용할 경우 소스코드에서 지정해 줘야 한다.

 

Pin 12 = wPi 1 = D7

Pin 16 = wPi 4 = D6

Pin 18 = wPi 5 = D5

Pin 22 = wPi 6 = D4

Pin 24 = wPi 10 = E

Pin 26 = wPi 11 = RS

 

 "HELLO WORLD"를 출력하기 위한 소스코드이다. wiringPi 와 LCD 사이에 핀을 지정해주는 코드가 초기부분에 있다.

 

편집과 수정 명령은

 

$nano lcd1602.c

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
$nano lcd1602.c
 
#include <wiringPi.h>           //WiringPi headers
#include <lcd.h>                //LCD headers from WiringPi
#include <stdio.h>              //Needed for the printf function below
 
//Pin numbers below are the WiringPi pin numbers
 
#define LCD_RS  11               //Register select pin
#define LCD_E   10               //Enable Pin
#define LCD_D4  6               //Data pin 4
#define LCD_D5  5               //Data pin 5
#define LCD_D6  4               //Data pin 6
#define LCD_D7  1               //Data pin 7
 
int main()
{
    int lcd;                //Handle for LCD
    wiringPiSetup();        //Initialise WiringPi
 
    //Initialise LCD(int rows, int cols, int bits, int rs, int enable, int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7)
    if (lcd = lcdInit (216,4, LCD_RS, LCD_E ,LCD_D4 , LCD_D5, LCD_D6,LCD_D7,0,0,0,0)){
            printf ("lcd init failed! \n");
            return -1 ;
    }
    lcdPosition(lcd,0,0);  //Position cursor on the first line in the first column
    lcdPuts(lcd, "HELLO WORLD");  //Print the text on the LCD at the current cursor postion
    getchar();                      //Wait for key press
}
 
cs

 

컴파일 후 실행하면 아름다운 "HELLO World" 메시지가 표시된다.  결과 사진을 첨부한다.

 

컴파일 할 때 중요한 점은 드시 wiringPiDev 를 라이브러리로 추가하여 컴파일 한다.

 

 $ gcc -o lcd1602 lcd1602.c -lwiringPi -lwiringPiDev

 

실행

 

 $ sudo ./lec1602

 

 

 

 

 

 

 

참고자료와 사이트

 

References

[1] http://www.raspberrypi-spy.co.uk/2012/07/16x2-lcd-module-control-using-python/

[2] http://ozzmaker.com/2013/10/04/interface-16x2-lcd-with-the-raspberry-pi/?utm_source=feedly

[3] http://www.instructables.com/id/How-to-drive-a-character-LCD-displays-using-DIP-sw/step2/HD44780-pinout/

[4] 사진과 소스코드 출처 http://kurup87.blogspot.kr/2014/09/16x2-lcd-hello-world-using-c-wiringpi.html

 

 

 

 

 

 

반응형