aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars-Dominik Braun <lars@6xq.net>2014-04-29 16:38:43 +0200
committerLars-Dominik Braun <lars@6xq.net>2014-04-29 16:38:43 +0200
commite9601a5adada7e6abe80f5d711a8adefc894dfeb (patch)
treefcf5fc4f830b3a873b9ad4b74f47b1ef13baed0f
parent6dc875e136b4ab418cf63154bdd2ffea46c6e67a (diff)
downloadhourglass-e9601a5adada7e6abe80f5d711a8adefc894dfeb.tar.gz
hourglass-e9601a5adada7e6abe80f5d711a8adefc894dfeb.tar.bz2
hourglass-e9601a5adada7e6abe80f5d711a8adefc894dfeb.zip
speaker: Initial implementation
-rw-r--r--Makefile2
-rw-r--r--main.c6
-rw-r--r--speaker.c49
-rw-r--r--speaker.h9
4 files changed, 65 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index addea24..8996720 100644
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@ MCU = atmega88
all: sanduhr.hex
-sanduhr.elf: main.c i2c.c i2c.h uart.c uart.h timer.c timer.h gyro.c gyro.h accel.c accel.h common.h
+sanduhr.elf: main.c i2c.c i2c.h uart.c uart.h timer.c timer.h gyro.c gyro.h accel.c accel.h common.h speaker.c speaker.h
avr-gcc -std=gnu99 -mmcu=$(MCU) -Os -o $@ $^
sanduhr.hex: sanduhr.elf
diff --git a/main.c b/main.c
index a581ea8..608327c 100644
--- a/main.c
+++ b/main.c
@@ -13,6 +13,7 @@
#include "timer.h"
#include "gyro.h"
#include "accel.h"
+#include "speaker.h"
static void ledInit () {
/* set led1,led2 to output */
@@ -49,6 +50,7 @@ int main () {
uartInit ();
gyroInit ();
accelInit ();
+ speakerInit ();
set_sleep_mode (SLEEP_MODE_IDLE);
printf ("initialization done\n");
@@ -58,6 +60,10 @@ int main () {
gyroStart ();
accelStart ();
+ speakerStart ();
+ _delay_ms (200);
+ speakerStop ();
+
timerStart ();
bool checkGyro = false;
while (1) {
diff --git a/speaker.c b/speaker.c
new file mode 100644
index 0000000..d903c57
--- /dev/null
+++ b/speaker.c
@@ -0,0 +1,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 ();
+}
+
diff --git a/speaker.h b/speaker.h
new file mode 100644
index 0000000..dec1d20
--- /dev/null
+++ b/speaker.h
@@ -0,0 +1,9 @@
+#ifndef SPEAKER_H
+#define SPEAKER_H
+
+void speakerInit ();
+void speakerStart ();
+void speakerStop ();
+
+#endif /* SPEAKER_H */
+