1 | #include <Wire.h>
|
---|
2 |
|
---|
3 | #define I2CLCD_ADDR 0x3e
|
---|
4 | byte lcd_contrast = 35;
|
---|
5 |
|
---|
6 | void i2clcd_cmd(byte x) {
|
---|
7 | Wire.beginTransmission(I2CLCD_ADDR);
|
---|
8 | Wire.write((uint8_t)0b00000000); // CO = 0,RS = 0
|
---|
9 | Wire.write(x);
|
---|
10 | Wire.endTransmission();
|
---|
11 | }
|
---|
12 |
|
---|
13 | void i2clcd_clear() {
|
---|
14 | i2clcd_cmd(0b00000001);
|
---|
15 | }
|
---|
16 |
|
---|
17 | void i2clcd_contdata(byte x) {
|
---|
18 | Wire.write(0b11000000); // CO = 1, RS = 1
|
---|
19 | Wire.write(x);
|
---|
20 | }
|
---|
21 |
|
---|
22 | void i2clcd_lastdata(byte x) {
|
---|
23 | Wire.write(0b01000000); // CO = 0, RS = 1
|
---|
24 | Wire.write(x);
|
---|
25 | }
|
---|
26 |
|
---|
27 | void i2clcd_printStr(const char *s) {
|
---|
28 | Wire.beginTransmission(I2CLCD_ADDR);
|
---|
29 | while (*s) {
|
---|
30 | if (*(s + 1)) {
|
---|
31 | i2clcd_contdata(*s);
|
---|
32 | } else {
|
---|
33 | i2clcd_lastdata(*s);
|
---|
34 | }
|
---|
35 | s++;
|
---|
36 | }
|
---|
37 | Wire.endTransmission();
|
---|
38 | }
|
---|
39 |
|
---|
40 | void i2clcd_setCursor(byte x, byte y) {
|
---|
41 | i2clcd_cmd(0x80 | (y * 0x40 + x));
|
---|
42 | }
|
---|
43 |
|
---|
44 | void i2clcd_printInt(int num) {
|
---|
45 | char int2str[10];
|
---|
46 | String str(num);
|
---|
47 | str.getBytes((unsigned char*)int2str, 10);
|
---|
48 | i2clcd_printStr(int2str);
|
---|
49 | }
|
---|
50 |
|
---|
51 | void i2clcd_printfloat(float num) {
|
---|
52 | char float2str[10];
|
---|
53 | String str1((int)num);
|
---|
54 | String str2((int)((num-(int)num)*100));
|
---|
55 | str1 += '.' + str2;
|
---|
56 | str1.getBytes((unsigned char*)(float2str), 10);
|
---|
57 | float2str[str1.length()] = '\0';
|
---|
58 | i2clcd_printStr(float2str);
|
---|
59 | }
|
---|
60 |
|
---|
61 | void i2clcd_begin() {
|
---|
62 | Wire.begin();
|
---|
63 | i2clcd_cmd(0b00111000); // function set
|
---|
64 | i2clcd_cmd(0b00111001); // function set
|
---|
65 | i2clcd_cmd(0b00000100); // EntryModeSet
|
---|
66 | i2clcd_cmd(0b00010100); // interval osc
|
---|
67 | i2clcd_cmd(0b01110000 | (lcd_contrast & 0xF)); // lcd_contrast Low
|
---|
68 | i2clcd_cmd(0b01011100 | ((lcd_contrast >> 4) & 0x3)); // contast High/icon/power
|
---|
69 | i2clcd_cmd(0b01101100); // follower control
|
---|
70 | delay(200);
|
---|
71 | i2clcd_cmd(0b00111000); // function set
|
---|
72 | i2clcd_cmd(0b00001100); // Display On
|
---|
73 | i2clcd_cmd(0b00000001); // Clear Display
|
---|
74 | delay(2);
|
---|
75 | }
|
---|