summaryrefslogtreecommitdiff
path: root/main.c.c
blob: 9ab0d748ff69cc5d6d49f5d69c7fd7e7e5bdc33e (plain)
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
/****************************************************************************/
/* Bausatz BIN-Hourglass ver.1.0 ********************************************/
/* main.c *******************************************************************/
/* Pollin Electronic GmbH ***************************************************/
/* 85104 Pförring ***********************************************************/
/* www.pollin.de ************************************************************/
/****************************************************************************/
/* Author: Leonhard Hesse ***************************************************/
/****************************************************************************/

#define F_CPU 2000000UL

#include <avr/io.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "binTime.h"
#include "setTime.h"

volatile uint8_t tasterZaeler = 4;
volatile uint16_t clockDelay = 0;

/****************************************************************************/
/* interrupt routine user button 1 ******************************************/
/****************************************************************************/

ISR(INT0_vect) {
	if (tasterZaeler >= 2) {
		tasterZaeler = 1;
		hour++;
		clockDelay = 0;
		_delay_ms(200);
	}
}

/****************************************************************************/
/* interrupt routine user button 2 ******************************************/
/****************************************************************************/

ISR(INT1_vect) {
	if (tasterZaeler >= 2) {
		tasterZaeler = 1;
		minute++;
		clockDelay = 0;
		_delay_ms(200);
	}
}

/****************************************************************************/
/* interrupt routine for 16-Bit-Timer to count seconds **********************/
/****************************************************************************/

ISR (TIMER1_COMPA_vect) {
	second++;
}

/****************************************************************************/
/* implement methods to light the LEDs for ca. 0,1 ms ***********************/
/****************************************************************************/

void showHour() {
	for (int i = 0; i < 200; i++) {
		hourOn();
	}
	hourOff();
}
void showMinute() {
	for (int i = 0; i < 200; i++) {
		minuteOn();
	}
	minuteOff();
}
void showSecond() {
	for (int i = 0; i < 200; i++) {
		secondOn();
	}
	secondOff();
}
void showTime() {
	showHour();
	showMinute();
	showSecond();
}

/****************************************************************************/
/* implement methods to switch off the LEDs for ca. 0,1 ms ******************/
/****************************************************************************/
void hideHour() {
	for (int i = 0; i < 200; i++) {
		hourOff();
	}
}
void hideMinute() {
	for (int i = 0; i < 200; i++) {
		minuteOff();
	}
}
void hideSecond() {
	for (int i = 0; i < 200; i++) {
		secondOff();
	}
}
void hideTime() {
	hideHour();
	hideMinute();
	hideSecond();
}

int main(void) {

	/************************************************************************/
	/* initialize ports and external interrupts *****************************/
	/************************************************************************/

	DDRB |= (1 << PB0) | (1 << PB1) | (1 << PB2) | (1 << PB3) | (1 << PB4)
			| (1 << PB5) | (1 << PB6) | (1 << PB7);
	DDRD |= (1 << PD4) | (1 << PD5) | (1 << PD6);
	DDRD &= ~((1 << PD0) | (1 << PD1));
	PORTD |= (1 << PD2) | (1 << PD3);
	GIMSK |= (1 << INT0) | (1 << INT1);
	MCUCR |= (1 << ISC11) | (1 << ISC10);
	sei();

	PORTB |= (1 << PD4) | (1 << PD5) | (1 << PD6);
	PORTB |= ((1 << PB0) | (1 << PB1) | (1 << PB2) | (1 << PB3) | (1 << PB4)
			| (1 << PB5) | (1 << PB6) | (1 << PB7));

	for (;;) {

		/********************************************************************/
		/* show default time for ca. 0,5s and switch off LEDs for ca. 0,5s **/
		/********************************************************************/

		if (tasterZaeler == 4) {
			defaultTime();
			for (int t = 0; t < 200; t++) {
				showTime();
			}
			for (int t = 0; t < 200; t++) {
				hideTime();
			}
		}

		/********************************************************************/
		/* start clock after 10s without user input *************************/
		/* if there is an user input stop clock and reset second ************/
		/********************************************************************/

		if (tasterZaeler >= 1) {
			if (tasterZaeler <= 2) {
				clockDelay++;
				second = 0;
				TIMSK &= ~(1 << OCIE1A);
				tasterZaeler = 2;
			}
		}
		if (clockDelay == 4000) {
			TCCR1B = ((1 << WGM12) | (1 << CS10) | (1 << CS11));
			TIMSK = (1 << OCIE1A);
			OCR1A = 31250;
			clockDelay = 0;
			tasterZaeler = 3;
		}
		resetTime();
		setSecond();
		showSecond();
		resetTime();
		setMinute();
		showMinute();
		resetTime();
		setHour();
		showHour();
	}
}