-
Notifications
You must be signed in to change notification settings - Fork 15
/
stm32_tm1637.c
165 lines (140 loc) · 3.33 KB
/
stm32_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
#include "stm32l1xx_hal.h"
#include "stm32_tm1637.h"
void _tm1637Start(void);
void _tm1637Stop(void);
void _tm1637ReadResult(void);
void _tm1637WriteByte(unsigned char b);
void _tm1637DelayUsec(unsigned int i);
void _tm1637ClkHigh(void);
void _tm1637ClkLow(void);
void _tm1637DioHigh(void);
void _tm1637DioLow(void);
// Configuration.
#define CLK_PORT GPIOC
#define DIO_PORT GPIOC
#define CLK_PIN GPIO_PIN_0
#define DIO_PIN GPIO_PIN_1
#define CLK_PORT_CLK_ENABLE __HAL_RCC_GPIOC_CLK_ENABLE
#define DIO_PORT_CLK_ENABLE __HAL_RCC_GPIOC_CLK_ENABLE
const char segmentMap[] = {
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, // 0-7
0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, // 8-9, A-F
0x00
};
void tm1637Init(void)
{
CLK_PORT_CLK_ENABLE();
DIO_PORT_CLK_ENABLE();
GPIO_InitTypeDef g = {0};
g.Pull = GPIO_PULLUP;
g.Mode = GPIO_MODE_OUTPUT_OD; // OD = open drain
g.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
g.Pin = CLK_PIN;
HAL_GPIO_Init(CLK_PORT, &g);
g.Pin = DIO_PIN;
HAL_GPIO_Init(DIO_PORT, &g);
tm1637SetBrightness(8);
}
void tm1637DisplayDecimal(int v, int displaySeparator)
{
unsigned char digitArr[4];
for (int i = 0; i < 4; ++i) {
digitArr[i] = segmentMap[v % 10];
if (i == 2 && displaySeparator) {
digitArr[i] |= 1 << 7;
}
v /= 10;
}
_tm1637Start();
_tm1637WriteByte(0x40);
_tm1637ReadResult();
_tm1637Stop();
_tm1637Start();
_tm1637WriteByte(0xc0);
_tm1637ReadResult();
for (int i = 0; i < 4; ++i) {
_tm1637WriteByte(digitArr[3 - i]);
_tm1637ReadResult();
}
_tm1637Stop();
}
// Valid brightness values: 0 - 8.
// 0 = display off.
void tm1637SetBrightness(char brightness)
{
// Brightness command:
// 1000 0XXX = display off
// 1000 1BBB = display on, brightness 0-7
// X = don't care
// B = brightness
_tm1637Start();
_tm1637WriteByte(0x87 + brightness);
_tm1637ReadResult();
_tm1637Stop();
}
void _tm1637Start(void)
{
_tm1637ClkHigh();
_tm1637DioHigh();
_tm1637DelayUsec(2);
_tm1637DioLow();
}
void _tm1637Stop(void)
{
_tm1637ClkLow();
_tm1637DelayUsec(2);
_tm1637DioLow();
_tm1637DelayUsec(2);
_tm1637ClkHigh();
_tm1637DelayUsec(2);
_tm1637DioHigh();
}
void _tm1637ReadResult(void)
{
_tm1637ClkLow();
_tm1637DelayUsec(5);
// while (dio); // We're cheating here and not actually reading back the response.
_tm1637ClkHigh();
_tm1637DelayUsec(2);
_tm1637ClkLow();
}
void _tm1637WriteByte(unsigned char b)
{
for (int i = 0; i < 8; ++i) {
_tm1637ClkLow();
if (b & 0x01) {
_tm1637DioHigh();
}
else {
_tm1637DioLow();
}
_tm1637DelayUsec(3);
b >>= 1;
_tm1637ClkHigh();
_tm1637DelayUsec(3);
}
}
void _tm1637DelayUsec(unsigned int i)
{
for (; i>0; i--) {
for (int j = 0; j < 10; ++j) {
__asm__ __volatile__("nop\n\t":::"memory");
}
}
}
void _tm1637ClkHigh(void)
{
HAL_GPIO_WritePin(CLK_PORT, CLK_PIN, GPIO_PIN_SET);
}
void _tm1637ClkLow(void)
{
HAL_GPIO_WritePin(CLK_PORT, CLK_PIN, GPIO_PIN_RESET);
}
void _tm1637DioHigh(void)
{
HAL_GPIO_WritePin(DIO_PORT, DIO_PIN, GPIO_PIN_SET);
}
void _tm1637DioLow(void)
{
HAL_GPIO_WritePin(DIO_PORT, DIO_PIN, GPIO_PIN_RESET);
}