/
ClockMultiplier.cpp
executable file
·285 lines (259 loc) · 6.53 KB
/
ClockMultiplier.cpp
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
#include <stdint.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include "device.h"
#include "adc_freerunner.h"
#ifndef UINT16_MAX
#define UINT16_MAX 65535
#endif
#ifndef min
#define min(a,b) ((a)<(b)?(a):(b))
#endif
#ifndef max
#define max(a,b) ((a)>(b)?(a):(b))
#endif
typedef uint32_t ClockTick;
inline bool mulIsHigh(){
return !(CLOCKMULTIPLIER_MULIN_PINS & _BV(CLOCKMULTIPLIER_MULIN_PIN));
}
inline bool durIsHigh(){
return !(CLOCKMULTIPLIER_DURIN_PINS & _BV(CLOCKMULTIPLIER_DURIN_PIN));
}
inline bool repIsHigh(){
return !(CLOCKMULTIPLIER_REPIN_PINS & _BV(CLOCKMULTIPLIER_REPIN_PIN));
}
inline uint16_t getDuration(){
return getAnalogValue(DURCV_ADC_CHANNEL);
// todo: use StiffValue for hysteresis
}
inline uint16_t getMultiplication(){
uint16_t value = getAnalogValue(MULCV_ADC_CHANNEL);
value >>= 8; // range of 0 to 15
value += 1;
return value;
}
inline uint16_t getRepetition(){
uint16_t value = getAnalogValue(REPCV_ADC_CHANNEL);
value >>= 8; // range 0 to 15
value += 1;
return value;
}
uint16_t getMultipliedPeriod();
class ClockDuration {
public:
ClockTick period;
ClockTick fallMark;
volatile ClockTick pos;
ClockDuration(){
reset();
}
inline void reset(){
period = fallMark = pos = 0;
off();
}
inline void rise(){
period = pos;
fallMark = ((uint32_t)period*getDuration())>>12;
pos = 0;
on();
}
inline void fall(){
}
inline void clock(){
if(++pos >= fallMark){
off();
}
}
void on(){
CLOCKMULTIPLIER_DUROUT_PORT &= ~_BV(CLOCKMULTIPLIER_DUROUT_PIN);
CLOCKMULTIPLIER_LED1_PORT &= ~_BV(CLOCKMULTIPLIER_LED1_PIN);
}
void off(){
CLOCKMULTIPLIER_DUROUT_PORT |= _BV(CLOCKMULTIPLIER_DUROUT_PIN);
CLOCKMULTIPLIER_LED1_PORT |= _BV(CLOCKMULTIPLIER_LED1_PIN);
}
bool isOff(){
return CLOCKMULTIPLIER_DUROUT_PORT & _BV(CLOCKMULTIPLIER_DUROUT_PIN);
}
};
class ClockMultiplier {
public:
ClockTick fallMark;
volatile ClockTick pos;
volatile ClockTick counter;
ClockTick period;
ClockMultiplier(){
reset();
}
inline void reset(){
fallMark = pos = period = counter = 0;
off();
}
inline void rise(){
on();
period = counter / getMultiplication();
fallMark = (((uint32_t)period*getDuration())>>12);
counter = 0;
pos = 0;
}
inline void fall(){
}
inline void clock(){
if(pos++ == fallMark){
off();
}else if(pos >= period){
on();
pos = 0;
}
counter++;
}
void on(){
CLOCKMULTIPLIER_MULOUT_PORT &= ~_BV(CLOCKMULTIPLIER_MULOUT_PIN);
}
void off(){
CLOCKMULTIPLIER_MULOUT_PORT |= _BV(CLOCKMULTIPLIER_MULOUT_PIN);
}
bool isOff(){
return CLOCKMULTIPLIER_MULOUT_PORT & _BV(CLOCKMULTIPLIER_MULOUT_PIN);
}
};
class ClockRepeater {
public:
ClockRepeater(){
reset();
}
ClockTick period;
ClockTick fallMark;
uint8_t reps;
volatile uint8_t times;
volatile ClockTick pos;
volatile bool running;
inline void stop(){
running = false;
}
inline void reset(){
stop();
off();
}
inline void rise(){
on();
times = 0;
reps = getRepetition();
period = getMultipliedPeriod();
fallMark = (((uint32_t)period*getDuration())>>12);
pos = 0;
running = true;
}
inline void fall(){
// stop();
}
inline void clock(){
if(running){
if(pos++ == fallMark){
off();
}else if(pos >= period){
if(++times >= reps){
stop();
}else{
on();
}
pos = 0;
}
}
}
void on(){
CLOCKMULTIPLIER_REPOUT_PORT &= ~_BV(CLOCKMULTIPLIER_REPOUT_PIN);
CLOCKMULTIPLIER_LED2_PORT &= ~_BV(CLOCKMULTIPLIER_LED2_PIN);
}
void off(){
CLOCKMULTIPLIER_REPOUT_PORT |= _BV(CLOCKMULTIPLIER_REPOUT_PIN);
CLOCKMULTIPLIER_LED2_PORT |= _BV(CLOCKMULTIPLIER_LED2_PIN);
}
bool isOff(){
return CLOCKMULTIPLIER_REPOUT_PORT & _BV(CLOCKMULTIPLIER_REPOUT_PIN);
}
};
ClockDuration dur;
ClockMultiplier mul;
ClockRepeater rep;
inline uint16_t getMultipliedPeriod(){
return mul.period;
}
void reset(){
mul.reset();
dur.reset();
rep.reset();
}
void setup(){
cli();
// define hardware interrupts 0 and 1
// EICRA = (1<<ISC10) | (1<<ISC01) | (1<<ISC00); // trigger int0 on rising edge
// EICRA = (1<<ISC10) | (1<<ISC01);
// trigger int0 on the falling edge, since input is inverted
// trigger int1 on any logical change.
// ISC01 ISC00 or ISC10 ISC11:
// 0 0 low level triggers interrupt
// 0 1 any logical change
// 1 0 falling edge
// 1 1 rising edge
EICRA = (1<<ISC10) | (1<<ISC00); // trigger int0 and int1 on any logical change
// pulses that last longer than one clock period will generate an interrupt.
EIMSK = (1<<INT1) | (1<<INT0); // enables INT0 and INT1
CLOCKMULTIPLIER_MULIN_DDR &= ~_BV(CLOCKMULTIPLIER_MULIN_PIN);
CLOCKMULTIPLIER_MULIN_PORT |= _BV(CLOCKMULTIPLIER_MULIN_PIN); // enable pull-up resistor
CLOCKMULTIPLIER_DURIN_DDR &= ~_BV(CLOCKMULTIPLIER_DURIN_PIN);
CLOCKMULTIPLIER_DURIN_PORT |= _BV(CLOCKMULTIPLIER_DURIN_PIN);
CLOCKMULTIPLIER_REPIN_DDR &= ~_BV(CLOCKMULTIPLIER_REPIN_PIN);
CLOCKMULTIPLIER_REPIN_PORT |= _BV(CLOCKMULTIPLIER_REPIN_PIN);
CLOCKMULTIPLIER_MULOUT_DDR |= _BV(CLOCKMULTIPLIER_MULOUT_PIN);
CLOCKMULTIPLIER_DUROUT_DDR |= _BV(CLOCKMULTIPLIER_DUROUT_PIN);
CLOCKMULTIPLIER_REPOUT_DDR |= _BV(CLOCKMULTIPLIER_REPOUT_PIN);
CLOCKMULTIPLIER_LED1_DDR |= _BV(CLOCKMULTIPLIER_LED1_PIN);
CLOCKMULTIPLIER_LED2_DDR |= _BV(CLOCKMULTIPLIER_LED2_PIN);
CLOCKMULTIPLIER_MULOUT_PORT |= _BV(CLOCKMULTIPLIER_MULOUT_PIN);
CLOCKMULTIPLIER_DUROUT_PORT |= _BV(CLOCKMULTIPLIER_DUROUT_PIN);
CLOCKMULTIPLIER_REPOUT_PORT |= _BV(CLOCKMULTIPLIER_REPOUT_PIN);
CLOCKMULTIPLIER_LED1_PORT |= _BV(CLOCKMULTIPLIER_LED1_PIN);
CLOCKMULTIPLIER_LED2_PORT |= _BV(CLOCKMULTIPLIER_LED2_PIN);
// At 16MHz CPU clock and prescaler 64, Timer 0 should run at 1024Hz.
// configure Timer 0 to Fast PWM, 0xff top.
TCCR0A |= _BV(WGM01) | _BV(WGM00);
// TCCR0B |= _BV(CS01) | _BV(CS00); // prescaler: 64.
// TCCR0B |= _BV(CS01); // prescaler: 8
TCCR0B |= _BV(CS00); // prescaler: 1
// enable timer 0 overflow interrupt
TIMSK0 |= _BV(TOIE0);
setup_adc();
reset();
sei();
}
/* Timer 0 overflow interrupt */
ISR(TIMER0_OVF_vect){
mul.clock();
dur.clock();
rep.clock();
}
ISR(CLOCKMULTIPLIER_DURIN_INT){
if(durIsHigh())
dur.rise();
else
dur.fall();
}
ISR(CLOCKMULTIPLIER_MULIN_INT){
if(mulIsHigh()){
mul.rise();
}else{
mul.fall();
}
}
void loop(){
static int repstate = 0;
int isrep = repIsHigh();
if(isrep != repstate){
if(isrep)
rep.rise();
else
rep.fall();
repstate = isrep;
}
}