/
MyTM1637.cpp
221 lines (210 loc) · 5.73 KB
/
MyTM1637.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
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
// Author:Frankie.Chu
// Date:9 April,2012
//
// 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.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
// Modified record:
//
/*******************************************************************************/
#include "MyTM1637.h"
#include <Arduino.h>
static int8_t TubeTabF[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//0~9,A,b,C,d,E,F
static int8_t TubeTabB[] = {0x3f,0x30,0x5b,0x79,0x74,0x6d,0x6f,0x38,0x7f,0x7d,0x7e,0x67,0x0f,0x73,0x4f,0x4e};//0~9,A,b,C,d,E,F
MyTM1637::MyTM1637(uint8_t Clk, uint8_t Data)
{
Clkpin = Clk;
Datapin = Data;
pinMode(Clkpin,OUTPUT);
pinMode(Datapin,OUTPUT);
}
void MyTM1637::init(void)
{
clearDisplay();
}
int MyTM1637::writeByte(int8_t wr_data)
{
uint8_t i,count1;
for(i=0;i<8;i++) //sent 8bit data
{
digitalWrite(Clkpin,LOW);
if(wr_data & 0x01)digitalWrite(Datapin,HIGH);//LSB first
else digitalWrite(Datapin,LOW);
wr_data >>= 1;
digitalWrite(Clkpin,HIGH);
}
digitalWrite(Clkpin,LOW); //wait for the ACK
digitalWrite(Datapin,HIGH);
digitalWrite(Clkpin,HIGH);
pinMode(Datapin,INPUT);
#if 0
while(digitalRead(Datapin))
{
count1 +=1;
if(count1 == 200)//
{
pinMode(Datapin,OUTPUT);
digitalWrite(Datapin,LOW);
count1 =0;
}
pinMode(Datapin,INPUT);
}
pinMode(Datapin,OUTPUT);
#endif
bitDelay();
uint8_t ack = digitalRead(Datapin);
if (ack == 0)
{
pinMode(Datapin,OUTPUT);
digitalWrite(Datapin,LOW);
}
bitDelay();
pinMode(Datapin,OUTPUT);
bitDelay();
return ack;
}
//send start signal to TM1637
void MyTM1637::start(void)
{
digitalWrite(Clkpin,HIGH);//send start signal to TM1637
digitalWrite(Datapin,HIGH);
digitalWrite(Datapin,LOW);
digitalWrite(Clkpin,LOW);
}
//End of transmission
void MyTM1637::stop(void)
{
digitalWrite(Clkpin,LOW);
digitalWrite(Datapin,LOW);
digitalWrite(Clkpin,HIGH);
digitalWrite(Datapin,HIGH);
}
//display function.Write to full-screen.
void MyTM1637::display(int8_t DispData[],uint8_t DispFlag)
{
int8_t SegData[4];
uint8_t i;
if(DispFlag == DISPLAY_FLAG_F)
{
for(i = 0;i < 4;i ++)
{
SegData[i] = DispData[i];
}
}
if(DispFlag == DISPLAY_FLAG_B)
{
for(i = 0;i < 4;i ++)
{
SegData[3 - i] = DispData[i];
}
}
coding(SegData,DispFlag);
start(); //start signal sent to TM1637 from MCU
writeByte(ADDR_AUTO);//
stop(); //
start(); //
writeByte(Cmd_SetAddr);//
for(i=0;i < 4;i ++)
{
writeByte(SegData[i]); //
}
stop(); //
start(); //
writeByte(Cmd_DispCtrl);//
stop(); //
}
//******************************************
void MyTM1637::display(uint8_t BitAddr,int8_t DispData,uint8_t DispFlag)
{
int8_t SegData;
SegData = coding(DispData,DispFlag);
start(); //start signal sent to TM1637 from MCU
writeByte(ADDR_FIXED);//
stop(); //
start(); //
if(DispFlag == DISPLAY_FLAG_F)writeByte(BitAddr|0xc0);//
if(DispFlag == DISPLAY_FLAG_B)writeByte((3 - BitAddr)|0xc0);//
writeByte(SegData);//
stop(); //
start(); //
writeByte(Cmd_DispCtrl);//
stop(); //
}
void MyTM1637::clearDisplay(void)
{
display(0x00,0x7f,0x00);
display(0x01,0x7f,0x00);
display(0x02,0x7f,0x00);
display(0x03,0x7f,0x00);
}
//To take effect the next time it displays.
void MyTM1637::set(uint8_t brightness,uint8_t SetData,uint8_t SetAddr)
{
Cmd_SetData = SetData;
Cmd_SetAddr = SetAddr;
Cmd_DispCtrl = 0x88 + brightness;//Set the brightness and it takes effect the next time it displays.
}
//Whether to light the clock point ":".
//To take effect the next time it displays.
void MyTM1637::point(boolean PointFlag)
{
_PointFlag = PointFlag;
}
void MyTM1637::coding(int8_t DispData[],uint8_t DispFlag)
{
uint8_t PointData;
if(DispFlag == DISPLAY_FLAG_F)
{
if(_PointFlag == POINT_ON)PointData = 0x80;
else PointData = 0;
for(uint8_t i = 0;i < 4;i ++)
{
if(DispData[i] == 0x7f)DispData[i] = 0x00;
else DispData[i] = TubeTabF[DispData[i]] + PointData;
}
}
if(DispFlag == DISPLAY_FLAG_B)
{
if(_PointFlag == POINT_ON)PointData = 0x80;
else PointData = 0;
for(uint8_t i = 0;i < 4;i ++)
{
if(DispData[i] == 0x7f)DispData[i] = 0x00;
else DispData[i] = TubeTabB[DispData[i]] + PointData;
}
}
}
int8_t MyTM1637::coding(int8_t DispData,uint8_t DispFlag)
{
uint8_t PointData;
if(DispFlag == DISPLAY_FLAG_F)
{
if(_PointFlag == POINT_ON)PointData = 0x80;
else PointData = 0;
if(DispData == 0x7f) DispData = 0x00 + PointData;//The bit digital tube off
else DispData = TubeTabF[DispData] + PointData;
}
if(DispFlag == DISPLAY_FLAG_B)
{
if(_PointFlag == POINT_ON)PointData = 0x80;
else PointData = 0;
if(DispData == 0x7f) DispData = 0x00 + PointData;//The bit digital tube off
else DispData = TubeTabB[DispData] + PointData;
}
return DispData;
}
void MyTM1637::bitDelay(void)
{
delayMicroseconds(50);
}