-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLCD.cpp
81 lines (72 loc) · 1.6 KB
/
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
#include "LCD.h"
LCD::LCD(int d4, int d5, int d6, int d7, int en, int rs, int v0) :
Driver({Pin(d4, FN_OUTPUT),
Pin(d5, FN_OUTPUT),
Pin(d6, FN_OUTPUT),
Pin(d7, FN_OUTPUT),
Pin(en, FN_OUTPUT),
Pin(rs, FN_OUTPUT),
Pin(v0, FN_PWM, {.wrap_value = 100, .clock_divider = 8.f})})
{
}
void LCD::init(void)
{
this->rsPin().set(0);
// Set interface 8-bit
this->send4(0b0011);
// Set interface 8-bit
this->send4(0b0011);
// Set interface 8-bit
this->send4(0b0011);
// Set interface 4-bit
this->send4(0b0010);
// Set interface 4-bit, 2-lines, 5x8 font
this->send8(0b00101000);
// Set display on, cursor off
this->send8(0b00001100);
// Increment cursor, no shift
this->send8(0b00000110);
this->rsPin().set(1);
this->clear();
}
void LCD::clear(void)
{
this->rsPin().set(0);
// Clear display
this->send8(0b00000001);
sleep_ms(2);
this->rsPin().set(1);
}
void LCD::print(char str[])
{
int i = 0;
while (str[i])
{
send8(str[i]);
i++;
}
}
void LCD::set_contrast(int val)
{
this->v0Pin().set_duty(val);
}
void LCD::pulse(void)
{
this->enPin().set(1);
sleep_us(40);
this->enPin().set(0);
sleep_us(80);
}
void LCD::send4(uint8_t data)
{
this->d4Pin().set((data & 0x1) >> 0);
this->d5Pin().set((data & 0x2) >> 1);
this->d6Pin().set((data & 0x4) >> 2);
this->d7Pin().set((data & 0x8) >> 3);
this->pulse();
}
void LCD::send8(uint8_t data)
{
this->send4((data & 0xf0) >> 4);
this->send4((data & 0x0f) >> 0);
}