blob: e96afcf1f431f206c883963bcc8c9ad2d5ea3985 (
plain)
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
|
/* speaker control, uses timer2
*/
#include "common.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdbool.h>
#include <stdlib.h>
#include "speaker.h"
static volatile uint16_t count;
/* Compare interrupt
*/
ISR(TIMER2_COMPA_vect) {
PORTD ^= (1 << PD6);
--count;
if (count == 0) {
speakerStop ();
}
}
void speakerInit () {
/* set PD6 to output */
DDRD |= (1 << PD6);
/* turn off */
PORTD = PORTD & ~(1 << PD6);
}
void speakerStart (const speakerMode mode) {
/* 12.8ms */
count = 100;
/* compare value (hit on every tick) */
OCR2A = 1;
/* reset timer value */
TCNT2 = 0;
/* set ctc mode */
TCCR2A = (1 << WGM21);
/* enable overflow interrupt */
TIMSK2 = (1 << OCIE2A);
/* io clock with 1024 prescaler -> ~4kHz tone, ctc part2 */
TCCR2B = (1 << CS22) | (1 << CS21) | (1 << CS20);
}
void speakerStop () {
/* zero clock source */
TCCR2B &= ~(0x7);
/* turn off */
PORTD = PORTD & ~(1 << PD6);
}
|