개발자/Raspberry Pi

라즈베리 파이 LCD display C 언어

지구빵집 2017. 2. 10. 11:46
반응형

 

 

 

똑 같은 내용으로 파이썬 프로그래밍과 소스코드는 이곳을 참조한다. 

 

http://www.circuitbasics.com/raspberry-pi-lcd-set-up-and-programming-in-python

 

Wiring the LCD in 8 Bit Mode 연결

Wiring the LCD in 4 Bit Mode 연결

 

 

 

위 연결도의 밝기와 선명도 조절 가변저항은 10K, 저항이나 1K~3K 저항을 사용하면 된다.

 

 wiringPi 설치

 

C언어를 사용하여 라즈베리 파이를 프로그래밍 하기 위해 wiringPi 라이브러리를 설치한다. 이미 설치되어 있다면 건너 뛴다.

아래 명령어를 사용하여 wiringPi 를 다운받아 컴파일한다. 

 

1. Git 을 사용하여 WiringPi 를 설치할 수 있는 git-core 설치

$sudo apt-get install git-core

 

만약 에러가 난다면 다음 명령어를 사용하여 업데이트를 한다.

$sudo apt-get update

 

2. WiringPi 를 다운로드

$git clone git://git.drogon.net/wiringPi

 

3. 체인지 디렉토리 명령

$cd wiringPi

 

4. 설치 명령을 입력

$./build

 

5. 설치가 잘 되었는지 아래 명령어로 확인

$gpio -v

$gpio readall

 

 

C로 만들어진 LCD 제어 프로그램의 컴파일과 실행방법 - 자신의 프로그램이 example.c 라 하면

 

컴파일

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

 

실행

$sudo ./example

 

 

Write to Display in 8 Bit Mode 

LCD 화면에 "Hello, World!" 하고 표시한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <wiringPi.h>
#include <lcd.h>
 
//USE WIRINGPI PIN NUMBERS
#define LCD_RS  25               //Register select pin
#define LCD_E   24               //Enable Pin
#define LCD_D0  29               //Data pin D0
#define LCD_D1  28               //Data pin D1
#define LCD_D2  27               //Data pin D2
#define LCD_D3  26               //Data pin D3
#define LCD_D4  23               //Data pin D4
#define LCD_D5  22               //Data pin D5
#define LCD_D6  21               //Data pin D6
#define LCD_D7  14               //Data pin D7
 
int main()
{
    int lcd;
    wiringPiSetup();
    lcd = lcdInit (2168, LCD_RS, LCD_E, LCD_D0, LCD_D1, LCD_D2, LCD_D3, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
 
    lcdPuts(lcd, "Hello, world!");
}
 
cs
 
여기서 LCD 를 초기화 시키는 함수를 살펴보면 
 

lcd = lcdInit (ROWS, COLUMNS, BIT MODE, LCD_RS, LCD_E, LCD_D0, LCD_D1, LCD_D2, LCD_D3, LCD_D4, LCD_D5, LCD_D6, LCD_D7);

 

이런 인자들로 구성됨을 확인한다.

 

 

Write to Display in 4 Bit Mode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <wiringPi.h>          
#include <lcd.h>               
 
//USE WIRINGPI PIN NUMBERS
#define LCD_RS  25               //Register select pin
#define LCD_E   24               //Enable Pin
#define LCD_D4  23               //Data pin 4
#define LCD_D5  22               //Data pin 5
#define LCD_D6  21               //Data pin 6
#define LCD_D7  14               //Data pin 7
 
int main()
{
    int lcd;               
    wiringPiSetup();        
    lcd = lcdInit (2164, LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7, 0000);
               
    lcdPuts(lcd, "Hello, world!");   
}
cs

 

 
 

Position the Text

LCD 에서 위치 변경을 위해 lcdPosition(lcd, COLUMN, ROW) 함수를 사용한다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <wiringPi.h>           
#include <lcd.h>                
 
//USE WIRINGPI PIN NUMBERS
#define LCD_RS  25               //Register select pin
#define LCD_E   24               //Enable Pin
#define LCD_D4  23               //Data pin 4
#define LCD_D5  22               //Data pin 5
#define LCD_D6  21               //Data pin 6
#define LCD_D7  14               //Data pin 7
 
int main()
{
    int lcd;               
    wiringPiSetup();        
    lcd = lcdInit (2164, LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7, 0000);
    
    lcdPosition(lcd, 31); 
    lcdPuts(lcd, "Hello, world!");   
}
 
cs
 

Clear the Screen

LCD 를 클리어 하고 커서를 윗줄 첫째 간에 위치한다
 
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
#include <wiringPi.h>          
#include <lcd.h>               
 
//USE WIRINGPI PIN NUMBERS
#define LCD_RS  25               //Register select pin
#define LCD_E   24               //Enable Pin
#define LCD_D4  23               //Data pin 4
#define LCD_D5  22               //Data pin 5
#define LCD_D6  21               //Data pin 6
#define LCD_D7  14               //Data pin 7
 
int main()
{
    int lcd;               
    wiringPiSetup();        
    lcd = lcdInit (2164, LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7, 0000);
               
    lcdPuts(lcd, "This is how you"); 
    sleep(2);
    lcdClear(lcd);  
 
    lcdPuts(lcd, "clear the screen"); 
    sleep(2);
    lcdClear(lcd);
}
cs
 
 

Blinking Text

블링크 효과는 반복된 루프에서 lcdclear() 함수와 lcdputs() 함수를 사용해서 구현한다.
 
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
#include <wiringPi.h>
#include <lcd.h>
 
//USE WIRINGPI PIN NUMBERS
#define LCD_RS  25               //Register select pin
#define LCD_E   24               //Enable Pin
#define LCD_D4  23               //Data pin 4
#define LCD_D5  22               //Data pin 5
#define LCD_D6  21               //Data pin 6
#define LCD_D7  14               //Data pin 7
 
int main()
{
    int lcd;
    wiringPiSetup();
    lcd = lcdInit (2164, LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7, 0000);
 
    while(1){
        lcdPosition(lcd, 00);
        lcdPuts(lcd, "Hello, world!");
        sleep(2);
        lcdClear(lcd);
        sleep(2);
        }
}
cs
 
 

Cursor On/Off

커서는 기본적으로 꺼져있다. 아래 함수를 사용해서 커서 스타일을 선택하여 사용할 수 있다.
 
  • Underline non-blinking cursor: lcdCursor(lcd, 1)
  • Underline blinking cursor: lcdCursor(lcd, 1), followed by lcdCursorBlink(lcd, 1)
  • Blinking block style cursor: lcdCursorBlink(lcd, 1)
  • Cursor off : lcdCursor(lcd, 0)
 
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
#include <wiringPi.h>          
#include <lcd.h>               
 
//USE WIRINGPI PIN NUMBERS
#define LCD_RS  25               //Register select pin
#define LCD_E   24               //Enable Pin
#define LCD_D4  23               //Data pin 4
#define LCD_D5  22               //Data pin 5
#define LCD_D6  21               //Data pin 6
#define LCD_D7  14               //Data pin 7
 
int main()
{
    int lcd;
    wiringPiSetup();
    lcd = lcdInit (2164, LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7, 0000);
 
    //lcdCursor(lcd, 0); //Cursor OFF
   
    lcdCursor(lcd, 1); //Cursor ON, underline, not blinking
    //lcdCursorBlink(lcd, 1); //Cursor ON, block, blinking    
    
    //Use both lines below to get a blinking underline/block cursor 
    //lcdCursor(lcd, 1);
    //lcdCursorBlink(lcd, 1);
 
    lcdPuts(lcd, "Hello, world!");
}
cs
 
 

Print the Date and Time

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
31
32
33
34
35
36
37
38
39
40
#include <wiringPi.h>         
#include <lcd.h>               
#include <stdio.h>
#include <time.h>
 
//USE WIRINGPI PIN NUMBERS
#define LCD_RS  25               //Register select pin
#define LCD_E   24               //Enable Pin
#define LCD_D4  23               //Data pin 4
#define LCD_D5  22               //Data pin 5
#define LCD_D6  21               //Data pin 6
#define LCD_D7  14               //Data pin 7
 
int main()
{
    int lcd;
    wiringPiSetup();
    lcd = lcdInit (2164, LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7, 0000);
 
    while(1){
        time_t timer;
        char buffer_date[26];
        char buffer_time[26];
        struct tm* tm_info;
 
        time(&timer);
        tm_info = localtime(&timer);
 
        strftime(buffer_date, 26"Date: %m:%d:%Y", tm_info);
        strftime(buffer_time, 26"Time: %H:%M:%S", tm_info);
 
        lcdPosition(lcd, 00);
        lcdPuts(lcd, buffer_date);
 
        lcdPosition(lcd, 01);
        lcdPuts(lcd, buffer_time);
 
        }
}
 
cs
 

Print Your IP Address

이더넷 연결에 대한 IP 주소를 나타낸다. WiFi 연결일 경우 30라인의 eth0 를  wlan0 로 수정한다.
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <wiringPi.h>          
#include <lcd.h>                
 
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <unistd.h>
#include <arpa/inet.h>
 
//USE WIRINGPI PIN NUMBERS
#define LCD_RS  25               //Register select pin
#define LCD_E   24               //Enable Pin
#define LCD_D4  23               //Data pin 4
#define LCD_D5  22               //Data pin 5
#define LCD_D6  21               //Data pin 6
#define LCD_D7  14               //Data pin 7
 
int main()
{
    int lcd;
    wiringPiSetup();
    lcd = lcdInit (2164, LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7, 0000);
 
    int n;
    struct ifreq ifr;
    char iface[] = "eth0"//Change this to the network of your choice (eth0, wlan0, etc.)
 
    n = socket(AF_INET, SOCK_DGRAM, 0);
    ifr.ifr_addr.sa_family = AF_INET;
    strncpy(ifr.ifr_name , iface , IFNAMSIZ - 1);
    ioctl(n, SIOCGIFADDR, &ifr);
    close(n);
 
    lcdPosition(lcd, 00);
    lcdPrintf(lcd, "IP Address: ");
 
    lcdPosition(lcd, 01);
    lcdPrintf(lcd, ("%s - %s\n" , iface , inet_ntoa(( (struct sockaddr_in *)&ifr.ifr_addr )->sin_addr)));
    return 0;
}
 
cs
 

Custom Characters

쉽게 편집하여 커스텀 캐릭터에 대한 bit array 를 생성해주는 사이트 - https://omerk.github.io/lcdchargen/
 

Printing a Single Custom Character

Then we store the character in the LCD’s memory with the function lcdCharDef(lcd, 2, omega) 함수를 사용하여 LCD 메모리에 저장한다. LCD 캐릭터 메모리는 0~ 7 까지가 있는데 2번 메모리에 저장하라는 명령이다. 
프린트 하는 명령은 lcdPutchar(lcd, 2) 이고 2번 LCD 메모리 위치에 있는 커스텀 캐릭터를 디스플레이 한다.
 
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
31
32
33
34
35
36
37
38
39
40
#include <wiringPi.h>           
#include <lcd.h>                
 
//USE WIRINGPI PIN NUMBERS
#define LCD_RS  25               //Register select pin
#define LCD_E   24               //Enable Pin
#define LCD_D4  23               //Data pin 4
#define LCD_D5  22               //Data pin 5
#define LCD_D6  21               //Data pin 6
#define LCD_D7  14               //Data pin 7
 
char omega[8= { 0b00000,
                  0b01110,
                  0b10001,
                  0b10001,
                  0b10001,
                  0b01010,
                  0b11011,
                  0b00000};
 
void customChar(void);
int lcd;
 
int main()
{
    int lcd;
    wiringPiSetup();
    lcd = lcdInit (2164, LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7, 0000);
 
    customChar();
}
 
void customChar(void)
{
        lcdCharDef(lcd, 2, omega);
        
        lcdClear(lcd);
        lcdPutchar(lcd, 2);
        sleep(3);
}
cs
 

Printing Multiple Custom Characters

기타 특수 문자와 온도 습도의 물방울을 표현한 캐릭터가 있다. 근데 아래 10~14번 할당한 lcdCharDef() 함수에 메모리 번호가 잘못된거 아닌가 ? 0~7 까지 라매~ㅎㅎ
 
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <wiringPi.h>         
#include <lcd.h>                
 
//USE WIRINGPI PIN NUMBERS
#define LCD_RS  25               //Register select pin
#define LCD_E   24               //Enable Pin
#define LCD_D4  23               //Data pin 4
#define LCD_D5  22               //Data pin 5
#define LCD_D6  21               //Data pin 6
#define LCD_D7  14               //Data pin 7
 
char omega[8= { 0b00000,
                  0b01110,
                  0b10001,
                  0b10001,
                  0b10001,
                  0b01010,
                  0b11011,
                  0b00000};
 
char pi[8= { 0b00000,
               0b00000,
               0b11111,
               0b01010,
               0b01010,
               0b01010,
               0b10011,
               0b00000};
 
char mu[8= { 0b00000,
               0b10010,
               0b10010,
               0b10010,
               0b10010,
               0b11101,
               0b10000,
               0b10000};
               
char drop[8= { 0b00100,
                 0b00100,
                 0b01010,
                 0b01010,
                 0b10001,
                 0b10001,
                 0b10001,
                 0b01110}; 
           
char temp[8= { 0b00100,
                 0b01010,
                 0b01010,
                 0b01110,
                 0b01110,
                 0b11111,
                 0b11111,
                 0b01110};
 
void customChar(void);
int lcd;
 
int main()
{
    int lcd;
    wiringPiSetup();
    lcd = lcdInit (2164, LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7, 0000);
 
    customChar();
}
 
void customChar(void)
{
        lcdCharDef(lcd, 10, omega); 
        lcdCharDef(lcd, 11, pi);
        lcdCharDef(lcd, 12, mu);
        lcdCharDef(lcd, 13, drop);
        lcdCharDef(lcd, 14, temp);
 
        lcdClear(lcd);
 
        lcdPutchar(lcd, 10);
        lcdPutchar(lcd, 11);
        lcdPutchar(lcd, 12);
        lcdPutchar(lcd, 13);
        lcdPutchar(lcd, 14);
 
        sleep(3);
}
 
cs
 

Scrolling Text

 

Scroll Right to Left

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <wiringPi.h>
#include <lcd.h>
#include <string.h>
 
//USE WIRINGPI PIN NUMBERS
#define LCD_RS  25               //Register select pin
#define LCD_E   24               //Enable Pin
#define LCD_D4  23               //Data pin 4
#define LCD_D5  22               //Data pin 5
#define LCD_D6  21               //Data pin 6
#define LCD_D7  14               //Data pin 7
 
void scrollText(void);
char message[] = "Hello, world!";
int count = 0;
int j = 0;
int lcd;
 
int main()
{
        wiringPiSetup();
        lcd = lcdInit (2164, LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7, 0000);
        while(1){
            scrollText();
            }
}
 
void  scrollText(void)
{
        int i, n;
        int h;
        int tempSpace = 0;
        char scrollPadding[] = "                ";
        int messageLength = strlen(scrollPadding) + strlen(message);
        for (n = 0; n < messageLength; n++){h = 16; usleep(300000); printf("\x1B[2J"); if (j > messageLength)
                        j = 0;
 
                for (i = 0; i < j ; i++){
                        scrollPadding[h - j] = message[i];
                h++;
                }
                lcdPosition(lcd, 00);
                lcdClear(lcd);
                lcdPrintf(lcd, "%s", scrollPadding);
                j++;
        }
}
 
cs
 
 

Scroll Left to Right

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <wiringPi.h>
#include <lcd.h>
#include <string.h>
 
//USE WIRINGPI PIN NUMBERS
#define LCD_RS  25               //Register select pin
#define LCD_E   24               //Enable Pin
#define LCD_D4  23               //Data pin 4
#define LCD_D5  22               //Data pin 5
#define LCD_D6  21               //Data pin 6
#define LCD_D7  14               //Data pin 7
 
void scrollText(void);
char message[] = "Hello, world!";
int count = 0;
int j = 0;
int lcd;
 
int main()
{
        wiringPiSetup();
        lcd = lcdInit (2164, LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7, 0000);
        while(1){
            scrollText();
            }
}
 
void  scrollText(void)
{
        int i, n;
        int h;
        int tempSpace = 0;
        char scrollPadding[] = "                ";
        int messageLength = strlen(scrollPadding) + strlen(message);
        for (n = 0; n < messageLength; n++){h = 16; usleep(300000); printf("\x1B[2J"); if (j > messageLength)
                        j = 0;
 
                for (i = strlen(message); i >= 0; i--){
                        scrollPadding[j - h] = message[i];
                h++;
                }
                lcdPosition(lcd, 00);
                lcdClear(lcd);
                lcdPrintf(lcd, "%s", scrollPadding);
                j++;
        }
}
 
cs
 
 

Print Data From a Sensor

 
LCD 에 온습도 데이터 읽어서 LCD에 표시하는 프로그램
 
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <wiringPi.h>
#include <lcd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
 
//USE WIRINGPI PIN NUMBERS
#define LCD_RS  25               //Register select pin
#define LCD_E   24               //Enable Pin
#define LCD_D4  23               //Data pin 4
#define LCD_D5  22               //Data pin 5
#define LCD_D6  21               //Data pin 6
#define LCD_D7  14               //Data pin 7
#define MAXTIMINGS 85
#define DHTPIN 7
 
int lcd;
int dht11_dat[5= {00000};
 
void read_dht11_dat()
{
        uint8_t laststate = HIGH;
        uint8_t counter = 0;
        uint8_t j = 0, i;
        float f; 
 
        dht11_dat[0= dht11_dat[1= dht11_dat[2= dht11_dat[3= dht11_dat[4= 0;
 
        pinMode(DHTPIN, OUTPUT);
        digitalWrite(DHTPIN, LOW);
        delay(18);
        
        digitalWrite(DHTPIN, HIGH);
        delayMicroseconds(40);
        
        pinMode(DHTPIN, INPUT);
 
        for (i = 0; i < MAXTIMINGS; i++)
        {
                counter = 0;
                while (digitalRead(DHTPIN) == laststate)
                {
                        counter++;
                        delayMicroseconds(1);
                        if (counter == 255)
                        {
                                break;
                        }
                }
                laststate = digitalRead(DHTPIN);
 
                if (counter == 255)
                        break;
 
                if ((i >= 4&& (i % 2 == 0))
                {
                        dht11_dat[j / 8<<= 1;
                        if (counter > 16)
                                dht11_dat[j / 8|= 1;
                        j++;
                }
         }
 
        if ((j >= 40&& (dht11_dat[4== ((dht11_dat[0+ dht11_dat[1+ dht11_dat[2+ dht11_dat[3]) & 0xFF)))
        {
                f = dht11_dat[2* 9. / 5. + 32;
 
                lcdPosition(lcd, 00);
                lcdPrintf(lcd, "Humidity: %d.%d %%\n", dht11_dat[0], dht11_dat[1]);
 
                lcdPosition(lcd, 01);
                //lcdPrintf(lcd, "Temp: %d.0 C", dht11_dat[2]); //Uncomment for Celcuis
                lcdPrintf(lcd, "Temp: %.1f F", f); //Comment out for Celcuis
        }
}
 
int main(void)
{
        int lcd;
        wiringPiSetup();
        lcd = lcdInit (2164, LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7, 0000);
        
        while (1)
        {
                read_dht11_dat();
                delay(1000); 
        }
 
        return(0);
}
 
cs
 
길고 긴데 이런거 그냥 저장하고, 링크만 걸면 생각 나지 않을까봐 모셔둔다.
 
소스코드 출처와 이미지 출처 
 
 
 

 

 

 

반응형