-
Notifications
You must be signed in to change notification settings - Fork 0
/
7seg_timer_interrupt.c
166 lines (130 loc) · 3.65 KB
/
7seg_timer_interrupt.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
/*
* 7seg_timer_interrupt.c
*
* Created: 4/8/2019 1:13:05 PM
* Author : Sahil S. Mahajan
* Description: Interrupt based driver for 7-segment display
*
*/
#include <REG51F.H>
void init(void);
void configureTimer0(void);
void enableInterrupt(void);
void resetTimer0(void);
void display(void);
sbit sl1 = P2^7;
sbit sl2 = P2^6;
sbit sl3 = P2^5;
sbit sl4 = P2^4;
unsigned int numbersToDisplay[4] = {3,6,9,2}; //enter 4 numbers here
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 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 x; //used with digitSelctor variable in display function to select digit
void main()
{
init();
configureTimer0();
enableInterrupt();
while(1);
}
/* -----------------
* Function: init
* -----------------
*
* clears display, initializes variables, and configures timer mode
*
*/
void init(void)
{
P0 = 0x00; //clear display
x = 0;
TMOD = 0x01; //use Timer 0 in 16-bit Timer operating mode
}
/* -----------------
* Function: configureTimer0
* -----------------
*
* sets up Timer 0 and enables timer interrupt
*
*/
void configureTimer0(void)
{
resetTimer0(); //load timer0 start value
TR0 = 1; //start Timer 0
ET0 = 1; //enable Timer 0 overflow interrupt
}
/* -----------------
* Function: enableInterrupt
* -----------------
*
* enables all interrupts that has their individual interrupt bit enabled
*
*/
void enableInterrupt(void)
{
EA = 1;
}
/* -----------------
* Function: resetTimer0
* -----------------
*
* loads Timer 0's 16 bit count register with start value
*
*/
void resetTimer0(void)
{
TH0 = 0xEF;
TL0 = 0xCD; //set to 4.5 milliseconds
}
/* -----------------
* Function: display
* -----------------
*
* selects one digit on the 7-segment display at a time and displays a number on the selected digit
*
*/
void display(void)
{
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]];
x++;
if(x == 4)
{
x = 0;
}
}
/* --------------------------------------
* Interrupt Service Routine: Timer 0 Interrupt
* --------------------------------------
*
* Runs each time timer 0 interrupt is generated
*
* resets Timer 0's count register to start value, and calls display function
*
*/
void isr_t0() interrupt 1
{
resetTimer0();
display();
}