-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cheapie_8x7_Segment_Display.ino
139 lines (124 loc) · 4.26 KB
/
Cheapie_8x7_Segment_Display.ino
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
//****************************************************************
//* Name : 74HC595 8x7 segment module *
//* Author : Robert Joseph Korn *
//* Notice : Copyright (c) 2015 Open Valley Consulting Corp *
//* : All Rights Reserved *
//* Date : 5/22/15 *
//* Version : 1.0 *
//* Notes : Cobbled together from various forum posts and *
//* : modified for a cheapie Ebay 74HC595 display. *
//* : *
//****************************************************************
#define PWR 7
#define GND 6
#define LATCH 3
#define CLOCK 4
#define DATA 5
#define MultiplexDelay 1
#define LEFT 0
#define RIGHT 1
#define BLANK 11
byte digits[8] = { 0b01111111, //digit 1 from right
0b10111111, //digit 2 from right
0b11011111, //digit 3 from right
0b11101111, //digit 4 from right
0b11110111, //digit 5 from right
0b11111011, //digit 6 from right
0b11111101, //digit 7 from right
0b11111110 //digit 8 from right
};
byte segments[12] = {0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111, // 9
0b10000000, //dot
0b00000000 //blank
};
//fucntion to send the serial data out to two 74HC595 serial to parallel shift register and activate the 7 segment.
void display8x7segment(byte datapin, byte clockpin, byte latchpin, byte digit, byte number)
{
digitalWrite(latchpin, LOW);
shiftOut(datapin, clockpin, MSBFIRST, digit);
shiftOut(datapin, clockpin, MSBFIRST, number);
digitalWrite(latchpin, HIGH);
}
//function to display value on 8x7 segment display according to the justify state
void displayNumber8x7segment(byte justify, unsigned long value)
{
byte decimal[8] = {0};
value = value % 100000000;
decimal[7] = value / 10000000;
value = value % 10000000;
decimal[6] = value / 1000000;
value = value % 1000000;
decimal[5] = value / 100000;
value = value % 100000;
decimal[4] = value / 10000;
value = value % 10000;
decimal[3] = value / 1000;
value = value % 1000;
decimal[2] = value / 100;
value = value % 100;
decimal[1] = value / 10;
decimal[0] = value % 10;
byte zero = 0;
if (justify == RIGHT)
{
for(byte e = 8; e > 0 ; e --)
{
if(zero == 0)
{
if(decimal[e-1] != 0)
{
display8x7segment(DATA, CLOCK, LATCH, digits[e-1], segments[decimal[e-1]]);
zero = 1;
}
}
else display8x7segment(DATA, CLOCK, LATCH, digits[e-1], segments[decimal[e-1]]);
delay(MultiplexDelay);
}
}
else //if justify == left
{
byte d = 0;
for(byte e = 8; e > 0; e --)
{
if(zero == 0)
{
if(decimal[e-1] != 0)
{
display8x7segment(DATA, CLOCK, LATCH, digits[7], segments[decimal[e-1]]);
zero = 1;
d ++;
delay(MultiplexDelay);
}
}
else
{
display8x7segment(DATA, CLOCK, LATCH, digits[7-d], segments[decimal[e-1]]);
d ++;
delay(MultiplexDelay);
}
}
}
display8x7segment(DATA, CLOCK, LATCH, 0b00000000, 0b00000000);
}
void setup() {
pinMode(LATCH, OUTPUT);
pinMode(CLOCK, OUTPUT);
pinMode(DATA, OUTPUT);
digitalWrite(LATCH, HIGH);
pinMode(PWR, OUTPUT);
digitalWrite(PWR, HIGH);
pinMode(GND, OUTPUT);
digitalWrite(GND, LOW);
}
void loop(){
displayNumber8x7segment(RIGHT, millis()/1000);
}