/
HD44780.c
executable file
·237 lines (207 loc) · 7.43 KB
/
HD44780.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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//
// HD44780 LCD 4-bit IO mode Driver
// (C) 2009 - 2012 Radu Motisan , radu.motisan@gmail.com , www.pocketmagic.net
// All rights reserved.
//
// HD44780.c: Definitions for LCD command instructions
// The constants define the various LCD controller instructions which can be passed to the
// function lcd_command(), see HD44780 data sheet for a complete description.
#include <stdio.h>
#include <util/delay.h>
#include <avr/io.h>
#include "HD44780.h"
//-----------------------------------------------------------------------------------------
// FUNCTION: lcd_toggle_e
// PURPOSE: flush channel E
void lcd_toggle_e(void)
{
lcd_e_high();
_delay_us(10);
lcd_e_low();
}
//-----------------------------------------------------------------------------------------
// FUNCTION: lcd_write
// PURPOSE: send a character or an instruction to the LCD
void lcd_write(uint8_t data, uint8_t rs)
{
_delay_us(1000);
//check write type
if (rs)
lcd_rs_high(); //write data
else
lcd_rs_low(); //write instruciton
// configure data pins as output
LCD_DDR(LCD_DATA0_PORT) |= _BV(LCD_DATA0_PIN);
LCD_DDR(LCD_DATA1_PORT) |= _BV(LCD_DATA1_PIN);
LCD_DDR(LCD_DATA2_PORT) |= _BV(LCD_DATA2_PIN);
LCD_DDR(LCD_DATA3_PORT) |= _BV(LCD_DATA3_PIN);
// output high nibble first
LCD_DATA3_PORT &= ~_BV(LCD_DATA3_PIN);
LCD_DATA2_PORT &= ~_BV(LCD_DATA2_PIN);
LCD_DATA1_PORT &= ~_BV(LCD_DATA1_PIN);
LCD_DATA0_PORT &= ~_BV(LCD_DATA0_PIN);
if(data & 0x80) LCD_DATA3_PORT |= _BV(LCD_DATA3_PIN);
if(data & 0x40) LCD_DATA2_PORT |= _BV(LCD_DATA2_PIN);
if(data & 0x20) LCD_DATA1_PORT |= _BV(LCD_DATA1_PIN);
if(data & 0x10) LCD_DATA0_PORT |= _BV(LCD_DATA0_PIN);
lcd_toggle_e();
// output low nibble
LCD_DATA3_PORT &= ~_BV(LCD_DATA3_PIN);
LCD_DATA2_PORT &= ~_BV(LCD_DATA2_PIN);
LCD_DATA1_PORT &= ~_BV(LCD_DATA1_PIN);
LCD_DATA0_PORT &= ~_BV(LCD_DATA0_PIN);
if(data & 0x08) LCD_DATA3_PORT |= _BV(LCD_DATA3_PIN);
if(data & 0x04) LCD_DATA2_PORT |= _BV(LCD_DATA2_PIN);
if(data & 0x02) LCD_DATA1_PORT |= _BV(LCD_DATA1_PIN);
if(data & 0x01) LCD_DATA0_PORT |= _BV(LCD_DATA0_PIN);
lcd_toggle_e();
// all data pins high (inactive)
LCD_DATA0_PORT |= _BV(LCD_DATA0_PIN);
LCD_DATA1_PORT |= _BV(LCD_DATA1_PIN);
LCD_DATA2_PORT |= _BV(LCD_DATA2_PIN);
LCD_DATA3_PORT |= _BV(LCD_DATA3_PIN);
}
//-----------------------------------------------------------------------------------------
// FUNCTION: lcd_char
// PURPOSE: send a character to the LCD
void lcd_char(uint8_t data)
{
if (data=='\n')
{
if (g_nCurrentLine >= LCD_LINES - 1)
lcd_setline(0);
else
lcd_setline(g_nCurrentLine+1);
}
else
{
lcd_write(data, 1);
}
}
//-----------------------------------------------------------------------------------------
// FUNCTION: lcd_instr
// PURPOSE: send an instruction to the LCD
void lcd_instr(uint8_t instr)
{
lcd_write(instr, 0);
}
//-----------------------------------------------------------------------------------------
// FUNCTION: lcd_init
// PURPOSE: Initialize LCD to 4 bit I/O mode
void lcd_init()
{
// configure all port bits as output (LCD data and control lines on different ports
LCD_DDR(LCD_RS_PORT) |= _BV(LCD_RS_PIN);
LCD_DDR(LCD_E_PORT) |= _BV(LCD_E_PIN);
LCD_DDR(LCD_DATA0_PORT) |= _BV(LCD_DATA0_PIN);
LCD_DDR(LCD_DATA1_PORT) |= _BV(LCD_DATA1_PIN);
LCD_DDR(LCD_DATA2_PORT) |= _BV(LCD_DATA2_PIN);
LCD_DDR(LCD_DATA3_PORT) |= _BV(LCD_DATA3_PIN);
// wait 25ms or more after power-on
_delay_us(25000);
// initial write to lcd is 8bit
LCD_DATA1_PORT |= _BV(LCD_DATA1_PIN); // _BV(LCD_FUNCTION)>>4;
LCD_DATA0_PORT |= _BV(LCD_DATA0_PIN); // _BV(LCD_FUNCTION_8BIT)>>4;
lcd_toggle_e();
_delay_us(2000); //2000 // delay, busy flag can't be checked here
// repeat last command
lcd_toggle_e();
_delay_us(64); //64 // delay, busy flag can't be checked here
// now configure for 4bit mode
LCD_DATA0_PORT &= ~_BV(LCD_DATA0_PIN); // LCD_FUNCTION_4BIT_1LINE>>4
lcd_toggle_e();
_delay_us(2000); // some displays need this additional delay
// set 4 bit IO
lcd_instr(LCD_FUNCTION_4BIT_2LINES); // 4-bit interface, dual line, 5x7 dots
lcd_toggle_e();
_delay_us(2000); // some displays need this additional delay
lcd_instr(LCD_ENTRY_INC_);//cursor move right, no shift display
lcd_toggle_e();
_delay_us(2500); // some displays need this additional delay
lcd_instr(LCD_DISP_ON);// display on, cursor on, blink char
lcd_toggle_e();
_delay_us(2500); // some displays need this additional delay
lcd_clrscr();
lcd_home();//set cursor to home and clear the cursor
}
//-----------------------------------------------------------------------------------------
// FUNCTION: lcd_newline
// PURPOSE: Move cursor on specified line
void lcd_setline(uint8_t line)
{
uint8_t addressCounter = 0;
switch(line)
{
case 0: addressCounter = LCD_START_LINE1; break;
case 1: addressCounter = LCD_START_LINE2; break;
case 2: addressCounter = LCD_START_LINE3; break;
case 3: addressCounter = LCD_START_LINE4; break;
default:addressCounter = LCD_START_LINE1; break;
}
g_nCurrentLine = line;
lcd_instr((1<<LCD_DDRAM)+addressCounter);
}
int g_nCurrentLine = 0;
//-----------------------------------------------------------------------------------------
// FUNCTION: lcd_string
// PURPOSE: send a null terminated string to the LCD eg. char x[10]="hello!";
void lcd_string(char *text)
{
char c;
while ( (c = *text++) ) lcd_char(c);
}
void lcd_string2(char *szFormat, ...)
{
char szBuffer[256]; //in this buffer we form the message
int NUMCHARS = sizeof(szBuffer) / sizeof(szBuffer[0]);
int LASTCHAR = NUMCHARS - 1;
va_list pArgs;
va_start(pArgs, szFormat);
vsnprintf(szBuffer, NUMCHARS - 1, szFormat, pArgs);
va_end(pArgs);
lcd_string(szBuffer);
}
//-----------------------------------------------------------------------------------------
// FUNCTION: lcd_gotoxy
// PURPOSE: Set cursor to specified position
// Input: x horizontal position (0: left most position)
// y vertical position (0: first line)
void lcd_gotoxy(uint8_t x, uint8_t y)
{
#if LCD_LINES==1
lcd_instr((1<<LCD_DDRAM)+LCD_START_LINE1+x);
#elif LCD_LINES==2
switch (y)
{
case 0:lcd_instr((1<<LCD_DDRAM)+LCD_START_LINE1+x);break;
case 1:lcd_instr((1<<LCD_DDRAM)+LCD_START_LINE2+x);break;
default: break;
}
#elif LCD_LINES==4
switch (y)
{
case 0:lcd_instr((1<<LCD_DDRAM)+LCD_START_LINE1+x);break;
case 1:lcd_instr((1<<LCD_DDRAM)+LCD_START_LINE2+x);break;
case 2:lcd_instr((1<<LCD_DDRAM)+LCD_START_LINE3+x);break;
case 3:lcd_instr((1<<LCD_DDRAM)+LCD_START_LINE4+x);break;
default: break;
}
#endif
}
//-----------------------------------------------------------------------------------------
// FUNCTION: lcd_clrscr
// PURPOSE: Clear display and set cursor to home position
void lcd_clrscr(void)
{
g_nCurrentLine = 0;
lcd_instr(1<<LCD_CLR);
_delay_us(500);
}
//-----------------------------------------------------------------------------------------
// FUNCTION: lcd_home
// PURPOSE: Set cursor to home position
void lcd_home(void)
{
g_nCurrentLine = 0;
lcd_instr(1<<LCD_HOME);
}