-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
138 lines (113 loc) · 3.25 KB
/
main.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
/*
Author:Jabez Winston C
14-01-2016
Simple Race Stop Watch
*/
//Uses six 7-segment displays in multiplexed form;uses timer1 interrupt to refresh them
#if defined(__C51__)
#include<reg52.h>
#elif defined(SDCC)
#include<8052.h>
typedef __bit bit;
/*Alert : This is an ugly hack ! This can backfire :-D */
#define interrupt __interrupt
#else
#error "Unsupported compiler !"
#endif
void Timer_Init();
#if defined(SDCC)
void timer_100ms() interrupt 1;
#endif
void Interrupts_Configure();
void display(unsigned int,bit); //Display time at 'Current Lap' / 'Prev. Lap' of 7-segment display
void delay_small();
unsigned long int t=0,t2=0;
bit full=0; //bit datatype is a C51 extexsion for standard C
unsigned char sw;
char seven_seg_code[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
//Equivalent values for 0, 1, 2, 3, 4, 5, 6, 8, 9 in 7 segment display
void main()
{
Timer_Init();
Interrupts_Configure();
while(1); //Wait indefinitely for an interrupt to occur
}
void timer_100ms() interrupt 1 //Invoked every 50 ms
{
full=!full;
if(full)
t++; //Incremented by 1 every 100ms
}
void Timer_Init()
{
TMOD=0x11; //Timer0 in mode 1 and Timer1 in mode 1
//TH0 and TL0 values calculated for 20 MHz
TH0=0x4B;
TL0=0xFE;
//TH1 and TL1 values were calculated by trial and error method to get optimal refresh rate
TH1=0xDC;
TL1=0x00;
TR1=1; //Start Timer1
}
void Interrupts_Configure()
{
EA=1; //Enable Interrupts
ET0=1; //Enable Timer0 interrupt
ET1=1; //Enable Timer1 interrupt
EX0=1; //Enable External Interrupt 0
EX1=1; //Enable External Interrupt 1
IT0=1; //Make External interrupt 0 as edge triggered
IT1=1; //Make External interrupt 1 as edge triggered
}
void Timer_Start_Stop() interrupt 0 //External interrupt 0 (P3.2)
{
TR0=!TR0; //Start Timer if Stopped , Stop Timer if running when START button is pushed
}
void Timer_Lap() interrupt 2 //External interrupt 0 (P3.3)
{
t2=t; //Save Laptime
t=0; //Reset timer
}
void Refresh() interrupt 3 //Timer1 Interrupt
{
TF1=0; //Clear Timer Overflow Flag
TR1=0; //Stop Timer1
TH1=0x00;
TL1=0x00;
display(t,0); //Display running time in 'Current lap' portion of 7 seg
display(t2,1); //Display running time in 'Prev lap' portion of 7 seg
TR1=1;
}
void display(unsigned int t_copy,bit s)
{
unsigned char j,n;
if(!s)
for(j=0;t_copy!=0;j++)
{
n=t_copy%10; //Break the number to individual digits
P2=(1<<j); //Switch from one 7 seg display to other
if(j==1)
P1=~((1<<7)+seven_seg_code[n]); // (1<<7) for display number with decimal point
else
P1=~seven_seg_code[n];
delay_small();
t_copy=t_copy/10; //Break the number to individual digits
}
else
for(j=3;t_copy!=0;j++)
{
n=t_copy%10; //Break the number to individual digits
P2=(1<<j); //Switch from one 7 seg display to other
if(j==4)
P1=~((1<<7)+seven_seg_code[n]); // (1<<7) for decimal point
else
P1=~seven_seg_code[n];
delay_small();
t_copy=t_copy/10; //Break the number to individual digits
}
}
void delay_small()
{
int k;
for(k=0;k<1000;k++);
}