-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
executable file
·147 lines (123 loc) · 3.72 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
/*
* Vetinari Clock
* (C) 2011 Simon Inns
*
* avr-gcc port for Attiny25
* (C) 2013 Akafugu Corporation
*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include <stdbool.h>
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
#define _NOP() do { __asm__ __volatile__ ("nop"); } while (0)
#define X1_BIT PB0
#define X2_BIT PB1
// Define the amount of time (in mS) that the coil should
// be energised for a 'tick'. This should be as low as
// possible but varies between clock modules...
#define ENERGISE_TIME 60
// 128 timing values for 32 seconds
// (4 possible movements per second * 32 seconds = 128)
const uint8_t timingSequence[128] PROGMEM = {
1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1,
0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0,
1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0,
1, 1
};
// The randomisation amount adds a delay before moving the
// seconds hand to ensure the movement is not always on the
// quarter second boundary. The extra delay is in units of
// 5 mS.
const uint8_t timingRandomisation[15] PROGMEM = {
0, 6, 8, 2, 10, 3, 4, 1, 7, 9, 5, 3, 8, 5, 9
};
volatile uint8_t timingPosition = 0;
volatile uint8_t randomisationPosition = 0;
// Global for storing the currently required polarity
volatile uint8_t polarity = 0;
void tick(void)
{
PORTB |= (1 << X1_BIT);
PORTB &= ~(1 << X2_BIT);
}
void tock(void)
{
PORTB |= (1 << X2_BIT);
PORTB &= ~(1 << X1_BIT);
}
void reset(void)
{
PORTB |= (1<< X1_BIT);
PORTB |= (1<< X2_BIT);
}
// Send a pulse to the clock module
void pulseClock(void)
{
if (polarity == 0) {
tick();
_delay_ms(ENERGISE_TIME);
reset();
polarity = 1;
}
else
{
tock();
_delay_ms(ENERGISE_TIME);
reset();
polarity = 0;
}
}
int main(void) {
// Enable timer 1 with 32 prescaler.
// This gives 1/4 second between each overflow
// (32 * 256 / 32768 clocks = 1/4 second)
sbi(TCCR1, CS12);
sbi(TCCR1, CS11);
sbi(TIMSK, TOIE1);
sei();
DDRB = (1<<X1_BIT)|(1<< X2_BIT);
reset();
PORTB |= (1<<PB2); // Turn on pull-up on unused pin
set_sleep_mode(SLEEP_MODE_IDLE);
while(1)
{
if (pgm_read_byte(&timingSequence[timingPosition]) == 1)
{
// Wait for the randomisation amount
for (unsigned char delay = 0; delay < pgm_read_byte(&timingRandomisation[randomisationPosition]); delay++)
_delay_ms(5);
randomisationPosition++;
if (randomisationPosition == 15) randomisationPosition = 0;
pulseClock();
}
timingPosition++;
if (timingPosition == 128) timingPosition = 0;
sleep_mode(); // system sleeps here
}
}
// Timer 1 interrupt (will wake system from idle sleep mode)
ISR(TIMER1_OVF_vect) {
_NOP();
}