-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSR3W.h
More file actions
178 lines (168 loc) · 4.96 KB
/
SR3W.h
File metadata and controls
178 lines (168 loc) · 4.96 KB
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
/**
* @file LCD/Adapter/SR3W.h
* @version 1.0
*
* @section License
* Copyright (C) 2017, Mikael Patel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#ifndef LCD_ADAPTER_SR3W_H
#define LCD_ADAPTER_SR3W_H
#include "LCD.h"
#include "Driver/HD44780.h"
#include "GPIO.h"
/**
* HD44780 (LCD-II) Dot Matix Liquid Crystal Display Controller/Driver
* Shift Register 3-Wire Port (SR3W), 74HC595/74HC164 (SR[pin]),
* with digital output pins.
*
* @param[in] SDA_PIN serial data pin.
* @param[in] SCL_PIN serial clock pin.
* @param[in] EN_PIN enable pulse.
*
* @section Circuit
* @code
* 74HC595 (VCC)
* +----U----+ |
* (LCD D5)------------1-|Q1 VCC|-16-+
* (LCD D6)------------2-|Q2 Q0|-15--------(LCD D4)
* (LCD D7)------------3-|Q3 SER|-14-------(SDA_PIN)
* (LCD RS)------------4-|Q4 /OE|-13-----------(GND)
* (LCD BT)------------5-|Q5 RCLK|-12--------(EN_PIN)
* 6-|Q6 SCLK|-11-------(SCL_PIN)
* 7-|Q7 /MR|-10-----------(VCC)
* +-8-|GND Q6'|--9
* | +---------+
* | 0.1uF
* (GND)-----||----(VCC)
* (LCD EN)------------------------------------(EN_PIN)
* (LCD RW)---------------------------------------(GND)
* (LCD K)----------------------------------------(GND)
* (LCD A)-----------------[330]------------------(VCC)
* @endcode
*
* Alternative circuit with 74HC164.
* @code
* 74HC164 (VCC)
* +----U----+ |
* (SDA_PIN)---------+-1-|DSA VCC|-14-+
* +-2-|DSB Q7|-13
* (LCD D4)------------3-|Q0 Q6|-12
* (LCD D5)------------4-|Q1 Q5|-11--------(LCD BT)
* (LCD D6)------------5-|Q2 Q4|-10--------(LCD RS)
* (LCD D7)------------6-|Q3 /MR|--9-----------(VCC)
* +-7-|GND CP|--8-------(SCL_PIN)
* | +---------+
* | 0.1uF
* (GND)-----||----(VCC)
* (LCD EN)---------------------------------------(EN/D5)
* (LCD RW)---------------------------------------(GND)
* (LCD K)----------------------------------------(GND)
* (LCD A)-----------------[330]------------------(VCC)
* @endcode
*/
namespace LCD {
template<BOARD::pin_t SDA_PIN, BOARD::pin_t SCL_PIN, BOARD::pin_t EN_PIN>
class SR3W : public HD44780::Adapter {
public:
/**
* Construct HD44780 3-wire serial adapter and initiate pins.
*/
SR3W() :
HD44780::Adapter(),
m_port()
{
m_sda.output();
m_scl.output();
m_en.output();
}
/**
* @override{HD44780::Adapter}
* Use 4-bit mode. Returns false.
* @return bool.
*/
virtual bool setup()
{
return (false);
}
/**
* @override{HD44780::Adapter}
* Write 4-bit data to display using shift register.
* @param[in] data (4b) to write.
*/
virtual void write4b(uint8_t data)
{
m_port.data = data;
data = m_port;
uint8_t mask = 0x20;
do {
m_sda = data & mask;
m_scl.toggle();
mask >>= 1;
m_scl.toggle();
} while (mask);
m_en.toggle();
m_en.toggle();
}
/**
* @override{HD44780::Adapter}
* Write byte (8bit) to display.
* @param[in] data (8b) to write.
*/
virtual void write8b(uint8_t data)
{
write4b(data >> 4);
write4b(data);
delayMicroseconds(SHORT_EXEC_TIME);
}
/**
* @override{HD44780::Adapter}
* Set instruction/data mode using given rs pin; zero for
* instruction, non-zero for data mode.
* @param[in] flag.
*/
virtual void set_mode(uint8_t flag)
{
m_port.rs = flag;
}
/**
* @override{HD44780::Adapter}
* Set backlight on/off using bt pin.
* @param[in] flag.
*/
virtual void set_backlight(uint8_t flag)
{
m_port.bt = flag;
}
protected:
/** Execution time delay (us). */
static const uint16_t SHORT_EXEC_TIME = 32;
/** Shift register port bit fields; little endian. */
union port_t {
uint8_t as_uint8; //!< Unsigned byte access.
struct {
uint8_t data:4; //!< Data port (Q0..Q3).
uint8_t rs:1; //!< Command/Data select (Q4).
uint8_t bt:1; //!< Back-light control (Q5).
uint8_t app2:1; //!< Application bit#2 (Q6).
uint8_t app1:1; //!< Application bit#1 (Q7).
};
operator uint8_t() { return (as_uint8); }
port_t() { as_uint8 = 0; }
};
port_t m_port; //!< Port setting.
GPIO<SDA_PIN> m_sda; //!< Serial data output.
GPIO<SCL_PIN> m_scl; //!< Serial clock.
GPIO<EN_PIN> m_en; //!< Starts data read/write.
};
};
#endif