-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound.c
203 lines (173 loc) · 5.61 KB
/
sound.c
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*************************************************************************
Title: Asuro Bibliothek
Author: Michael Tissen und Alexander Berg
File: $Id: functionen.c,v 1.6.2.1 2007/07/01 11:14:38 peter Exp $
Software: AVR-GCC 4.1, AVR Libc 1.4.6 or higher
Hardware: Atmega32
License: GNU General Public License
DESCRIPTION:
Eine Sammlung von Funktionen für den Asuro.
USAGE:
Zur Nutzung funktionen.h und Settings.h einbinden
LICENSE:
Copyright (C) --
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*************************************************************************/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdint.h>
#include "include/Motor.h"
#include "include/sound.h"
#include "include/Settings.h"
/*
=======================================================================================================================
-
=======================================================================================================================
*/
/**
* @brief Funktion Sound
*
* Diese Funktion gibt Sound auf EINEM Asuro Motor aus.
*
*
* @param freq Frequenz
* @param duration Dauer
* @param amplitude Amplitude
* @return Kein Rückgabewert(void)
*
*/
void Sound (uint16_t freq,uint16_t duration_msec,uint8_t amplitude)
{
uint16_t wait_tics;
uint32_t n,k,period_usec,dauer_usec;
period_usec = 1000000L / freq;
dauer_usec = 1000 * (uint32_t) duration_msec;
k = dauer_usec / period_usec;
//IR Interuptfreq=36KHz
//Wavefreq=18KHz
wait_tics = 18000 / freq;
Motor(BREAK,BREAK,amplitude,amplitude);
for (n = 0; n < k; n++)
{
Motor (FWD, FWD,amplitude,amplitude);
Sleep (wait_tics);
Motor (BWD, BWD),amplitude,amplitude;
Sleep (wait_tics);
}
Motor(BREAK,BREAK,0,0);
}
/*
=======================================================================================================================
Sleep
=======================================================================================================================
*/
/**
* @brief Funktion Sleep
*
* Erzeugt eine Wartezeit mit Hilfe vom Timer
*
* @param time36kHz Zählerwert vom Timerinterrupt
* @return Kein Rückgabewert
*
*/
void Sleep (unsigned char time36kHz)
{
unsigned char ziel = (time36kHz + count72kHz) & 0x00FF;
while (count72kHz != ziel)
;
}
/*
=======================================================================================================================
Stereo Sound
=======================================================================================================================
*/
/**
* @brief Funktion StereoSound
*
* Diese Funktion kann zwei verschiedene Töne an den Motoren ausgeben
* je nachdem welche Noten rein kommen.
*
* @param *noten1 Note für den linken Motor
* @param *noten2 Note für den rechten Motor
* @return Kein Rückgabewert
*
*/
void StereoSound(uint16_t *noten1,uint16_t *noten2)
{
uint16_t index1=0,index2=0;
uint16_t timer1,timer2;
uint16_t phase1=0,phase2=0,angle1,angle2;
uint8_t dir1=FWD,dir2=FWD;
uint8_t speed1,speed2;
angle1=(uint32_t)noten1[index1++]*65536/FS;
timer1=noten1[index1++]*(FS/1000);
if (angle1==0)speed1=0;
else speed1=255;
angle2=(uint32_t)noten2[index2++]*65536/FS;
timer2=noten2[index2++]*(FS/1000);
if (angle2==0)speed2=0;
else speed2=255;
MotorSpeed(speed1,speed2);
cli(); // stop all interrupts
while (timer1!=0)
{
timer1--;
timer2--;
if (timer1==0)
{
angle1=(uint32_t)noten1[index1++]*65536/FS;
timer1=noten1[index1++]*(FS/1000);
if (angle1==0)speed1=0;
else speed1=255;
MotorSpeed(speed1,speed2);
}
if (timer2==0)
{
angle2=(uint32_t)noten2[index2++]*65536/FS;
timer2=noten2[index2++]*(FS/1000);
if (angle2==0)speed2=0;
else speed2=255;
MotorSpeed(speed1,speed2);
}
phase1+=angle1;
if (phase1&0x8000) dir1=FWD;
else dir1=BWD;
phase2+=angle2;
if (phase2&0x8000) dir2=FWD;
else dir2=BWD;
MotorRichtung(dir1,dir2);
// sync with Timer2 ( 31250Hz )
while (!(TIFR&(1<<TOV2)));
TIFR|=TIFR&(1<<TOV2);
}
MotorSpeed(0,0);
sei(); // enable all interrupts
}
/*
=======================================================================================================================
Muss vor Nutzung des Sounds initialisiert werden!
=======================================================================================================================
*/
/**
* @brief Funktion InitSoundTimer
*
* Diese Funktion initialisiert dir Register um
* die Stereosound Funktion nutzen zu können
*
* @return Kein Rückgabewert(void)
*
*/
void InitSoundTimer(void)
{
TCCR2 |= (1 << WGM20) | (1 << WGM21) | (1 << COM20) | (1 << COM21) | (1 << CS20);
OCR2 = 0x91; // duty cycle for 36kHz
TIMSK |= (1 << TOIE2); // 36kHz counter for sleep
}