-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtm1637.c
executable file
·237 lines (188 loc) · 4.65 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
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
/**
* Copyright (c) 2017-2018, Łukasz Marcin Podkalicki <lpodkalicki@gmail.com>
*
* This is ATtiny13/25/45/85 library for 4-Digit LED Display based on TM1637 chip.
*
* Features:
* - display raw segments
* - display digits
* - display colon
* - display on/off
* - brightness control
*
* References:
* - library: https://github.com/lpodkalicki/attiny-tm1637-library
* - documentation: https://github.com/lpodkalicki/attiny-tm1637-library/README.md
* - TM1637 datasheet: https://github.com/lpodkalicki/attiny-tm1637-library/blob/master/docs/TM1637_V2.4_EN.pdf
*/
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/cpufunc.h>
#include "tm1637.h"
void clock_delay() { _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); }
#define TM1637_DIO_HIGH() (PORTC |= _BV(TM1637_DIO_PIN))
#define TM1637_DIO_LOW() (PORTC &= ~_BV(TM1637_DIO_PIN))
#define TM1637_DIO_OUTPUT() (DDRC |= _BV(TM1637_DIO_PIN))
#define TM1637_DIO_INPUT() (DDRC &= ~_BV(TM1637_DIO_PIN))
#define TM1637_DIO_READ() (((PINC & _BV(TM1637_DIO_PIN)) > 0) ? 1 : 0)
#define TM1637_CLK_HIGH() (PORTC |= _BV(TM1637_CLK_PIN))
#define TM1637_CLK_LOW() (PORTC &= ~_BV(TM1637_CLK_PIN))
static void TM1637_send_config(const uint8_t enable, const uint8_t brightness);
static void TM1637_send_command(const uint8_t value);
static void TM1637_start(void);
static void TM1637_stop(void);
static uint8_t TM1637_write_byte(uint8_t value);
static uint8_t _config = TM1637_SET_DISPLAY_ON | TM1637_BRIGHTNESS_MAX;
static uint8_t _segments = 0xff;
PROGMEM const uint8_t _digit2segments[] =
{
0x3F, // 0
0x06, // 1
0x5B, // 2
0x4F, // 3
0x66, // 4
0x6D, // 5
0x7D, // 6
0x07, // 7
0x7F, // 8
0x6F, // 9
0x39, // C
0x40, // -
};
void I2C_Enable()
{
DDRC |= _BV(TM1637_DIO_PIN) | _BV(TM1637_CLK_PIN); //output pins
}
void I2C_Disable()
{
DDRC &= ~(_BV(TM1637_DIO_PIN) | _BV(TM1637_CLK_PIN)); //input pins
}
void TM1637_init(const uint8_t enable, const uint8_t brightness)
{
TM1637_send_config(enable, brightness);
}
void TM1637_enable(const uint8_t value)
{
TM1637_send_config(value, _config & TM1637_BRIGHTNESS_MAX);
}
void TM1637_set_brightness(const uint8_t value)
{
TM1637_send_config(_config & TM1637_SET_DISPLAY_ON,
value & TM1637_BRIGHTNESS_MAX);
}
void TM1637_display_segments(const uint8_t position, const uint8_t segments)
{
TM1637_send_command(TM1637_CMD_SET_DATA | TM1637_SET_DATA_F_ADDR);
TM1637_start();
TM1637_write_byte(TM1637_CMD_SET_ADDR | (position & (TM1637_POSITION_MAX - 1)));
TM1637_write_byte(segments);
TM1637_stop();
}
void TM1637_display_digit(const uint8_t position, const uint8_t digit)
{
uint8_t segments = (digit < 10 ? pgm_read_byte_near((uint8_t *)&_digit2segments + digit) : 0x00);
if (position == 0x01)
{
segments = segments | (_segments & 0x80);
_segments = segments;
}
TM1637_display_segments(position, segments);
}
void TM1637_display_char(const uint8_t position, const uint8_t c)
{
uint8_t segments = (c < 12 ? pgm_read_byte_near((uint8_t *)&_digit2segments + c) : 0x00);
if (position == 0x01)
{
segments = segments | (_segments & 0x80);
_segments = segments;
}
TM1637_display_segments(position, segments);
}
void TM1637_display_colon(const uint8_t value)
{
if (value)
{
_segments |= 0x80;
}
else
{
_segments &= ~0x80;
}
TM1637_display_segments(0x01, _segments);
}
void TM1637_clear(void)
{
uint8_t i;
for (i = 0; i < TM1637_POSITION_MAX; ++i)
{
TM1637_display_segments(i, 0x00);
}
}
void TM1637_send_config(const uint8_t enable, const uint8_t brightness)
{
_config = (enable ? TM1637_SET_DISPLAY_ON : TM1637_SET_DISPLAY_OFF) |
(brightness > TM1637_BRIGHTNESS_MAX ? TM1637_BRIGHTNESS_MAX : brightness);
TM1637_send_command(TM1637_CMD_SET_DSIPLAY | _config);
}
void TM1637_send_command(const uint8_t value)
{
TM1637_start();
TM1637_write_byte(value);
TM1637_stop();
}
void TM1637_start(void)
{
I2C_Enable();
TM1637_DIO_HIGH();
TM1637_CLK_HIGH();
clock_delay();
TM1637_DIO_LOW();
}
void TM1637_stop(void)
{
TM1637_CLK_LOW();
clock_delay();
TM1637_DIO_LOW();
clock_delay();
TM1637_CLK_HIGH();
clock_delay();
TM1637_DIO_HIGH();
I2C_Disable();
}
uint8_t TM1637_write_byte(uint8_t value)
{
uint8_t ack=0;
for (uint8_t i = 0; i < 8; ++i)
{
TM1637_CLK_LOW();
clock_delay();
if (value & 0x01)
{
TM1637_DIO_HIGH();
}
else
{
TM1637_DIO_LOW();
}
TM1637_CLK_HIGH();
clock_delay();
value >>= 1;
}
TM1637_CLK_LOW();
TM1637_DIO_INPUT();
TM1637_DIO_HIGH();
clock_delay();
ack = TM1637_DIO_READ();
if (ack)
{
TM1637_DIO_OUTPUT();
TM1637_DIO_LOW();
}
clock_delay();
TM1637_CLK_HIGH();
clock_delay();
TM1637_CLK_LOW();
clock_delay();
TM1637_DIO_OUTPUT();
return ack;
}