1 | #include <avr/io.h>
|
2 | #include <avr/delay.h>
|
3 | #include <avr/pgmspace.h>
|
4 |
|
5 | #define LCD_PORT PORTA
|
6 | #define LCD_DDR DDRA
|
7 | #define LCD_DB4 PA0
|
8 | #define LCD_DB5 PA1
|
9 | #define LCD_DB6 PA2
|
10 | #define LCD_DB7 PA3
|
11 | #define LCD_PIN_RS PA4
|
12 | #define LCD_PIN_E PA5
|
13 |
|
14 | #define CMD_EMS (1<<2)
|
15 | #define EMS_CURSOR_BACKWARD 0
|
16 | #define EMS_CURSOR_FORWARD (1<<1)
|
17 | #define EMS_NODISPLAYSHIFT 0
|
18 | #define EMS_DISPLAYSHIFT 1
|
19 |
|
20 | #define CMD_CLD 1
|
21 |
|
22 | #define CMD_RTH 2
|
23 |
|
24 | #define CMD_SHIFT (1<<4)
|
25 | #define SHIFT_CURSOR 0
|
26 | #define SHIFT_DISPLAY (1<<3)
|
27 | #define SHIFT_LEFT 0
|
28 | #define SHIFT_RIGHT (1<<2)
|
29 |
|
30 | #define CMD_CONFIG (1<<5)
|
31 | #define CONFIG_8BIT (1<<4)
|
32 | #define CONFIG_4BIT 0
|
33 | #define CONFIG_2LINE (1<<3)
|
34 | #define CONFIG_1LINE 0
|
35 | #define CONFIG_5X10 (1<<2)
|
36 | #define CONFIG_5X7 0
|
37 |
|
38 | #define CMD_DISPLAY (1<<3)
|
39 | #define DISPLAY_ON (1<<2)
|
40 | #define DISPLAY_OFF 0
|
41 | #define DISPLAY_CURSOR (1<<1)
|
42 | #define DISPLAY_NOCURSOR 0
|
43 | #define DISPLAY_BLINKING 1
|
44 | #define DISPLAY_NOBLINKING 0
|
45 |
|
46 | #define CMD_CURSORADDR (1<<7)
|
47 |
|
48 | #define POWERUP_TIME 40
|
49 |
|
50 |
|
51 | void enable()
|
52 | {
|
53 | LCD_PORT |= (1<<LCD_PIN_E);
|
54 | LCD_PORT &= ~(1<<LCD_PIN_E);
|
55 | }
|
56 |
|
57 | void sendNibble(uint8_t nibble)
|
58 | {
|
59 | LCD_PORT &= 0xF0;
|
60 | LCD_PORT |= nibble;
|
61 | enable();
|
62 | }
|
63 |
|
64 | void wd4(uint8_t data)
|
65 | {
|
66 | uint8_t nibble = (data >> 4); // high nibble speichern
|
67 | LCD_PORT |= (1<<LCD_PIN_RS); // Datenregister auswählen
|
68 | sendNibble(nibble);
|
69 | nibble = (data&0x0F); // low nibble speichern
|
70 | sendNibble(nibble);
|
71 | _delay_us(46);
|
72 | }
|
73 |
|
74 | void cmd4(uint8_t cmd)
|
75 | {
|
76 | uint8_t nibble = (cmd >> 4); // high nibble speichern
|
77 | LCD_PORT &= ~(1<<LCD_PIN_RS); // Steuerregister auswählen
|
78 | sendNibble(nibble);
|
79 | nibble = (cmd&0x0F); // low nibble speichern
|
80 | sendNibble(nibble);
|
81 | _delay_us(42);
|
82 | }
|
83 |
|
84 | void cldLCD162C()
|
85 | {
|
86 | cmd4(CMD_CLD);
|
87 | _delay_ms(2);
|
88 | }
|
89 |
|
90 | void rthLCD162C()
|
91 | {
|
92 | cmd4(CMD_RTH);
|
93 | _delay_ms(2);
|
94 | }
|
95 |
|
96 | void initLCD162C()
|
97 | {
|
98 |
|
99 | LCD_PORT = 0x00;
|
100 | LCD_DDR |= ((1<<LCD_DB4)|(1<<LCD_DB5)|(1<<LCD_DB6)|(1<<LCD_DB7)|(1<<LCD_PIN_E)|(1<<LCD_PIN_RS));
|
101 | _delay_ms(POWERUP_TIME);
|
102 | /* 4-bit Modus konfigurieren ------Function-Set 1*/
|
103 | LCD_PORT |= (1<<LCD_DB5);
|
104 | LCD_PORT &= ~(1<<LCD_DB4);
|
105 |
|
106 | _delay_ms(40);
|
107 | /* 4-bit Modus konfigurieren ------Function-Set 2*/
|
108 | LCD_PORT |= (1<<LCD_DB5);
|
109 | LCD_PORT &= ~(1<<LCD_DB4);
|
110 |
|
111 | _delay_ms(5);
|
112 | /* 4-bit Modus konfigurieren ------Function-Set 3*/
|
113 | LCD_PORT |= (1<<LCD_DB5);
|
114 | LCD_PORT &= ~(1<<LCD_DB4);
|
115 |
|
116 | /* 4-Bit Modus starten ... */
|
117 | LCD_PORT |= (1<<LCD_PIN_E);
|
118 | LCD_PORT &= ~(1<<LCD_PIN_E);
|
119 | _delay_ms(5);
|
120 |
|
121 | cmd4(CMD_CONFIG | CONFIG_2LINE);
|
122 | cmd4(CMD_DISPLAY | DISPLAY_OFF);
|
123 | cldLCD162C();
|
124 | cmd4(CMD_EMS | EMS_CURSOR_FORWARD);
|
125 | cmd4(CMD_DISPLAY | DISPLAY_ON);
|
126 | }
|
127 |
|
128 |
|
129 | void gotoXY(uint8_t x,uint8_t y)
|
130 | {
|
131 | cmd4(CMD_CURSORADDR | (y*0x40) | x);
|
132 | }
|
133 |
|
134 | void writeString(const char* str)
|
135 | {
|
136 | while(*str)writeChar(*str++);
|
137 | }
|
138 |
|
139 | void writeStringP(const char* str)
|
140 | {
|
141 | while(pgm_read_byte(str) != 0x00)writeChar(pgm_read_byte(str++));
|
142 | }
|
143 |
|
144 | void writeChar(char c)
|
145 | {
|
146 | wd4(c);
|
147 | }
|
148 |
|
149 | #include <string.h>
|
150 |
|
151 | void writeInteger(int i)
|
152 | {
|
153 | char buffer[12];
|
154 | itoa(i,buffer,10);
|
155 | writeString(buffer);
|
156 | }
|
157 |
|
158 | int main()
|
159 | {
|
160 | initLCD162C();
|
161 | writeStringP(PSTR("Hello World"));
|
162 | while(1){}
|
163 | }
|