-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter.c
212 lines (177 loc) · 4.92 KB
/
counter.c
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
/*
* counter.c
*
* Created: 4/6/2019 8:46:08 PM
* Author : Sahil S. Mahajan
* Description: Simple counter 0000 to 9999
*
*/
#include <REG51F.h>
void delay(unsigned int k);
void display(void);
void incrementOnes(void);
void incrementTens(void);
void incrementHundreds(void);
void incrementThousands(void);
void resetToZero(void);
sbit sl1 = P2^7;
sbit sl2 = P2^6;
sbit sl3 = P2^5;
sbit sl4 = P2^4;
unsigned int numberList[10] = {
0xFC, //0
0x60, //1
0xDA, //2
0xF2, //3
0x66, //4
0xB6, //5
0xBE, //6
0xE0, //7
0xFE, //8
0xF6 //9
};
unsigned int numbersToDisplay[4] = {'0','0','0','0'}; //the thousands, hundreds, tens, and ones place of the count number
unsigned int digitSelector[4][4] = {
0,1,1,1, //select 1st digit on 7seg display
1,0,1,1, //select 2nd digit on 7seg display
1,1,0,1, //select 3rd digit on 7seg display
1,1,1,0 //select 4th digit on 7seg display
};
unsigned int n; /* This is just to make multiple iterations of the display function in the main function. */
/* The number of iterations will control the speed of the count. */
void main()
{
while(1)
{
for(n=0;n<29;n++)
{
display();
}
incrementOnes();
}
}
/* -----------------
* Function: delay
* -----------------
*
* creates delay for a specificed number of milliseconds
*
* k: number of milliseconds (approximate)
*
*/
void delay(unsigned int k)
{
unsigned int i;
unsigned int j;
for(i=0;i<k;i++)
{
for(j=0;j<120;j++);
}
}
/* -----------------
* Function: display
* -----------------
*
* selects one digit on the 7-segment display at a time and displays a number on the selected digit
*
*/
void display(void)
{
unsigned char x;
for(x=0;x<4;x++)
{
P0 = 0x00;
sl1 = digitSelector[x][0];
sl2 = digitSelector[x][1];
sl3 = digitSelector[x][2];
sl4 = digitSelector[x][3]; /* Digit is selected when its pin is reset. */
/* Only one of these is selected in each iteration */
P0 = numberList[numbersToDisplay[x]-'0'];
delay(4); /* 4 milliseconds seems about the right amount of time */
/* for a smooth transition to the next digit/number. */
}
}
/* -----------------
* Function: incrementOnes
* -----------------
*
* 1. Increments ones place of the count number
* 2. Calls incrementTens function if ones place increments past 9
*
*/
void incrementOnes(void)
{
numbersToDisplay[3]++;
if(numbersToDisplay[3] == '9'+1)
{
incrementTens();
}
}
/* -----------------
* Function: incrementTens
* -----------------
*
* 1. Resets ones place to zero
* 2. Increments tens place of the count number
* 3. Calls incrementHundreds function if tens place increments past 9
*
*/
void incrementTens(void)
{
numbersToDisplay[3] = '0';
numbersToDisplay[2]++;
if(numbersToDisplay[2] == '9'+1)
{
incrementHundreds();
}
}
/* -----------------
* Function: incrementHundreds
* -----------------
*
* 1. Resets tens place to zero
* 2. Increments hundreds place of the count number
* 3. Calls incrementThousands function if hundreds place increments past 9
*
*/
void incrementHundreds(void)
{
numbersToDisplay[2] = '0';
numbersToDisplay[1]++;
if(numbersToDisplay[1] == '9'+1)
{
incrementThousands();
}
}
/* -----------------
* Function: incrementThousands
* -----------------
*
* 1. Resets hundreds place to zero
* 2. Increments thousands place of the count number
* 3. Calls resetToZero function if thousands place increments past 9
*
*/
void incrementThousands(void)
{
numbersToDisplay[1] = '0';
numbersToDisplay[0]++;
if(numbersToDisplay[0] == '9'+1)
{
resetToZero();
}
}
/* -----------------
* Function: resetToZero
* -----------------
*
* resets count to 0000
*
*/
void resetToZero(void)
{
numbersToDisplay[0] = '0';
numbersToDisplay[1] = '0';
numbersToDisplay[2] = '0';
numbersToDisplay[3] = '0';
}