-
Notifications
You must be signed in to change notification settings - Fork 14
/
ht1621.cpp
139 lines (122 loc) · 2.9 KB
/
ht1621.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
#include <arduino.h>
#include "HT1621.h"
#define ADDR_MAX 128
#define TAKE_CS() digitalWrite(cs_pin, LOW)
#define RELEASE_CS() digitalWrite(cs_pin, HIGH)
inline void HT1621::initControlBus()
{
pinMode(data_pin, OUTPUT);
pinMode(wr_pin, OUTPUT);
pinMode(rd_pin, OUTPUT);
pinMode(cs_pin, OUTPUT);
digitalWrite(cs_pin, HIGH);
digitalWrite(wr_pin, HIGH);
digitalWrite(rd_pin, HIGH);
digitalWrite(data_pin, HIGH);
}
inline bool HT1621::testMem()
{
uint8_t test = 10;
writeMem(0x5a, test);
//if (readMem(0x5a) != test)
// return false;
return true;
}
bool HT1621::begin()
{
initControlBus();
//delay(100); //uncomment if init fails
sendCommand(SYS_DIS);
if (! testMem())
return false;
memset(0, 0, ADDR_MAX);
sendCommand(SYS_EN);
sendCommand(LCD_ON);
return true;
}
void HT1621::writeBits(uint8_t data, uint8_t cnt)
{
uint8_t bitmask;
while (cnt) {
digitalWrite(wr_pin, LOW);
uint8_t bitval = (data & (1 << (cnt - 1))) ? HIGH : LOW;
digitalWrite(data_pin, bitval);
digitalWrite(wr_pin, HIGH);
cnt--;
}
}
uint8_t HT1621::readBits(uint8_t cnt)
{
uint8_t data = 0;
pinMode(data_pin, INPUT);
while (cnt) {
digitalWrite(rd_pin, LOW);
data += digitalRead(data_pin) << (cnt - 1);
digitalWrite(rd_pin, HIGH);
cnt--;
}
pinMode(data_pin, OUTPUT);
return data;
}
#define COMMAND_MODE 0b100
void HT1621::sendCommand(uint8_t cmd, bool first /*= true*/, bool last /*= true*/)
{
if (first) {
TAKE_CS();
writeBits(COMMAND_MODE, 3);
}
writeBits(cmd, 8);
writeBits(0, 1); //Last bit - don't care
if (last)
RELEASE_CS();
}
#define WRITE_MODE 0b101
void HT1621::writeMem(uint8_t address, uint8_t data)
{
TAKE_CS();
writeBits(WRITE_MODE, 3);
writeBits(address, 6);
writeBits(data, 4);
RELEASE_CS();
}
void HT1621::memset(uint8_t address, uint8_t data, uint8_t cnt)
{
TAKE_CS();
writeBits(WRITE_MODE, 3);
writeBits(address, 6);
for (uint8_t i = 0; i < cnt; i++)
writeBits(data, 4);
RELEASE_CS();
}
//Write up to 256 values starting from address
//Note: Data is 8-bit aligned. This is not vary efficient
void HT1621::write(uint8_t address, uint8_t *data, uint8_t cnt)
{
TAKE_CS();
writeBits(WRITE_MODE, 3);
writeBits(address, 6);
for (uint8_t i = 0; i < cnt; i++)
writeBits(data[i], 4);
RELEASE_CS();
}
#define READ_MODE 0b110
uint8_t HT1621::readMem(uint8_t address)
{
uint8_t data;
TAKE_CS();
writeBits(READ_MODE, 3);
writeBits(address, 6);
data = readBits(4);
RELEASE_CS();
return data;
}
#define READ_MODE 0b110
void HT1621::read(uint8_t address, uint8_t *data, uint8_t cnt)
{
TAKE_CS();
writeBits(READ_MODE, 3);
writeBits(address, 6);
for (uint8_t i = 0; i < cnt; i++)
data[i] = readBits(4);
RELEASE_CS();
}