-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcd1602.cpp
160 lines (128 loc) · 2.49 KB
/
lcd1602.cpp
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
#include "lcd1602.h"
#include "define.h"
uint8_t display_function;
uint8_t numlines;
uint8_t display_control;
uint8_t display_mode;
void initLCD(uint8_t bitmode)
{
if (bitmode) display_function = FULL_DATA_BUS | ONE_ROW | FONT_5_7_STYLE;
else display_function = HALF_DATA_BUS | ONE_ROW | FONT_5_7_STYLE;
beginLCD(16, 1, FONT_5_7_STYLE);
}
void beginLCD(uint8_t columns, uint8_t rows, uint8_t dot_style)
{
if (rows > 1) display_function |= DOUBLE_ROW;
numlines = rows;
_delay_us(50000);
RS_LOW
ENABLE_LOW
WRITE_ON
commandSet(FUNCTION_SET | display_function);
_delay_us(4500);
commandSet(FUNCTION_SET | display_function);
_delay_us(150);
commandSet(FUNCTION_SET | display_function);
commandSet(FUNCTION_SET | display_function);
display_control = DISPLAY_ON | CURSOR_OFF | BLINK_OFF;
displaySwitch(ON);
screenClear();
display_mode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
commandSet(LCD_ENTRYMODESET | display_mode);
}
void blinkOn(uint8_t state)
{
display_control |= (state ? BLINK_ON : BLINK_OFF);
commandSet(LCD_DISPLAYCONTROL | display_control);
}
uint8_t readBusyFlag(void)
{
return 0;
}
uint8_t readInstruction(uint8_t command)
{
return 0;
}
void writeData(uint8_t data)
{
RS_HIGH
ENABLE_LOW
WRITE_ON
*(volatile uint8_t*)(0x2B) = data;
pulseEnable();
}
uint8_t readData(uint8_t data)
{
return 0;
}
void screenClear(void)
{
commandSet(SCREEN_CLEAR);
_delay_us(2000);
}
void cursorReturn(void)
{
}
void displaySwitch(uint8_t state)
{
display_control |= (state ? DISPLAY_ON : DISPLAY_OFF);
commandSet(DISPLAY_SWITCH | display_control);
}
void shift(uint8_t flags)
{
}
void commandSet(uint8_t flags)
{
RS_LOW
ENABLE_LOW
WRITE_ON
*(volatile uint8_t*)(0x2B) = flags;
pulseEnable();
}
void pulseEnable(void)
{
ENABLE_LOW
_delay_us(1);
ENABLE_HIGH
_delay_us(1);
ENABLE_LOW
_delay_us(100);
}
void setCursor(uint8_t column, uint8_t row)
{
switch(row)
{
case 0:
commandSet(LCD_SETDDRAMADDR | column);
break;
case 1:
commandSet(LCD_SETDDRAMADDR | 0x40 | column);
break;
}
}
void printChar(uint8_t character)
{
writeData(character);
}
void printInt(int16_t val)
{
if (val < 0)
{
writeData('-');
val = -val;
}
if (val / 10)
{
printInt(val / 10);
}
writeData(val % 10 + '0');
//_delay_us(500);
}
void printString(const char *string)
{
while(*string)
{
writeData(*string);
string++;
}
}