-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMax7219.cpp
More file actions
190 lines (166 loc) · 4.68 KB
/
Max7219.cpp
File metadata and controls
190 lines (166 loc) · 4.68 KB
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
/*
* File: max7219.c
* Author: syfre
* MAXIM max7219
* Created on 8 mai 2014, 21:13
*/
#include "Arduino.h"
#include "Max7219.h"
// MAX commands
#define MAX7219_NOOP 0x00
#define MAX7219_DIGIT0 0x01
#define MAX7219_DIGIT1 0x02
#define MAX7219_DIGIT2 0x03
#define MAX7219_DIGIT3 0x04
#define MAX7219_DIGIT4 0x05
#define MAX7219_DIGIT5 0x06
#define MAX7219_DIGIT6 0x07
#define MAX7219_DIGIT7 0x08
#define MAX7219_DECODEMODE 0x09
#define MAX7219_INTENSITY 0x0A
#define MAX7219_SCANLIMIT 0x0B
#define MAX7219_SHUTDOWN 0x0C
#define MAX7219_DISPLAYTEST 0x0F
#define MAX7219_SHUTDOWN_ON 0x00
#define MAX7219_SHUTDOWN_OFF 0x01
#define MAX7219_DECODEMODE_ALL_BCD 0xFF
#define BCD_0 0x00
#define BCD_1 0x01
#define BCD_2 0x02
#define BCD_3 0x03
#define BCD_4 0x04
#define BCD_5 0x05
#define BCD_6 0x06
#define BCD_7 0x07
#define BCD_8 0x08
#define BCD_9 0x09
#define BCD_MINUS 0x0A
#define BCD_E 0x0B
#define BCD_H 0x0C
#define BCD_L 0x0D
#define BCD_P 0x0E
#define BCD_BLANK 0x0F
#define BCD_DECIMAL 0x80
#define MAX7219_SCAN_0 0x00
#define MAX7219_SCAN_1 0x01
#define MAX7219_SCAN_2 0x02
#define MAX7219_SCAN_3 0x03
#define MAX7219_SCAN_4 0x04
#define MAX7219_SCAN_5 0x05
#define MAX7219_SCAN_6 0x06
#define MAX7219_SCAN_7 0x07
#define _DELAY_DATA() delayMicroseconds(1)
#define _DELAY_LOAD() delayMicroseconds(1)
Max7219::Max7219(char _pinData, char _pinClock, char _pinLoad) {
// Assign variables.
pinData = _pinData;
pinClock = _pinClock;
pinLoad = _pinLoad;
// Set pins to input.
pinMode(pinData, OUTPUT);
pinMode(pinClock, OUTPUT);
pinMode(pinLoad, OUTPUT);
reset();
}
void Max7219::_send(unsigned char b) {
digitalWrite(pinData,b);
_DELAY_DATA();
digitalWrite(pinClock,HIGH);
_DELAY_DATA();
digitalWrite(pinClock,LOW);
_DELAY_DATA();
}
void Max7219::_load() {
digitalWrite(pinLoad,HIGH);
_DELAY_LOAD();
digitalWrite(pinLoad,LOW);
_DELAY_LOAD();
}
void Max7219::_send1(unsigned char data) {
// data 8 bits
//
for (unsigned char i=0; i<8; i++) {
if (data & 0x80) {_send(HIGH); } else { _send(LOW); }
data = data << 1;
}
}
void Max7219::_send2(unsigned char addr, unsigned char data) {
_send1(addr);
_send1(data);
_load();
}
void Max7219::reset() {
// Reset maxim 7219
//
digitalWrite(pinClock,LOW);
digitalWrite(pinLoad,LOW);
//
_send2(MAX7219_SCANLIMIT, MAX7219_SCAN_7);
_send2(MAX7219_DECODEMODE, MAX7219_DECODEMODE_ALL_BCD );
_send2(MAX7219_DIGIT7,BCD_BLANK);
_send2(MAX7219_DIGIT6,BCD_BLANK);
_send2(MAX7219_DIGIT5,BCD_BLANK);
_send2(MAX7219_DIGIT4,BCD_BLANK);
_send2(MAX7219_DIGIT3,BCD_BLANK);
_send2(MAX7219_DIGIT2,BCD_BLANK);
_send2(MAX7219_DIGIT1,BCD_BLANK);
_send2(MAX7219_DIGIT0,BCD_0);
//
_send2(MAX7219_SHUTDOWN, MAX7219_SHUTDOWN_OFF);
}
void Max7219::_output_digits() {
unsigned char c;
unsigned char dp;
unsigned char j;
dp = 0; j = 1;
for (unsigned char i=1; i<=DIGIT_COUNT; i++) {
c = digits[DIGIT_COUNT-i];
switch (c) {
case '-' : _send2(j++,BCD_MINUS); break;
case '.' : dp = 0x80; break;
case ' ' : _send2(j++, BCD_BLANK); break;
case 'E' : _send2(j++, BCD_E); break;
case 'H' : _send2(j++, BCD_H); break;
case 'L' : _send2(j++, BCD_L); break;
case 'P' : _send2(j++, BCD_P); break;
default :
if ((c>=0x30)&&(c<=0x39)) {
_send2(j++, (c-0x30) | dp);
dp = 0;
}
}
}
}
void Max7219::printFixedPoint(long value) {
for (unsigned char i=0; i<4; i++) digits[i]=' ';
sprintf(digits+4,"%04d",value);
_output_digits();
_send2(MAX7219_DIGIT3,BCD_DECIMAL+digits[4]-0x30);
}
void Max7219::print(long value) {
sprintf(digits,"%8d",value);
_output_digits();
}
void Max7219::print(unsigned long value) {
sprintf(digits,"%8u",value);
_output_digits();
}
void Max7219::print(unsigned char value) {
sprintf(digits,"%8u",value);
_output_digits();
}
void Max7219::print(int value) {
sprintf(digits,"%8d",value);
_output_digits();
}
void Max7219::print(unsigned int value) {
sprintf(digits,"%8u",value);
_output_digits();
}
void Max7219::print(float value, int decimals) {
char fmt[6];
if (decimals>99) decimals = 99;
sprintf(fmt,"%%.%df",decimals);
sprintf(digits,fmt,value);
_output_digits();
}