-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c_lcd.cpp
executable file
·179 lines (136 loc) · 3.76 KB
/
i2c_lcd.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
Test.h - Test library for Wiring - implementation
Copyright (c) 2006 John Doe. All right reserved.
*/
// include core Wiring API
#include "WProgram.h"
// include this library's description file
#include "i2c_lcd.h"
// include description files for other libraries used (if any)
#include <inttypes.h>
#include <Wire.h>
#include "Print.h"
#define I2C_LCD_ADDR (0x82>>1)
LiquidCrystal::LiquidCrystal()
{
init (I2C_LCD_ADDR);
}
LiquidCrystal::LiquidCrystal(uint8_t addr)
{
init (addr);
}
void LiquidCrystal::init(uint8_t addr)
{
_addr = addr;
Wire.begin();
delayMicroseconds (40000);
// setregval (0x14, 1);
//delayMicroseconds (10000);
begin (16, 2);
}
void LiquidCrystal::begin(uint8_t cols, uint8_t lines, uint8_t dotsize)
{
_currline = 0;
_numlines = lines;
_displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
}
void LiquidCrystal::setregval (uint8_t reg, uint8_t val)
{
Wire.beginTransmission(_addr); // transmit to lcd
Wire.send (reg); // Send register
Wire.send (val); // send value.
Wire.endTransmission(); // stop transmitting
}
void LiquidCrystal::clear()
{
setregval (0x10, 0xaa /* dummy */);
}
void LiquidCrystal::setCursor(uint8_t col, uint8_t row)
{
setregval (0x11, (row << 5) | (col & 0x1f));
}
void LiquidCrystal::home()
{
setCursor (0,0);
}
void LiquidCrystal::write(uint8_t val)
{
setregval (0, val);
}
// Turn the display on/off (quickly)
void LiquidCrystal::noDisplay() {
_displaycontrol &= ~LCD_DISPLAYON;
command(LCD_DISPLAYCONTROL | _displaycontrol);
}
void LiquidCrystal::display() {
_displaycontrol |= LCD_DISPLAYON;
command(LCD_DISPLAYCONTROL | _displaycontrol);
}
// Turns the underline cursor on/off
void LiquidCrystal::noCursor() {
_displaycontrol &= ~LCD_CURSORON;
command(LCD_DISPLAYCONTROL | _displaycontrol);
}
void LiquidCrystal::cursor() {
_displaycontrol |= LCD_CURSORON;
command(LCD_DISPLAYCONTROL | _displaycontrol);
}
// Turn on and off the blinking cursor
void LiquidCrystal::noBlink() {
_displaycontrol &= ~LCD_BLINKON;
command(LCD_DISPLAYCONTROL | _displaycontrol);
}
void LiquidCrystal::blink() {
_displaycontrol |= LCD_BLINKON;
command(LCD_DISPLAYCONTROL | _displaycontrol);
}
// These commands scroll the display without changing the RAM
void LiquidCrystal::scrollDisplayLeft(void) {
command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
}
void LiquidCrystal::scrollDisplayRight(void) {
command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
}
// This is for text that flows Left to Right
void LiquidCrystal::leftToRight(void) {
_displaymode |= LCD_ENTRYLEFT;
command(LCD_ENTRYMODESET | _displaymode);
}
// This is for text that flows Right to Left
void LiquidCrystal::rightToLeft(void) {
_displaymode &= ~LCD_ENTRYLEFT;
command(LCD_ENTRYMODESET | _displaymode);
}
// This will 'right justify' text from the cursor
void LiquidCrystal::autoscroll(void) {
_displaymode |= LCD_ENTRYSHIFTINCREMENT;
command(LCD_ENTRYMODESET | _displaymode);
}
// This will 'left justify' text from the cursor
void LiquidCrystal::noAutoscroll(void) {
_displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
command(LCD_ENTRYMODESET | _displaymode);
}
void LiquidCrystal::reset(void) {
setregval (0x14, 1);
}
void LiquidCrystal::contrast(uint8_t val)
{
setregval (0x12, val);
}
void LiquidCrystal::backlight(uint8_t val)
{
setregval (0x13, val);
}
// Allows us to fill the first 8 CGRAM locations
// with custom characters
void LiquidCrystal::createChar(uint8_t location, uint8_t charmap[]) {
location &= 0x7; // we only have 8 locations 0-7
command(LCD_SETCGRAMADDR | (location << 3));
for (int i=0; i<8; i++) {
write(charmap[i]);
}
}
inline void LiquidCrystal::command(uint8_t value) {
setregval (1, value);
}