[Tiva] tm4c 시리즈 MCU로 TextLCD(CLCD) 동작

임베디드/TI 2014. 8. 21. 02:30

 MCU로 프로그램을 실행할 때 자신이 확인하고 싶은 값을 보기 위해서 주로 UART통신 또는 TextLCD에 값을 띄워보는 방법이 있습니다.

 MCU가 컴퓨터와 항상 연결되어 있을 때엔 당연히 UART를 통해 원하는 값을 모니터에 출력하는 것이 가능합니다. 그러나 일반적으로 MCU를 응용하시는 분들께서는 컴퓨터와 연결하여 사용하는 경우는 많지 않으실 겁니다. 이럴 때 가장 좋은 방법이 TextLCD를 활용하는 것이지요.


 자신이 원하는 값이 숫자와 같은 수치라면 FND를 사용하는 방법또한 있습니다만 FND의 경우 원하는 값을 얻기애 연결해야 하는 핀의 수가 TextLCD보다 상대적으로 많이 사용되는 편이기도 합니다. 심지어 TextLCD는 텍스트 값 또한 출력이 가능하기 때문에 간단한 수치를 확인하는 목적 이상을 원한다면 TextLCD를 사용하는 쪽이 바람직할 듯 합니다.


 가장 많이 사용되는 TextLCD는 2 X 16일 겁니다. 인터넷 상에서도 Atmega128을 기반으로 한 소스코드도 쉽게 구할 수 있어 생각보다 번거롭지 않게 TextLCD에 값을 출력하실 수 있을 것입니다.


 아래의 코드는 Atmega128에서 많이 사용되는 코드를 TivaWare를 활용하는 MCU에서 바로 사용할 수 있게 작성되었습니다.



참고로 회로는 다음과 같이 연결하시면 되겠습니다.

RS    = A6,    RW    = GND,    EN    = A7

데이터0~7    = B0 ~ B7


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
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
 
void EnablePulse(void) {
    GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_7, GPIO_PIN_7);
    SysCtlDelay(SysCtlClockGet() / 1200);
    GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_7, 0);
}
 
void sendLCDcommand(unsigned char command) {
    GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, 0);
    GPIOPinWrite(GPIO_PORTB_BASE, 0xFF, command);
    EnablePulse();
    SysCtlDelay(SysCtlClockGet() / 1200);
}
 
void InitLcd(void) {
    SysCtlDelay(SysCtlClockGet() / 10);
    sendLCDcommand(0x38);
    sendLCDcommand(0x0C);
    sendLCDcommand(0x01);
    SysCtlDelay(SysCtlClockGet() / 1200);
}
 
void sendLCDdata(unsigned char data) {
    GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, GPIO_PIN_6);
    GPIOPinWrite(GPIO_PORTB_BASE, 0xFF, data);
    EnablePulse();
    SysCtlDelay(SysCtlClockGet() / 1200);
 
}
 
void displayStr(char *str){
    while(*str){
        sendLCDdata(*str);
        str++;
    }
}
 
void setLocate(int x, int y){
    unsigned char addr;
    if(x==0)
        addr = 0x80+y;
    else
        addr = 0xC0+y;
    sendLCDcommand(addr);
}
 
void setLcdShift(char a){
    unsigned char shift;
    //0 == Left    1 == Right
    if(a==0)
        shift = 0x18;
    else if(a==1)
        shift = 0x1C;
    sendLCDcommand(shift);
    SysCtlDelay(SysCtlClockGet() / 10);
}
 
int main(void) {
 
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
 
    GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_6 | GPIO_PIN_7);
    GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE,
            GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4
                    | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7);
 
    GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6 | GPIO_PIN_7, 0x00);
    GPIOPinWrite(GPIO_PORTB_BASE,
            GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4
                    | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7, 0x00);
 
 
    InitLcd();
    setLocate(0,1);
    displayStr("Hello, World!");
    setLocate(1,0);
    displayStr("http://elecs.tistory.com/");
 
    while (1) {
        setLcdShift(0);
    }
}
 


해당 코드를 런치패드에 실행시키면 CLCD에 다음과 같은 값이 출력됨을 확인하실 수 있습니다.



300x250