1
1
/*
2
- This library allows an Arduino to easily display numbers and characters on a 4 digit 7-segment
2
+ This library allows an Arduino to easily display numbers and characters on a 4 digit 7-segment
3
3
display without a separate 7-segment display controller.
4
4
5
5
If you have feature suggestions or need support please use the github support page: https://github.com/sparkfun/SevSeg
6
6
7
7
Original Library by Dean Reading (deanreading@hotmail.com: http://arduino.cc/playground/Main/SevenSegmentLibrary), 2012
8
8
Improvements by Nathan Seidle, 2012
9
9
10
- Now works for any digital pin arrangement, common anode and common cathode displays.
10
+ Now works for any digital pin arrangement, common anode and common cathode displays.
11
11
Added character support including letters A-F and many symbols.
12
12
13
- Hardware Setup: 4 digit 7 segment displays use 12 digital pins. You may need more pins if your display has colons or
13
+ Hardware Setup: 4 digit 7 segment displays use 12 digital pins. You may need more pins if your display has colons or
14
14
apostrophes.
15
15
16
- There are 4 digit pins and 8 segment pins. Digit pins are connected to the cathodes for common cathode displays, or anodes
17
- for common anode displays. 8 pins control the individual segments (seven segments plus the decimal point).
16
+ There are 4 digit pins and 8 segment pins. Digit pins are connected to the cathodes for common cathode displays, or anodes
17
+ for common anode displays. 8 pins control the individual segments (seven segments plus the decimal point).
18
18
19
- Connect the four digit pins with four limiting resistors in series to any digital or analog pins. Connect the eight segment
19
+ Connect the four digit pins with four limiting resistors in series to any digital or analog pins. Connect the eight segment
20
20
pins to any digital or analog pins (no limiting resistors needed). See the SevSeg example for more connection information.
21
-
21
+
22
22
SparkFun has a large, 1" 7-segment display that has four digits.
23
23
https://www.sparkfun.com/products/11408
24
- Looking at the display like this: 8.8.8.8. pin 1 is on the lower row, starting from the left.
24
+ Looking at the display like this: 8.8.8.8. pin 1 is on the lower row, starting from the left.
25
25
Pin 12 is the top row, upper left pin.
26
-
26
+
27
27
Pinout:
28
28
1: Segment E
29
29
2: Segment D
40
40
41
41
42
42
Software:
43
- Call SevSeg.Begin in setup.
43
+ Call SevSeg.Begin in setup.
44
44
The first argument (boolean) tells whether the display is common cathode (0) or common
45
45
anode (1).
46
46
The next four arguments (bytes) tell the library which arduino pins are connected to
47
47
the digit pins of the seven segment display. Put them in order from left to right.
48
48
The next eight arguments (bytes) tell the library which arduino pins are connected to
49
49
the segment pins of the seven segment display. Put them in order a to g then the dp.
50
-
50
+
51
51
In summary, Begin(type, digit pins 1-4, segment pins a-g, dp)
52
-
52
+
53
53
The calling program must run the DisplayString() function repeatedly to get the number displayed.
54
- Any number between -999 and 9999 can be displayed.
54
+ Any number between -999 and 9999 can be displayed.
55
55
To move the decimal place one digit to the left, use '1' as the second
56
- argument. For example, if you wanted to display '3.141' you would call
56
+ argument. For example, if you wanted to display '3.141' you would call
57
57
myDisplay.DisplayString("3141", 1);
58
58
59
-
59
+
60
60
*/
61
61
62
62
#include " SevSeg.h"
@@ -67,11 +67,11 @@ SevSeg::SevSeg()
67
67
DecAposColon = 0 ; // This variable tracks the decimal place, apostrophe, and colon (if the display has support)
68
68
69
69
}
70
- void SevSeg::Begin (boolean mode_in, byte numOfDigits,
71
- byte dig1, byte dig2, byte dig3, byte dig4,
70
+ void SevSeg::Begin (boolean mode_in, byte numOfDigits,
71
+ byte dig1, byte dig2, byte dig3, byte dig4,
72
72
byte digitCol, byte digitApos,
73
- byte segA, byte segB, byte segC, byte segD, byte segE, byte segF, byte segG,
74
- byte segDP,
73
+ byte segA, byte segB, byte segC, byte segD, byte segE, byte segF, byte segG,
74
+ byte segDP,
75
75
byte segCol, byte segApos)
76
76
{
77
77
// Bring all the variables in from the caller
@@ -92,7 +92,7 @@ void SevSeg::Begin(boolean mode_in, byte numOfDigits,
92
92
segmentDP = segDP;
93
93
segmentApostrophe = segApos;
94
94
segmentColon = segCol;
95
-
95
+
96
96
// Assign input values to variables
97
97
// mode is what the digit pins must be set at for it to be turned on. 0 for common cathode, 1 for common anode
98
98
mode = mode_in;
@@ -103,7 +103,7 @@ void SevSeg::Begin(boolean mode_in, byte numOfDigits,
103
103
SegOn = LOW;
104
104
SegOff = HIGH;
105
105
}
106
- else
106
+ else
107
107
{
108
108
DigitOn = LOW;
109
109
DigitOff = HIGH;
@@ -126,13 +126,13 @@ void SevSeg::Begin(boolean mode_in, byte numOfDigits,
126
126
127
127
// Turn everything Off before setting pin as output
128
128
// Set all digit pins off. Low for common anode, high for common cathode
129
- for (byte digit = 0 ; digit < numberOfDigits ; digit++)
129
+ for (byte digit = 0 ; digit < numberOfDigits ; digit++)
130
130
{
131
131
digitalWrite (DigitPins[digit], DigitOff);
132
132
pinMode (DigitPins[digit], OUTPUT);
133
133
}
134
134
// Set all segment pins off. High for common anode, low for common cathode
135
- for (byte seg = 0 ; seg < 8 ; seg++)
135
+ for (byte seg = 0 ; seg < 8 ; seg++)
136
136
{
137
137
digitalWrite (SegmentPins[seg], SegOff);
138
138
pinMode (SegmentPins[seg], OUTPUT);
@@ -159,9 +159,9 @@ void SevSeg::Begin(boolean mode_in, byte numOfDigits,
159
159
// Set pin modes and turns all displays off
160
160
// This second begin is used when the display does not support a colon and apostrophe
161
161
// The digitApostrophe, segmentApostrophe, and dig/segColon are set to 255 and the normal .Begin is called
162
- void SevSeg::Begin (boolean mode_in, byte numOfDigits,
163
- byte dig1, byte dig2, byte dig3, byte dig4,
164
- byte segA, byte segB, byte segC, byte segD, byte segE, byte segF, byte segG,
162
+ void SevSeg::Begin (boolean mode_in, byte numOfDigits,
163
+ byte dig1, byte dig2, byte dig3, byte dig4,
164
+ byte segA, byte segB, byte segC, byte segD, byte segE, byte segF, byte segG,
165
165
byte segDP)
166
166
{
167
167
Begin (mode_in, numOfDigits, dig1, dig2, dig3, dig4, 255 , 255 , segA, segB, segC,
@@ -189,9 +189,9 @@ void SevSeg::SetBrightness(byte percentBright)
189
189
void SevSeg::DisplayString (char * toDisplay, byte DecAposColon)
190
190
{
191
191
// For the purpose of this code, digit = 1 is the left most digit, digit = 4 is the right most digit
192
- for (byte digit = 1 ; digit < (numberOfDigits+1 ) ; digit++)
192
+ for (byte digit = 1 ; digit < (numberOfDigits+1 ) ; digit++)
193
193
{
194
- switch (digit)
194
+ switch (digit)
195
195
{
196
196
case 1 :
197
197
digitalWrite (digit1, DigitOn);
@@ -212,7 +212,7 @@ void SevSeg::DisplayString(char* toDisplay, byte DecAposColon)
212
212
// This could be cleaned up a bit but it works
213
213
// displayCharacter(toDisplay[digit-1]); //Now display this digit
214
214
// displayArray (defined in SevSeg.h) decides which segments are turned on for each number or symbol
215
- char characterToDisplay = toDisplay[digit-1 ];
215
+ unsigned char characterToDisplay = toDisplay[digit-1 ];
216
216
if (characterToDisplay & 0x80 ) // bit 7 enables bit-per-segment control
217
217
{ // Each bit of characterToDisplay turns on a single segment (from A-to-G)
218
218
if (characterToDisplay & 0x01 ) digitalWrite (segmentA, SegOn);
@@ -237,7 +237,7 @@ void SevSeg::DisplayString(char* toDisplay, byte DecAposColon)
237
237
// Service the decimal point, apostrophe and colon
238
238
if ((DecAposColon & (1 <<(digit-1 ))) && (digit < 5 )) // Test DecAposColon to see if we need to turn on a decimal point
239
239
digitalWrite (segmentDP, SegOn);
240
-
240
+
241
241
delayMicroseconds (brightnessDelay + 1 ); // Display this digit for a fraction of a second (between 1us and 5000us, 500-2000 is pretty good)
242
242
// The + 1 is a bit of a hack but it removes the possible zero display (0 causes display to become bright and flickery)
243
243
// If you set this too long, the display will start to flicker. Set it to 25000 for some fun.
@@ -253,7 +253,7 @@ void SevSeg::DisplayString(char* toDisplay, byte DecAposColon)
253
253
digitalWrite (segmentDP, SegOff);
254
254
255
255
// Turn off this digit
256
- switch (digit)
256
+ switch (digit)
257
257
{
258
258
case 1 :
259
259
digitalWrite (digit1, DigitOff);
@@ -270,11 +270,11 @@ void SevSeg::DisplayString(char* toDisplay, byte DecAposColon)
270
270
// This only currently works for 4 digits
271
271
}
272
272
// The display is on for microSeconds(brightnessLevel + 1), now turn off for the remainder of the framePeriod
273
- delayMicroseconds (FRAMEPERIOD - brightnessDelay + 1 ); // the +1 is a hack so that we can never have a delayMicroseconds(0), causes display to flicker
273
+ delayMicroseconds (FRAMEPERIOD - brightnessDelay + 1 ); // the +1 is a hack so that we can never have a delayMicroseconds(0), causes display to flicker
274
274
}
275
275
276
276
// After we've gone through the digits, we control the colon and apostrophe (if the display supports it)
277
-
277
+
278
278
// Turn on the colon and/or apostrophe
279
279
if ((digitColon != 255 ) || (digitApostrophe != 255 ))
280
280
{
@@ -294,8 +294,8 @@ void SevSeg::DisplayString(char* toDisplay, byte DecAposColon)
294
294
digitalWrite (digitColon, DigitOff);
295
295
digitalWrite (segmentColon, SegOff);
296
296
digitalWrite (digitApostrophe, DigitOff);
297
- digitalWrite (segmentApostrophe, SegOff);
297
+ digitalWrite (segmentApostrophe, SegOff);
298
298
delayMicroseconds (FRAMEPERIOD - brightnessDelay + 1 ); // the +1 is a hack so that we can never have a delayMicroseconds(0), causes display to flicker
299
299
}
300
-
300
+
301
301
}
0 commit comments