YamA schrieb:
...
> pinMode(10, OUTPUT);
>
> TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM11);
...
Schöner Mix aus Ardu'ino' und echtem 'C' :-/
Mal blöd gefragt, warum verwendest Du nicht die Arduino-Funktion 'tone'?
https://www.arduino.cc/en/reference/tone
Ich würde mir den Code noch kommentieren,
damit man später auch ohne Blick ins Datenblatt sieht, was gemacht wird:
1 | // Waveform generator mode 14 (Table 20-6)
|
2 | // Fast PWM, TOP = ICR1
|
3 | //
|
4 | // Clear OC1A/OC1B on compare match, (Table 20-4)
|
5 | // Set OC1A/OC1B at bottom
|
6 | //
|
7 | // Prescaler = 1 (Table 20-7)
|
8 | TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM11);
|
9 | TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS10);
|
10 |
|
11 | ICR1 = (800- 1);
|
12 |
|
13 | OCR1A = (800 - 1) / 2;
|
14 | OCR1B = (800 - 1) / 3;
|
Zusätzlich lasse mir gerne die Zählerendwerte vom Präprozessor
ausrechnen.
Hier ein aktuelles Beispiel, nur für einen anderen Timer:
1 | static void
|
2 | timer0_init( const uint16_t frequency)
|
3 | {
|
4 | #define TIMER0_PRESCALER 256
|
5 |
|
6 | TCCR0A |= ( 1 << WGM01); // CTC mode, Table 19-9
|
7 | TCCR0A |= ( 1 << COM0B0); // Toggle OC0B on Compare Match, Tab. 19-6
|
8 |
|
9 | #if TIMER0_PRESCALER == 1
|
10 | TCCR0B |= ( 1 << CS00);
|
11 | #elif TIMER0_PRESCALER == 8
|
12 | TCCR0B |= ( 1 << CS01);
|
13 | #elif TIMER0_PRESCALER == 64
|
14 | TCCR0B |= ( 1 << CS01) | ( 1 << CS00);
|
15 | #elif TIMER0_PRESCALER == 256
|
16 | TCCR0B |= ( 1 << CS02);
|
17 | #elif TIMER0_PRESCALER == 1024
|
18 | TCCR0B |= ( 1 << CS02) | ( 1 << CS00);
|
19 | #else
|
20 | #error "TIMER 0 PRESCALER undefined"
|
21 | #endif
|
22 |
|
23 | OCR0A = ( F_CPU / 2 / TIMER0_PRESCALER / frequency) - 1;
|
24 | OCR0B = 0;
|
25 | }
|