b. 임베디드/AVR

[ATmega328] Nokia 5110 LCD (GLCD) 문자 출력하기

로봇쟁이 2018. 8. 4. 01:45
얼마전에 작업한 시계를 적용할 LCD를 이걸로 정했습니다.
적용전에 한번 문자 출력은 해봐야겠죠?? ^^

#define F_CPU   16000000UL
#include <avr/io.h>
 
/*  핀맵 
    PORTB4 : RST
    PORTB3 : CE
    PORTB2 : DC
    PORTB1 : DIN
    PORTB0 : CLK
    PORTD7 : BACKLIGHT
*/
 
#define RST_SET         PORTB |= 0x10
#define RST_RESET       PORTB &= ~0x10
#define CE_SET          PORTB |= 0x08
#define CE_RESET        PORTB &= ~0x08
#define DC_SET          PORTB |= 0x04
#define DC_RESET        PORTB &= ~0x04
#define DIN_SET         PORTB |= 0x02
#define DIN_RESET       PORTB &= ~0x02
#define CLK_SET         PORTB |= (1<<0)
#define CLK_RESET       PORTB &= ~(1<<0)
#define BACKLIGHT_ON    PORTD |= (1<<7)
#define BACKLIGHT_OFF   PORTD &= ~(1<<7)
 
static const byte ASCII[][5= // 아스키코드의 문자열을 나열합니다.
{
 {0x000x000x000x000x00// 20  
,{0x000x000x5f0x000x00// 21 !
,{0x000x070x000x070x00// 22 "
,{0x140x7f0x140x7f0x14// 23 #
,{0x240x2a0x7f0x2a0x12// 24 $
,{0x230x130x080x640x62// 25 %
,{0x360x490x550x220x50// 26 &
,{0x000x050x030x000x00// 27 '
,{0x000x1c0x220x410x00// 28 (
,{0x000x410x220x1c0x00// 29 )
,{0x140x080x3e0x080x14// 2a *
,{0x080x080x3e0x080x08// 2b +
,{0x000x500x300x000x00// 2c ,
,{0x080x080x080x080x08// 2d -
,{0x000x600x600x000x00// 2e .
,{0x200x100x080x040x02// 2f /
,{0x3e0x510x490x450x3e// 30 0
,{0x000x420x7f0x400x00// 31 1
,{0x420x610x510x490x46// 32 2
,{0x210x410x450x4b0x31// 33 3
,{0x180x140x120x7f0x10// 34 4
,{0x270x450x450x450x39// 35 5
,{0x3c0x4a0x490x490x30// 36 6
,{0x010x710x090x050x03// 37 7
,{0x360x490x490x490x36// 38 8
,{0x060x490x490x290x1e// 39 9
,{0x000x360x360x000x00// 3a :
,{0x000x560x360x000x00// 3b ;
,{0x080x140x220x410x00// 3c <
,{0x140x140x140x140x14// 3d =
,{0x000x410x220x140x08// 3e >
,{0x020x010x510x090x06// 3f ?
,{0x320x490x790x410x3e// 40 @
,{0x7e0x110x110x110x7e// 41 A
,{0x7f0x490x490x490x36// 42 B
,{0x3e0x410x410x410x22// 43 C
,{0x7f0x410x410x220x1c// 44 D
,{0x7f0x490x490x490x41// 45 E
,{0x7f0x090x090x090x01// 46 F
,{0x3e0x410x490x490x7a// 47 G
,{0x7f0x080x080x080x7f// 48 H
,{0x000x410x7f0x410x00// 49 I
,{0x200x400x410x3f0x01// 4a J
,{0x7f0x080x140x220x41// 4b K
,{0x7f0x400x400x400x40// 4c L
,{0x7f0x020x0c0x020x7f// 4d M
,{0x7f0x040x080x100x7f// 4e N
,{0x3e0x410x410x410x3e// 4f O
,{0x7f0x090x090x090x06// 50 P
,{0x3e0x410x510x210x5e// 51 Q
,{0x7f0x090x190x290x46// 52 R
,{0x460x490x490x490x31// 53 S
,{0x010x010x7f0x010x01// 54 T
,{0x3f0x400x400x400x3f// 55 U
,{0x1f0x200x400x200x1f// 56 V
,{0x3f0x400x380x400x3f// 57 W
,{0x630x140x080x140x63// 58 X
,{0x070x080x700x080x07// 59 Y
,{0x610x510x490x450x43// 5a Z
,{0x000x7f0x410x410x00// 5b [
,{0x020x040x080x100x20// 5c ¥
,{0x000x410x410x7f0x00// 5d ]
,{0x040x020x010x020x04// 5e ^
,{0x400x400x400x400x40// 5f _
,{0x000x010x020x040x00// 60 `
,{0x200x540x540x540x78// 61 a
,{0x7f0x480x440x440x38// 62 b
,{0x380x440x440x440x20// 63 c
,{0x380x440x440x480x7f// 64 d
,{0x380x540x540x540x18// 65 e
,{0x080x7e0x090x010x02// 66 f
,{0x0c0x520x520x520x3e// 67 g
,{0x7f0x080x040x040x78// 68 h
,{0x000x440x7d0x400x00// 69 i
,{0x200x400x440x3d0x00// 6a j 
,{0x7f0x100x280x440x00// 6b k
,{0x000x410x7f0x400x00// 6c l
,{0x7c0x040x180x040x78// 6d m
,{0x7c0x080x040x040x78// 6e n
,{0x380x440x440x440x38// 6f o
,{0x7c0x140x140x140x08// 70 p
,{0x080x140x140x180x7c// 71 q
,{0x7c0x080x040x040x08// 72 r
,{0x480x540x540x540x20// 73 s
,{0x040x3f0x440x400x20// 74 t
,{0x3c0x400x400x200x7c// 75 u
,{0x1c0x200x400x200x1c// 76 v
,{0x3c0x400x300x400x3c// 77 w
,{0x440x280x100x280x44// 78 x
,{0x0c0x500x500x500x3c// 79 y
,{0x440x640x540x4c0x44// 7a z
,{0x000x080x360x410x00// 7b {
,{0x000x000x7f0x000x00// 7c |
,{0x000x410x360x080x00// 7d }
,{0x100x080x080x100x08// 7e ←
,{0x780x460x410x460x78// 7f →
};
 
 
 
void LcdWriteString(char *characters) {
    while(*characters) LcdWriteCharacter(*characters++);
}
 
void LcdWriteCharacter(char character) {
    for(int i=0; i<5; i++) LcdWriteData(ASCII[character - 0x20][i]);
    LcdWriteData(0x00);
}
 
void LcdWriteData(byte dat) {
    DC_SET;
    CE_RESET;
    shiftWrite(dat);  // 시리얼 데이터를 LCD에 전송합니다.
    CE_SET;
}
 
void LcdXY(int x, int y) {
    LcdWriteCmd(0x80 | x);  // 가로크기를 지정합니다.
    LcdWriteCmd(0x40 | y);  // 세로크기를 지정합니다.
}
 
void LcdWriteCmd(byte cmd) {
    DC_RESET;
    CE_RESET;
    shiftWrite(cmd);                    //시리얼 데이터를 LCD에 전송합니다.
    CE_SET;
}
 
 
void shiftWrite(uint8_t val) {
    for (uint8_t i = 0; i < 8; i++)  {
        if(!!(val & (1 << (7 - i)))) {
            DIN_SET;
        }
        else {
            DIN_RESET;
        }
        CLK_SET;
        CLK_RESET;
    }
}
 
 
int main() {
    Serial.begin(9600);
    // 각각의 핀모드를 출력으로     설정합니다.
    DDRB |= ((1<<0| (1<<1| (1<<2| (1<<3| (1<<4));
    DDRD |= (1<<7);
 
    BACKLIGHT_ON;
    RST_RESET;
    RST_SET;
    
    LcdWriteCmd(0x21);  // LCD 확장 명령
    LcdWriteCmd(0xC0);  // LCD의 콘트라스트(명암을 결정합니다)
    LcdWriteCmd(0x07);  
    LcdWriteCmd(0x13);  
    LcdWriteCmd(0x20);  
    LcdWriteCmd(0x0C);  
    
    for(int i=0; i<504; i++) LcdWriteData(0x00); //LCD를 초기화 합니다
    
    LcdXY(0,0);
    LcdWriteString("really?"); 
    while(1);
}
cs

조금 더 디테일하게 수정 보완해봐야겠습니다. 


반응형