/
main.c
217 lines (190 loc) · 4.7 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
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
213
214
215
216
217
/********************************
* Soldering Clock *
* MIT License *
* Copyright 2015 - Mike Szczys *
* http://jumptuck.com *
* *
********************************/
#define F_CPU 8000000
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#define SENSE 1<<PB5
#define HOURT 1<<PB2
#define HOURO 1<<PB3
#define MINST 1<<PB1
#define MINSO 1<<PB0
#define BUTSHOW 1<<PD1
#define BUTHOUR 1<<PD0
#define BUTMIN 1<<PD2
volatile uint8_t showTimeFlag = 0;
volatile uint8_t hourTen = 1;
volatile uint8_t hourOne = 2;
volatile uint8_t minTen = 5;
volatile uint8_t minOne = 3;
volatile uint8_t seconds = 0;
void init_IO(void){
//LEDs
DDRC |= (1<<PC0) | (1<<PC1) | (1<<PC2) | (1<<PC3) | (1<<PC4); //Set control pins as outputs
PORTC &= ~((1<<PC0) | (1<<PC1) | (1<<PC2) | (1<<PC3) | (1<<PC4)); //Set control pins low
//Input pin
DDRC &= ~(1<<PC5);
//Buttons
DDRD &= ~(BUTSHOW | BUTHOUR | BUTMIN);
PORTD |= BUTSHOW | BUTHOUR | BUTMIN; //Pullup resistors
}
void init_RTC(void){
//RTC Example Code from Atmel (altered for m168)
//http://www.atmel.com/Images/Atmel-1259-Real-Time-Clock-RTC-Using-the-Asynchronous-Timer_AP-Note_AVR134.zip
TIMSK2 &= ~((1<<TOIE2)|(1<<OCIE2B)); //Make sure all TC0 interrupts are disabled
ASSR |= (1<<AS2); //set Timer/counter0 to be asynchronous from the CPU clock
//with a second external clock (32,768kHz)driving it.
TCNT2 =0; //Reset timer
TCCR2B =(1<<CS20)|(1<<CS22); //Prescale the timer to be clock source/128 to make it
//exactly 1 second for every overflow to occur
while (ASSR & ((1<<TCN2UB)|(1<<OCR2BUB)|(1<<TCR2BUB))) //Wait until TC0 is updated
{}
TIMSK2 |= (1<<TOIE2); //Set 8-bit Timer/Counter0 Overflow Interrupt Enable
sei(); //Set the Global Interrupt Enable Bit
}
void twiddle(void){
PORTC |= 1<<PC4;
_delay_ms(10);
PORTC &= ~(1<<PC4);
_delay_ms(10);
}
void readInput(void){
if (PINC & (1<<PC5)) {
PORTC |= 1<<PC0;
_delay_ms(1000);
}
else { PORTC &= ~(1<<PC0); }
}
void findZero(void) {
while(1) {
if (PINC & SENSE) { break; }
else { twiddle(); }
}
for (uint8_t i=0; i<7; i++) { twiddle(); }
}
void showNumber(uint8_t num) {
findZero();
for (uint8_t i=0; i<num; i++) { twiddle(); }
}
void showHours(void) {
//Hours Tens
if (hourTen > 0) {
showNumber(hourTen);
PORTC |= HOURT;
_delay_ms(1000);
PORTC &= ~HOURT;
}
//Hours Ones
showNumber(hourOne);
PORTC |= HOURO;
_delay_ms(1000);
PORTC &= ~HOURO;
}
void showMinutes(void) {
//Minutes Tens
showNumber(minTen);
PORTC |= MINST;
_delay_ms(1000);
PORTC &= ~MINST;
//Minutes Ones
showNumber(minOne);
PORTC |= MINSO;
_delay_ms(1000);
PORTC &= ~MINSO;
}
void showTime(void) {
showHours();
showMinutes();
}
void incHours(void) {
//Only used by main so disable interrupts
cli();
++hourOne;
if (hourTen && (hourOne == 3)) {
hourTen = 0;
hourOne = 1;
}
else if (hourOne == 10) {
hourOne = 0;
++hourTen;
}
seconds = 0;
sei();
}
void incMinutes(void) {
//Only used by main so disable interrupts
cli();
if (++minOne == 10) {
minOne = 0;
if (++minTen == 6) {
minTen = 0;
}
}
seconds = 0;
sei();
}
int main(void)
{
init_IO();
init_RTC();
while(1)
{
if (~PIND & BUTSHOW) { showTime(); }
if (showTimeFlag) {
showTimeFlag = 0;
showTime();
}
if (~PIND & BUTHOUR) {
incHours();
showHours();
}
if (~PIND & BUTMIN) {
incMinutes();
showMinutes();
}
}
while(1)
{
// twiddle();
//readInput();
findZero();
findZero();
findZero();
_delay_ms(1000);
showTime();
/*
PORTC |= 1<<PC4;
_delay_ms(200);
PORTC &= ~(1<<PC4);
_delay_ms(200);
*/
}
}
ISR(TIMER2_OVF_vect) {
//PINC |= MINST; //Toggle LED at 1Hz
if (++seconds == 60) {
seconds = 0;
showTimeFlag = 1;
if (++minOne == 10) {
minOne = 0;
if (++minTen == 6) {
minTen = 0;
++hourOne;
if (hourTen && (hourOne == 3)) {
hourTen = 0;
hourOne = 1;
}
else if (hourOne == 10) {
hourOne = 0;
++hourTen;
}
}
}
}
}