-
Notifications
You must be signed in to change notification settings - Fork 2
/
timer.html
100 lines (90 loc) · 7.98 KB
/
timer.html
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
<html>
<head>
<meta charset="utf-8"/>
<script type="text/javascript" src="dist/index.js"></script>
<script type="text/javascript" src="customfunctions.js"></script>
<script type="text/javascript" src="compileandrun.js"></script>
</head>
<body onload='try {new Function("(a = 0) => a")} catch(e) {alert("Your browser does not support ES6! Please update")}'>
<h1>Timer</h1>
<textarea id="code" rows="30" cols="50">
#ifndef F_CPU
#define F_CPU 16000000UL // 16 MHz clock speed
#endif
#ifndef F_CPU
#define F_CPU 16000000UL // 16 MHz clock speed
#endif
const int NOTE_TICK_DURATION_MILLISECONDS = 10;//schneller für Simulation
//const int NOTE_TICK_DURATION_MILLISECONDS = 40;//Mit echten Arduino bitte dies verwenden!
const float BASE_FREQ = 440.0;
const float FRQ_MULT[] = {1.0,2.3,0.6,4.1};
typedef struct {
float tone;
float pulseWidth;
int duration; // duration in note-ticks
} Note;
Note notes[] = {
{.tone=3,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=0,.pulseWidth=1,.duration=4},{.tone=-9,.pulseWidth=0.25,.duration=4},{.tone=2,.pulseWidth=0.5,.duration=4},{.tone=-14,.pulseWidth=0.25,.duration=4},{.tone=5,.pulseWidth=0.5,.duration=4},{.tone=-2,.pulseWidth=0.25,.duration=4},{.tone=3,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=0,.pulseWidth=1,.duration=4},{.tone=-9,.pulseWidth=0.25,.duration=4},{.tone=0,.pulseWidth=1,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=0,.pulseWidth=1,.duration=4},{.tone=-9,.pulseWidth=0.25,.duration=4},{.tone=7,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=0,.pulseWidth=1,.duration=4},{.tone=-9,.pulseWidth=0.25,.duration=4},{.tone=5,.pulseWidth=0.5,.duration=4},{.tone=-14,.pulseWidth=0.25,.duration=4},{.tone=8,.pulseWidth=0.5,.duration=4},{.tone=-2,.pulseWidth=0.25,.duration=4},{.tone=7,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=0,.pulseWidth=1,.duration=4},{.tone=-9,.pulseWidth=0.25,.duration=4},{.tone=0,.pulseWidth=1,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=3,.pulseWidth=0.5,.duration=4},{.tone=5,.pulseWidth=0.5,.duration=4},{.tone=7,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=7,.pulseWidth=0.5,.duration=4},{.tone=-9,.pulseWidth=0.25,.duration=4},{.tone=7,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=5,.pulseWidth=0.5,.duration=4},{.tone=7,.pulseWidth=0.5,.duration=4},{.tone=8,.pulseWidth=0.5,.duration=4},{.tone=-14,.pulseWidth=0.25,.duration=4},{.tone=5,.pulseWidth=0.5,.duration=4},{.tone=-14,.pulseWidth=0.25,.duration=4},{.tone=5,.pulseWidth=0.5,.duration=4},{.tone=-14,.pulseWidth=0.25,.duration=4},{.tone=5,.pulseWidth=0.5,.duration=4},{.tone=7,.pulseWidth=0.5,.duration=4},{.tone=8,.pulseWidth=0.5,.duration=4},{.tone=-14,.pulseWidth=0.25,.duration=4},{.tone=12,.pulseWidth=0.5,.duration=4},{.tone=-2,.pulseWidth=0.25,.duration=4},{.tone=12,.pulseWidth=0.5,.duration=4},{.tone=-14,.pulseWidth=0.25,.duration=4},{.tone=10,.pulseWidth=0.5,.duration=4},{.tone=8,.pulseWidth=0.5,.duration=4},{.tone=10,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=7,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=7,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=5,.pulseWidth=0.5,.duration=4},{.tone=-9,.pulseWidth=0.25,.duration=4},{.tone=3,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=0,.pulseWidth=1,.duration=4},{.tone=-9,.pulseWidth=0.25,.duration=4},{.tone=5,.pulseWidth=0.5,.duration=4},{.tone=-14,.pulseWidth=0.25,.duration=4},{.tone=0,.pulseWidth=1,.duration=4},{.tone=-2,.pulseWidth=0.25,.duration=4},{.tone=7,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=12,.pulseWidth=0.5,.duration=4},{.tone=-16,.pulseWidth=0.25,.duration=4},{.tone=10,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=8,.pulseWidth=0.5,.duration=4},{.tone=-16,.pulseWidth=0.25,.duration=4},{.tone=7,.pulseWidth=0.5,.duration=4},{.tone=-14,.pulseWidth=0.25,.duration=4},{.tone=-2,.pulseWidth=0.25,.duration=4},{.tone=-14,.pulseWidth=0.25,.duration=4},{.tone=5,.pulseWidth=0.5,.duration=4},{.tone=-14,.pulseWidth=0.25,.duration=4},{.tone=-2,.pulseWidth=0.25,.duration=4},{.tone=-14,.pulseWidth=0.25,.duration=4},{.tone=3,.pulseWidth=0.5,.duration=4},{.tone=-21,.pulseWidth=0.25,.duration=4},{.tone=-9,.pulseWidth=0.25,.duration=4},{.tone=-21,.pulseWidth=0.125,.duration=4},{.tone=-9,.pulseWidth=0.125,.duration=4},{.tone=-21,.pulseWidth=0.08333333333333333,.duration=4},{.tone=-9,.pulseWidth=0.08333333333333333,.duration=4},{.tone=-21,.pulseWidth=0.0625,.duration=4},{.tone=-9,.pulseWidth=0.0625,.duration=4},{.tone=-21,.pulseWidth=0.05,.duration=4},{.tone=-9,.pulseWidth=0.041666666666666664,.duration=4},{.tone=-21,.pulseWidth=0.03571428571428571,.duration=4},{.tone=-9,.pulseWidth=0.03125,.duration=4},{.tone=-21,.pulseWidth=0.027777777777777776,.duration=4},{.tone=-9,.pulseWidth=0.025,.duration=4},{.tone=-21,.pulseWidth=0.020833333333333332,.duration=4},{.tone=0,.pulseWidth=1,.duration=16}
};
int notesLength = sizeof(notes)/sizeof(Note);
int calcOcrFq(int prescaler,float freq) {
return (int)(F_CPU/(prescaler*freq) + 0.5) & 0xffff;
}
//toneId: ... ,A (440Hz) :0, A# : 1, ... , C : 3, A (880Hz) : 12, ...
int calcFreq(float toneId) {
return pow(2.0,toneId/12.0)*BASE_FREQ;
}
// pulseWidth between 0.0 and 1.0
void playFq(int prescaler,float fq, float pulseWidth) {
int timerTop = calcOcrFq(prescaler,fq);
int timerCompare = timerTop*pulseWidth + 0.5;
OCR1A = timerTop;
OCR1B = timerCompare;
TCNT1 = 0;
}
int main(void){
DDRB |= (1 << PORTB2); //Define OCR1B as Output (Pin Name auf arduino: 10)
TCCR1A |= (1 << COM1A0) | (1 << COM1B1) | (1 << WGM10); // PB2: output, phase correct PWM Modus 9
TCCR1B |= (1 << CS10) | (1 << CS11) | (1 << WGM13);// prescaler=64, Modus 9
OCR1A = 0;
OCR1B = 500;
int n = 0;
while(1) {
for (int i = 0; i < notesLength; i++) {
playFq(64, calcFreq(notes[i].tone)*FRQ_MULT[n%4], notes[i].pulseWidth);
for (int t = 0; t < notes[i].duration; t++) _delay_ms(NOTE_TICK_DURATION_MILLISECONDS);
}
n++;
}
}
</textarea><br>
<div id="divId">
cyclesPerFrame:<input type="text" id="cyclesPerFrame" value="1000"><br>
interFrameDelayMilliseconds:<input type="text" id="frameDelayMilliseconds" value="0"><br>
<input type="button" value="RUN" onclick="this.disabled=true; compileAndRun(document.getElementById('code').value,'divId',document.getElementById('cyclesPerFrame').value*1, document.getElementById('frameDelayMilliseconds').value*1, undefined, console.log)"><br>
<wokwi-led color="red" pin="10" label="13"></wokwi-led>
<span id="simulation-time"></span><br>
<memout-element type="bin" address="PORTB"></memout-element>
<memout-element type="bin" address="DDRB"></memout-element>
<memout-element type="bin" address="PINB"></memout-element><br>
<memout-element type="bin" address="TCCR1" bytes="2" endian="little"></memout-element><br>
<memout-element type="bin" address="TCNT1" bytes="2" endian="little"></memout-element><br>
<memout-element type="bin" address="OCR1A" bytes="2" endian="little"></memout-element><br>
<memout-element type="bin" address="OCR1B" bytes="2" endian="little"></memout-element><br>
<!-- Diagramm -->
<memout-element
type="diagram2"
outputs="[bytesToInt(data[0x84],data[0x85]), bytesToInt(data[0x88],data[0x89]), bytesToInt(data[AVR8_REGISTER.OCR1BL],data[AVR8_REGISTER.OCR1BH]),extractBit(data[AVR8_REGISTER.PINB],2)*450 + 2500]"
color="blue"
min="0"
max="3000"
width="800"
height="600"
interval="1000000"
title="TCNT1, OCR1A, OCR1B und PB2"
colors='["red","blue","yellow","green"]'
labels='["TCNT1","OCR1A","OCR1B","PB2 (Audio)"]'
></memout-element><br>
</div>
</body>
</html>