-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTM1637.c
181 lines (138 loc) · 3.01 KB
/
TM1637.c
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
/*
* TM1637.c
*
* Created: 19/09/2020 20:50:14
* Author: Alexey Ivanov
* Driver for TM1637
* v1.0
* NOTA!! no key scanning end-user functions implemented
*/
#include "TM1637.h"
#include <avr/pgmspace.h>
const char TM1637_7SEG_Digits[] PROGMEM =
{
TM1637_0, //0
TM1637_1, //1
TM1637_2, //2
TM1637_3, //3
TM1637_4, //4
TM1637_5, //5
TM1637_6, //6
TM1637_7, //7
TM1637_8, //8
TM1637_9, //9
TM1637_A, //A
TM1637_B, //B
TM1637_C, //C
TM1637_D, //D
TM1637_E, //E
TM1637_F
};
void TM1637_Initialize()
{
TM1637_PIN_DIRECTION |= (1<<TM1637_CLK)|(1<<TM1637_DIO);
TM1637_PORT |= (1<<TM1637_CLK)|(1<<TM1637_DIO);
TM1637_SetBrightness(0);
}
void TM1637_SetBrightness(unsigned char ucBrightness)
{
TM1637_Start();
TM1637_SendByte(TM1637_DISPLAY_1_16 + ucBrightness);
TM1637_Stop();
}
void TM1637_DisplayOff()
{
TM1637_Start();
TM1637_SendByte(TM1637_DISPLAY_OFF);
TM1637_Stop();
}
static void TM1637_Start()
{
TM1637_DIO_LOW
_delay_us(TM1637_DELAY);
TM1637_CLK_LOW
_delay_us(TM1637_DELAY);
}
static void TM1637_Stop()
{
TM1637_DIO_LOW
_delay_us(TM1637_DELAY);
TM1637_CLK_HIGH
_delay_us(TM1637_DELAY);
TM1637_DIO_HIGH
_delay_us(TM1637_DELAY);
}
void TM1637_DisplayDigit(char cAdr, char cDigit)
{
TM1637_Start();
TM1637_SendByte(TM1637_WRITE_CMD);
TM1637_Stop();
TM1637_Start();
TM1637_SendByte(TM1637_SET_ADDR + cAdr);
TM1637_SendByte(cDigit);
TM1637_Stop();
}
void TM1637_DisplayAll(char cData[], char cDataLen)
{
TM1637_Start();
TM1637_SendByte(TM1637_WRITE_CMD);
TM1637_Stop();
TM1637_Start();
TM1637_SendByte(TM1637_SET_ADDR + TM1637_C0H_ADR);
for (unsigned char c=0; c<cDataLen; c++)
{
TM1637_SendByte(cData[c]);
}
TM1637_Stop();
}
static void TM1637_SendCommand (unsigned char ucCommand, unsigned char ucAdress, unsigned char data[], unsigned char ucLen, unsigned char ucDisplayControl)
{
TM1637_Start();
TM1637_SendByte(ucCommand);
TM1637_Stop();
TM1637_Start();
TM1637_SendByte(ucAdress);
for (unsigned char c=0; c<ucLen; c++)
{
TM1637_SendByte(data[c]);
}
TM1637_Start();
TM1637_SendByte(ucDisplayControl);
TM1637_Stop();
}
static void TM1637_SendByte(unsigned char ucByte)
{
unsigned char state = ucByte;
for(unsigned char ucCount = 0;ucCount<8;ucCount++)
{
_delay_us(TM1637_DELAY);
TM1637_CLK_LOW
_delay_us(TM1637_DELAY);
if (state & 0x01 )
{
TM1637_DIO_HIGH
}
else
{
TM1637_DIO_LOW
}
_delay_us(TM1637_DELAY);
TM1637_CLK_HIGH
_delay_us(TM1637_DELAY);
state = state>>1;
}
TM1637_CLK_LOW
TM1637_DIO_HIGH
TM1637_PIN_DIRECTION &= ~(1<<TM1637_DIO);
_delay_us(TM1637_DELAY);
TM1637_CLK_HIGH
_delay_us(TM1637_DELAY);
TM1637_CLK_LOW
_delay_us(TM1637_DELAY);
TM1637_PIN_DIRECTION |= (1<<TM1637_DIO);
_delay_us(TM1637_DELAY);
}
char TM1637_7SgmDigit(char cDigit)
{
return pgm_read_byte(&TM1637_7SEG_Digits[cDigit]);
}