-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordClock.c
341 lines (300 loc) · 8.12 KB
/
WordClock.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
* WordClock.c
*
* Author: Brendan Vercoelen
* http://no8hacks.com
*
* A functional clock which displays the time in words in increments of 5 minutes. It
* uses multiplexing to control a gridded array of LEDs and uses 74HC595 Shift
* Registers. It is written for the Attiny2313 but could be easily be adapted for any
* Microcontroller. The RTC Clock is the DS1307.
*
* A full write up on the build is available at:
* http://no8hacks.com
*/
#include <avr/io.h>
#include <util/delay.h>
#include <compat/twi.h>
// I2C Implementation by doctek
// http://www.instructables.com/id/I2C_Bus_for_ATtiny_and_ATmega/
#include "USI_TWI_Master.h"
// Output Pins
#define Clear_Enable PORTB |= 0b00000010
#define Set_Enable PORTB &= ~0b00000010
#define Shift_Clk_H PORTD |= 0b1000000
#define Shift_Clk_L PORTD &= ~0b1000000
#define Latch_Clk_H PORTD |= 0b0010000
#define Latch_Clk_L PORTD &= ~0b0010000
#define Data_H PORTD |= 0b0100000
#define Data_L PORTD &= ~0b0100000
#define Reset PORTB &= ~0b00000001
#define Reset_Clear PORTB |= 0b00000001
//Input Pins
#define UP ~PIND & 0b0000100
#define DOWN ~PIND & 0b0001000
int main(void); // Main function
void setclock(int, int); // Function sets the time on the clock face
unsigned char getTime(void); // Function gets the time from the RTC
void setTime(int, int); // Function sets the time in the RTC
unsigned char int2bcd(int); // Function converts integer to binary coded decimal
int bcd2int(unsigned char); // Function converts binary coded decimal to integer
void increment(void); // Increment time to next 5 minutes
void decrement(void); // Decrement time to previous 5 minutes
// Output
int output[9] = {0,0,0,0,0,0,0,0,0};
unsigned char hour, minute;
unsigned char TWI_targetSlaveAddress = 0b1101000;
unsigned char messageBuf[5];
int main(void)
{
// Set output pins
DDRB = 0b11111111;
DDRD = 0b1110011;
PORTB = 0b00000000;
PORTD = 0b0000000;
// Prepare Shift Register
Set_Enable;
Latch_Clk_L;
Reset;
Reset_Clear;
// Setup I2C
USI_TWI_Master_Initialise();
getTime();
int count = 0;
unsigned char transition = 0;
while(1)
{
// If UP Button is pressed
if (UP && count > 30)
{
increment();
count = 0;
}
// If DOWN button is pressed
if (DOWN && count > 30)
{
decrement();
count = 0;
}
// If time has not been updated in ~1 second
if (count > 100)
{
transition = getTime();
count = 0;
}
if (transition > 0 && count%7 == 0)
{
for (int i=8; i > 0; i--)
{
output[i] = output[i-1];
}
output[0] = 0b01010111010*count >> 1;
}
count++;
for (int i=0; i < 9; i++)
{
// If row has no data to display
if (output[i] == 0)
{
continue; // Go to next row
}
// Shift in row location
for (int j=0; j < 9; j++)
{
if (i == j)
{
Data_H;
}
else
{
Data_L;
}
// Shift Clock Pulse
Shift_Clk_H;
Shift_Clk_L;
}
// Shift in enabled columns
for (int j=0; j < 11; j++)
{
if ((output[i] & (1 << j)) >> j)
{
Data_H;
}
else
{
Data_L;
}
// Shift Clock Pulse
Shift_Clk_H;
Shift_Clk_L;
}
// Latch Clock Pulse
Latch_Clk_H;
Latch_Clk_L;
_delay_ms(2);
}
}
}
void setClock(int h, int m)
{
for (int i=0; i<9; i++)
{
output[i] = 0;
}
output[0] = 0b00000001111; // IT IS
if (m > 4 && m < 35)
{
output[3] = 0b00000001111; // PAST
}
else if (m > 34)
{
output[3] = 0b00000011000; // TO
h = (h + 1)%24;
}
switch (h)
{
case 0:
output[6] = 0b00011111111; // MIDNIGHT
break;
case 1:
case 13:
output[7] = 0b01110000000; // ONE
break;
case 2:
case 14:
output[6] = 0b01110000000; // TWO
break;
case 3:
case 15:
output[4] = 0b11111000000; // THREE
break;
case 4:
case 16:
output[5] = 0b00000001111; // FOUR
break;
case 5:
case 17:
if (hour == 17 && minute < 5)
{
output[2] = 0b11110000000; // BEER
}
else
{
output[5] = 0b00011110000; // FIVE
}
break;
case 6:
case 18:
output[5] = 0b11100000000; // SIX
break;
case 7:
case 19:
output[8] = 0b00000011111; // SEVEN
break;
case 8:
case 20:
output[3] |= 0b11111000000; // EIGHT
break;
case 9:
case 21:
output[4] = 0b00000111100; // NINE
break;
case 10:
case 22:
output[4] = 0b00000000111; // TEN
break;
case 11:
case 23:
output[7] = 0b00000111111; // ELEVEN
break;
case 12:
output[7] = 0b00111100000; // NOON
break;
}
if (m < 5 && (h != 12 && h != 0))
{
output[8] |= 0b11111100000; // OCLOCK
}
else if ((m > 4 && m < 10) || m > 54)
{
output[2] = 0b00000001111; // FIVE
}
else if ((m> 9 && m < 15) || (m > 49 && m < 55))
{
output[2] = 0b00001110000; // TEN
}
else if ((m > 14 && m < 20) || (m > 44 && m < 50))
{
output[1] = 0b00001111111; // QUARTER
}
else if ((m > 19 && m < 25) || (m > 39 && m < 45))
{
output[0] = 0b11111101111; // ITIS TWENTY
}
else if ((m > 24 && m < 30) || (m > 34 && m < 40))
{
output[0] = 0b11111101111; // ITIS TWENTY
output[2] = 0b00000001111; // FIVE
}
else if (m > 29 && m < 35)
{
output[1] = 0b11110000000; // HALF
}
}
unsigned char getTime(void)
{
int h;
messageBuf[0] = (TWI_targetSlaveAddress<<1) | (FALSE<<TWI_READ_BIT);
messageBuf[1] = 0x00;
USI_TWI_Start_Read_Write(messageBuf, 2);
messageBuf[0] = (TWI_targetSlaveAddress<<1) | (TRUE<<TWI_READ_BIT);
USI_TWI_Start_Read_Write(messageBuf, 4);
minute = bcd2int(messageBuf[2]);
h = bcd2int(messageBuf[3]);
if (hour != h) {
hour = h;
return 1; // Show transition effect on hour change
}
setClock(hour,minute);
return 0;
}
void setTime(int h, int m)
{
hour = h;
minute = m;
messageBuf[0] = (TWI_targetSlaveAddress<<1) | (FALSE<<TWI_READ_BIT);
messageBuf[1] = 0x00;
messageBuf[2] = 0;
messageBuf[3] = int2bcd(m);
messageBuf[4] = int2bcd(h);
USI_TWI_Start_Read_Write(messageBuf, 5);
setClock(h,m);
}
unsigned char int2bcd(int val)
{
return ((val / 10) << 4) | (val % 10);
}
int bcd2int(unsigned char val)
{
return ((val & 0b11110000) >> 4)*10 + (val & 0b00001111);
}
void increment(void)
{
if (minute > 54) {
setTime((hour+1)%24,00);
}
else
{
setTime(hour,(minute/5+1)*5);
}
}
void decrement(void)
{
if (minute < 5) {
setTime((hour+23)%24,55);
}
else
{
setTime(hour,(minute/5-1)*5);
}
}