-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
121 lines (106 loc) · 2.01 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
#include<avr/io.h>
#include<avr/interrupt.h>
#include<util/delay.h>
unsigned char hours=0,minutes=0,seconds=0,flagSec=0,flagMin=0;
void setupTimer1(void){
OCR1A=976;
TCCR1A = (1<<FOC1A)| (1<<FOC1B);
TCCR1B = (1<<CS12)|(1<<CS10)|(1<<WGM12);
TCNT1=0;
TIMSK |=(1<<OCIE1A);
// TCCR1B = (1<<WGM12) | (1<<CS11);
}
ISR(TIMER1_COMPA_vect)
{
seconds%=60;
if(seconds==59)
minutes++;
if(minutes==59 && seconds==59)
{
hours++;
minutes=0;
}
if(seconds<59)
seconds++;
else
seconds=0;
}
// reset stop watch
void INI0_Init(void){
DDRD &=~(1<<2);
PORTD|=(1<<2);
MCUCR|=(1<<ISC01); // with falling edge
MCUCR &=~(1<<ISC00);
GICR |=(1<<INT0);
}
// stop counting stop watch
void INI1_Init(void){
DDRD &=~(1<<3);
MCUCR|=(1>>ISC01) |(1<<ISC00);// with raising edge
GICR |=(1<<INT1);
}
// resume stop watch
void INI2_Init(void){
DDRB &=~(1<<2);
PORTB|=(1<<2);
MCUCR|=(1>>ISC01); // with falling edge
MCUCR &=~(1<<ISC00);
GICR |=(1<<INT2);
}
// ISR for reset
ISR(INT0_vect){
TIMSK|=(1<<OCIE1A);
PORTA &=0x00;
PORTA|=0xff;
PORTC=0;
seconds=0,minutes=0,hours=0;
}
// ISR stop
ISR(INT1_vect){
TIMSK&=~(1<<OCIE1A); // disable clock pin
}
// ISR resume
ISR(INT2_vect){
TIMSK|=(1<<OCIE1A); // enable clock pin
}
int main ()
{
SREG|=(1<<7); // enable I-global interrupt
DDRC|=0x0f; // decoder
DDRA |=0X3f; // enable 6-7 segment
PORTA |=0x00;
PORTC =0xC0;
INI0_Init();
INI1_Init();
INI2_Init();
setupTimer1();
while(1){
// displaying seconds
PORTA &=0x00;
PORTA |=0x20;
PORTC=seconds%10;
_delay_ms(2);
PORTA &=0x00;
PORTA|=0x10;
PORTC=seconds/10;
_delay_ms(2);
// displaying minutes
PORTA &=0x00;
PORTA |=0x08;
PORTC=minutes%10;
_delay_ms(2);
PORTA &=0x00;
PORTA|=0x04;
PORTC=minutes/10;
_delay_ms(2);
// displaying hours
PORTA &=0x00;
PORTA |=0x02;
PORTC =hours%10;
_delay_ms(2);
PORTA &=0x00;
PORTA |=0x01;
PORTC=hours/10;
_delay_ms(2);
}
}