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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
#include "project.h"
#define NCS (GPIO7)
#define NCS_PORT GPIOG
#define SCK (GPIO3)
#define SCK_PORT GPIOB
#define MOSI (GPIO5)
#define MOSI_PORT GPIOB
static void
set (int sck, int ncs, int mosi)
{
if (sck)
SET (SCK);
else
CLEAR (SCK);
if (ncs)
SET (NCS);
else
CLEAR (NCS);
if (mosi)
SET (MOSI);
else
CLEAR (MOSI);
// delay_us(1);
//delay_us(10);
}
static void
spip_send_8 (uint8_t wot)
{
int i;
for (i = 0; i < 8; ++i) {
set (0, 0, wot & 0x80);
set (1, 0, wot & 0x80);
set (0, 0, wot & 0x80);
wot <<= 1;
}
}
static void
write_reg (uint8_t reg, uint8_t data)
{
set (0, 1, 0);
set (0, 0, 0);
spip_send_8 (reg);
spip_send_8 (data);
spip_send_8 (reg);
spip_send_8 (data);
spip_send_8 (reg);
spip_send_8 (data);
set (0, 0, 0);
set (0, 1, 0);
}
static void
write_regs (uint8_t reg, uint8_t data1, uint8_t data2, uint8_t data3)
{
set (0, 1, 0);
set (0, 0, 0);
spip_send_8 (reg);
spip_send_8 (data3);
spip_send_8 (reg);
spip_send_8 (data2);
spip_send_8 (reg);
spip_send_8 (data1);
set (0, 0, 0);
set (0, 1, 0);
}
#define SDP 0x80
#define SA 0x40
#define SB 0x20
#define SC 0x10
#define SD 0x08
#define SE 0x04
#define SF 0x02
#define SG 0x01
static uint8_t hex (unsigned v)
{
switch (v) {
case 0:
case '0':
return SA | SF | SB | SE | SC | SD;
case 1:
case '1':
return SB | SC;
case 2:
case '2':
case 'z':
return SA | SB | SG | SE | SD;
case 3:
case '3':
return SA | SB | SG | SC | SD;
case 4:
case '4':
return SF | SG | SB | SC;
case 5:
case '5':
case 's':
return SA | SF | SG | SC | SD;
case 6:
case '6':
return SA | SF | SG | SE | SC | SD;
case 7:
case '7':
return SA | SB | SC;
case 8:
case '8':
return SA | SF | SB | SG | SE | SC | SD;
case 9:
case '9':
case 'g':
return SA | SF | SB | SG | SC | SD;
case 0xa:
case 'a':
return SA | SF | SB | SG | SE | SC;
case 0xb:
case 'b':
return SF | SG | SE | SC | SD;
case 0xc:
case 'c':
return SG | SE | SD;
case 0xd:
case 'd':
return SB | SG | SE | SC | SD;
case 0xe:
case 'e':
return SA | SF | SG | SE | SD;
case 0xf:
case 'f':
return SA | SF | SG | SE;
case 'h':
return SF | SG | SE | SC;
case 'i':
return SE;
case 'j':
return SB | SC | SD;
case 'k':
case 'x':
return SF | SB | SG | SE | SC;
case 'l':
return SF | SE | SD;
case 'm':
case 'n':
return SG | SE | SC;
case 'o':
return SG | SE | SC | SD;
case 'p':
return SA | SF | SB | SG | SE;
case 'q':
return SA | SF | SB | SG | SC;
case 'r':
return SG | SE;
case 't':
return SF | SG | SE | SD;
case 'u':
case 'w':
return SE | SC | SD;
case 'y':
return SF | SB | SG | SC | SD;
case '-':
return SG;
case '.':
return SDP;
}
return 0;
}
void max7219_report_addr (uint32_t baseaddr)
{
unsigned i;
for (i = 1; i <= 8; ++i) {
write_regs (i, hex (baseaddr & 0xf), 0, 0);
baseaddr >>= 4;
}
}
void
max7219 (const char *str)
{
MAP_OUTPUT_PP (SCK);
MAP_OUTPUT_PP (NCS);
MAP_OUTPUT_PP (MOSI);
unsigned reg;
set (0, 1, 0);
write_reg (0xc, 0x1); //Power up
write_reg (0xf, 0x0); //normal mode
write_reg (0x9, 0x0); //No decode
write_reg (0xb, 0x7); //8 digits
write_regs (0xa, 15, 15, 15); //max brightness
for (reg = 8; reg && *str; reg--, str++)
write_regs (reg, hex (*str) | (!str[1] ? SDP : 0), 0, 0);
for (; reg; reg--)
write_reg (reg, 0);
}
|