-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathHT1621.cpp
More file actions
266 lines (207 loc) · 5 KB
/
HT1621.cpp
File metadata and controls
266 lines (207 loc) · 5 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* \file HT1621.cpp
* \brief Implementation of a class for dealing with the Holtek HT1621 chip.
* \author Enrico Formenti
* \date 31 january 2015
* \version 1.0
* \copyright BSD license, check the License page on the blog for more information. All this text must be
* included in any redistribution.
* <br><br>
* See macduino.blogspot.com for more details.
*
*/
#include "HT1621.h"
byte setDigits( long number, byte *d, byte len ) // returns # of digits
{
static byte idx; // static stays
long fullrange = number;
if ( fullrange < 0L )
{
fullrange *= -1L;
}
for ( idx = 0; idx < len; idx++ )
{
d[ idx ] = 0;
}
// Here is the actual conversion to array
idx = 0;
while (( fullrange ) && ( idx < len ))
{
d[ idx++ ] = fullrange % 10;
fullrange /= 10;
}
// Finished actual conversion to array
if( number < 0 && idx < len ) {
d[ idx++ ] = 11;
}
return idx;
}
void HT1621::clear(int places)
{
for (uint8_t i = 0; i < places; i++)
write(i, 0);
}
void HT1621::flush()
{
clear();
}
void HT1621::begin()
{
pinMode(_DATA_pin, OUTPUT);
pinMode(_RW_pin, OUTPUT);
pinMode(_CS_pin, OUTPUT);
digitalWrite(_CS_pin, HIGH);
digitalWrite(_RW_pin, HIGH);
digitalWrite(_DATA_pin, HIGH);
#ifndef __HT1621_READ
register uint8_t i;
for(i=0; i<16; i++)
ram[i] = 0;
#endif
delay(10);
clear();
}
// OCIO !!!
// nell'esempio dopo ogni write viene dato un delay
// di 20 microsecondi...
void HT1621::writeBits(uint8_t data, uint8_t cnt)
{
register uint8_t i;
for(i=0;i<cnt;i++,data <<=1)
{
digitalWrite(_RW_pin, LOW);
delayMicroseconds(20);
digitalWrite(_DATA_pin, data&0x80 ? HIGH : LOW);
delayMicroseconds(20);
digitalWrite(_RW_pin, HIGH);
delayMicroseconds(20);
}
}
#ifdef __HT1621_READ
uint8_t HT1621::readBits(uint8_t cnt)
{
uint8_t data, i, state;
pinMode(_DATA_pin, INPUT);
for(i=0, data=0; i<cnt; data <<= 1, i++) {
digitalWrite(_RW_pin, LOW);
delayMicroseconds(20);
data |= (digitalRead(_DATA_pin) == HIGH);
digitalWrite(_RW_pin, HIGH);
delayMicroseconds(20);
}
pinMode(_DATA_pin, OUTPUT);
return data;
}
#endif
void HT1621::sendCommand(uint8_t cmd, bool first, bool last)
{
if (first) {
TAKE_CS();
writeBits(COMMAND_MODE, 4);
}
writeBits(cmd, 8);
if (last)
RELEASE_CS();
}
void HT1621::write(uint8_t address, uint8_t data)
{
TAKE_CS();
writeBits(WRITE_MODE, 3);
writeBits(address<<3, 6); // 6 is because max address is 128
writeBits(data, 8);
#ifndef __HT1621_READ
ram[address] = data;
#endif
RELEASE_CS();
}
void HT1621::writeChar(uint8_t address, uint8_t c, bool decimal)
{
TAKE_CS();
if( decimal )
write(address, charMap[c]|DECIMAL);
else
write(address, charMap[c]);
RELEASE_CS();
}
void HT1621::write(uint8_t address, uint8_t data, uint8_t cnt)
{
register uint8_t i;
TAKE_CS();
writeBits(WRITE_MODE, 3);
writeBits(address<<3, 6);
for (i = 0; i < cnt; i++) {
writeBits(data, 8);
#ifndef __HT1621_READ
ram[i] = data;
#endif
}
RELEASE_CS();
}
void HT1621::write(uint8_t address, uint8_t *data, uint8_t cnt)
{
register uint8_t i;
TAKE_CS();
writeBits(WRITE_MODE, 3);
writeBits(address<<3, 6);
for (i = 0; i < cnt; i++) {
writeBits(data[i], 8);
#ifndef __HT1621_READ
ram[i] = data[i];
#endif
}
RELEASE_CS();
}
#ifdef __HT1621_READ
uint8_t HT1621::read(uint8_t address)
{
uint8_t data;
TAKE_CS();
writeBits(READ_MODE, 3);
writeBits(address<<3, 6);
data = readBits(8);
RELEASE_CS();
return data;
}
void HT1621::read(uint8_t address, uint8_t *data, uint8_t cnt)
{
register uint8_t i;
TAKE_CS();
writeBits(READ_MODE, 3);
writeBits(address<<3, 6);
for (i = 0; i < cnt; i++)
data[i] = readBits(8);
RELEASE_CS();
}
#else
uint8_t HT1621::read(uint8_t address)
{
return ram[address];
}
void HT1621::read(uint8_t address, uint8_t *data, uint8_t cnt)
{
register uint8_t i;
for (i = 0; i < cnt; i++)
data[i] = ram[address+i];
}
void HT1621::printNumber(long number, int places, int dec, bool flushdisplay)
{
if ( flushdisplay ) clear();
byte d[ places ];
byte converted;
int decpoint = places - dec - 1;
int len = setDigits( number, d, places );
int pos = places - 1; //-1 as we start at index LEN-1 (end)
for (int i = 0; i < len; i++) {
bool addDec = false;
if ( pos == decpoint ) addDec = true;
writeChar(pos, d[i], addDec);
pos--;
if ( pos < 0 ) break;
}
}
void HT1621::printFloat(double number, int places, int decimalplaces, bool flushdisplay)
{
long num = (long)(number * pow(10, decimalplaces));
printNumber(num, places, decimalplaces, flushdisplay);
}
#endif