-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
149 lines (126 loc) · 3.77 KB
/
main.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
#include "mbed.h"
#define max7219_reg_noop 0x00
#define max7219_reg_digit0 0x01
#define max7219_reg_digit1 0x02
#define max7219_reg_digit2 0x03
#define max7219_reg_digit3 0x04
#define max7219_reg_digit4 0x05
#define max7219_reg_digit5 0x06
#define max7219_reg_digit6 0x07
#define max7219_reg_digit7 0x08
#define max7219_reg_decodeMode 0x09
#define max7219_reg_intensity 0x0a
#define max7219_reg_scanLimit 0x0b
#define max7219_reg_shutdown 0x0c
#define max7219_reg_displayTest 0x0f
#define LOW 0
#define HIGH 1
SPI max72_spi(PTD2, NC, PTD1);
DigitalOut load(PTD0); //will provide the load signal
DigitalOut gpo(PTD6);
AnalogIn Ain(PTB0);
AnalogOut Aout(PTE30);
Timer Talwayson;
unsigned int current, j, seg;
char row[8] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
unsigned int high = 65000;
unsigned int low = 0;
void write_to_max( int reg, int col)
{
load = LOW; // begin
max72_spi.write(reg); // specify register
max72_spi.write(col); // put data
load = HIGH; // make sure data is loaded (on rising edge of LOAD/CS)
}
//writes 8 bytes to the display
void pattern_to_display(char *testdata){
int cdata;
for(int idx = 0; idx <= 7; idx++) {
cdata = testdata[idx];
write_to_max(idx+1,cdata);
}
}
void setup_dot_matrix ()
{
// initiation of the max 7219
// SPI setup: 8 bits, mode 0
max72_spi.format(8, 0);
max72_spi.frequency(100000); //down to 100khx easier to scope ;-)
write_to_max(max7219_reg_scanLimit, 0x07);
write_to_max(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits)
write_to_max(max7219_reg_shutdown, 0x01); // not in shutdown mode
write_to_max(max7219_reg_displayTest, 0x00); // no display test
for (int e=1; e<=8; e++) { // empty registers, turn all LEDs off
write_to_max(e,0);
}
// maxAll(max7219_reg_intensity, 0x0f & 0x0f); // the first 0x0f is the value you can set
write_to_max(max7219_reg_intensity, 0x08);
}
void clear(){
for (int e=1; e<=8; e++) { // empty registers, turn all LEDs off
write_to_max(e,0);
}
}
void shift(char *data) {
gpo = 1;
for (int j=0;j<7;j++) {
data[j] = data[j+1];
}
data[7] = 0;
gpo = 0;
}
int main()
{
setup_dot_matrix (); /* setup matrix */
while(1){
gpo=1;
current=Ain.read_u16();
seg = high/8;
if (current < seg) {
shift(row);
row[7] = 0x1;
pattern_to_display(row);
}
else if (current < (seg * 2)) {
shift(row);
row[7] = 0x2;
pattern_to_display(row);
}
else if (current < (seg * 3)) {
shift(row);
row[7] = 0x4;
pattern_to_display(row);
}
else if (current < (seg * 4)) {
shift(row);
row[7] = 0x8;
pattern_to_display(row);
}
else if (current < (seg * 5)) {
shift(row);
row[7] = 0x10;
pattern_to_display(row);
}
else if (current < (seg * 6)) {
shift(row);
row[7] = 0x20;
pattern_to_display(row);
}
else if (current < (seg * 7)) {
shift(row);
row[7] = 0x40;
pattern_to_display(row);
}
else if (current < (seg * 8)) {
shift(row);
row[7] = 0x80;
pattern_to_display(row);
}
else {
char row[] = {0, 0x7e, 0x46, 0x4a, 0x52, 0x62, 0x7e, 0x0};
pattern_to_display(row);
}
Aout.write_u16(current);
wait_ms(50);
}
}