aboutsummaryrefslogtreecommitdiff
path: root/speaker.c
blob: 2eace8ddd3cfc6b68a1d3d8c70f5a82bf7ba0f6d (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
/*	speaker control, uses timer2
 */

#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdbool.h>

#include "speaker.h"

static bool value = false;

static void speakerSet () {
	if (value) {
		PORTD = PORTD | (1 << PD6);
	} else {
		PORTD = PORTD & ~(1 << PD6);
	}
}

ISR(TIMER2_OVF_vect) {
	value = !value;
	speakerSet ();
}

void speakerInit () {
	/* set PD6 to output */
	DDRD |= (1 << PD6);
	/* value is false */
	speakerSet ();
}

void speakerStart () {
	/* reset timer value */
	TCNT2 = 0;
	/* set normal mode timer0 */
	TCCR2A = 0;
	/* enable overflow interrupt */
	TIMSK2 = (1 << TOIE2);
	/* io clock with 1024 prescaler */
	TCCR2B = ((TCCR2B & ~(1 << CS21)) | (1 << CS22) | (1 << CS20));
}

void speakerStop () {
	/* zero clock source */
	TCCR2B = 0;
	value = false;
	speakerSet ();
}