Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1920 | - | 1 | //file pwmsine8bit.c |
2 | //generate 8 bit sinewave using wavetable lookup |
||
3 | //12khz sample rate, 62500 hz pwm on pd5 oc1a |
||
4 | //for ere co embmega32 16Mhz 38400 |
||
5 | |||
6 | //May 3 07 Bob G initial edit 8 bit |
||
7 | //May 5 07 Bob G add traffic tones |
||
8 | //Jan 18 08 Bob G compile with 7.15 |
||
9 | |||
10 | //#include <stdio.h> |
||
11 | //#include <stdlib.h> |
||
12 | |||
13 | #include <ctype.h> |
||
14 | #include <math.h> //sin |
||
15 | #include <avr/io.h> |
||
16 | #include <avr/interrupt.h> |
||
17 | #include <avr/pgmspace.h> |
||
18 | #include <util/delay.h> |
||
19 | #include <string.h> |
||
20 | #include <stdint.h> |
||
21 | #include "../timer/timer.h" |
||
22 | |||
23 | //#include "../bluetooth/fifo.h" |
||
24 | |||
25 | #define INTR_OFF() asm("CLI") |
||
26 | #define INTR_ON() asm("SEI") |
||
27 | |||
28 | |||
29 | //------vars in bss------- |
||
30 | |||
31 | volatile unsigned char sindat[256]; //8 bit sine table |
||
32 | //unsigned int addat[8]; //internal 10 bit |
||
33 | unsigned int idesfreq; |
||
34 | unsigned char iattenfac; //0-255 |
||
35 | unsigned char dispon; |
||
36 | unsigned char freqincipart,freqincfpart; |
||
37 | unsigned char waveptipart,waveptfpart; |
||
38 | unsigned char tics; |
||
39 | unsigned char generate; |
||
40 | |||
41 | float basefreq; |
||
42 | float freqratio; |
||
43 | float sampfreq; //12khz |
||
44 | float samppd; //83.33usec |
||
45 | float fdesfreq; |
||
46 | float attendb; //0-50 |
||
47 | float attenfac; //.999-0 |
||
48 | const float pi=3.141592654; |
||
49 | |||
50 | |||
51 | |||
52 | #define MSB(w) ((char*) &w)[1] |
||
53 | #define LSB(w) ((char*) &w)[0] |
||
54 | |||
55 | #define TONE_BUFFER_SIZE 64 |
||
56 | #define TONE_BUFFER_MASK ( TONE_BUFFER_SIZE - 1) |
||
57 | |||
58 | static volatile unsigned char ToneBuf[TONE_BUFFER_SIZE]; |
||
59 | static volatile unsigned char ToneBufHead; |
||
60 | static volatile unsigned char ToneBufTail; |
||
61 | |||
62 | |||
63 | |||
64 | //------------------------------- |
||
65 | void cvtfreq2frac(void){ |
||
66 | //calc freq ratio, separate into integer and fractional parts |
||
67 | |||
68 | fdesfreq=idesfreq; |
||
69 | freqratio=fdesfreq/basefreq; //calc freq ratio from basefreq |
||
70 | freqincipart=(int)freqratio; //calc integer part |
||
71 | freqincfpart=(freqratio-freqincipart)*256; //calc fractional part |
||
72 | // cprintf("ipart %02x fpart %02x\n",freqincipart,freqincfpart); |
||
73 | } |
||
74 | |||
75 | |||
76 | //------------------------------- |
||
77 | void cvtdb2fac(void){ |
||
78 | //cvt db to attenuation factor |
||
79 | |||
80 | attenfac=pow(10.0,-attendb/20.0); //10^0=1 |
||
81 | iattenfac=(unsigned char)(attenfac*255.0); |
||
82 | // cprintf("attenfac %#7.5f\n",attenfac); |
||
83 | // cprintf("iattenfac %02x\n",iattenfac); |
||
84 | } |
||
85 | |||
86 | //------------------------- |
||
87 | void initsindat(void){ |
||
88 | //init 8 bit signed sin table 0-ff in ram 36dB s/n 46Hz basefreq |
||
89 | unsigned int n; |
||
90 | |||
91 | for(n=0; n < 256; n++){ |
||
92 | sindat[n]=(signed char)127.0*sin(2.0*pi*n/256.0); //256 sin points |
||
93 | } |
||
94 | } |
||
95 | |||
96 | |||
97 | //------------------------- |
||
98 | void initvars(void){ |
||
99 | //init vars |
||
100 | |||
101 | // sampfreq=12000.0; //hz |
||
102 | sampfreq=19530.0; //hz |
||
103 | // printf("samp freq %#7.1f hz\n\r",sampfreq); |
||
104 | // printf("samp freq %#7.1f hz\n\r",1234567.1); |
||
105 | samppd=1.0/sampfreq; //83.33usec |
||
106 | // printf("samppd %#7.1f usec \n\r",samppd*1e6); |
||
107 | basefreq=sampfreq/256.0; //46.875hz |
||
108 | // printf("basefreq %#7.5f hz\n\r",basefreq); |
||
109 | idesfreq=400; |
||
110 | cvtfreq2frac(); |
||
111 | cvtdb2fac(); //0db =.9999= 0xff |
||
112 | } |
||
113 | |||
114 | |||
115 | //--------------------- |
||
116 | |||
117 | |||
118 | |||
119 | |||
120 | void tone1(int hz) |
||
121 | //tone at hz for ms |
||
122 | |||
123 | { |
||
124 | |||
125 | uint8_t volume = 0; |
||
126 | // TCNT2=0; |
||
127 | // sound_timer = ms/5; |
||
128 | // soundpause_timer = soundpause/5; |
||
129 | |||
130 | idesfreq=hz; |
||
131 | cvtfreq2frac(); |
||
132 | attendb=volume; |
||
133 | cvtdb2fac(); |
||
134 | if (hz ==0) generate=0; |
||
135 | else { generate = 1; |
||
136 | // TIMSK2 |= _BV(TOIE2); // Interrupt freigeben |
||
137 | } |
||
138 | |||
139 | } |
||
140 | |||
141 | |||
142 | |||
143 | //------------------------------- |
||
144 | void tone_handler(void) // wird aus Timerinterrupt aufgerufen |
||
145 | |||
146 | { |
||
147 | uint16_t f_Hz=0; |
||
148 | uint16_t dur_ms=0; |
||
149 | char volume=0; |
||
150 | char tmp_high = 0; |
||
151 | char tmp_low = 0; |
||
152 | unsigned char tmptail; |
||
153 | |||
154 | |||
155 | if ( ToneBufHead != ToneBufTail) { |
||
156 | /* calculate and store new buffer index */ |
||
157 | tmptail = (ToneBufTail + 1) & TONE_BUFFER_MASK; |
||
158 | ToneBufTail = tmptail; |
||
159 | /* get one byte from buffer */ |
||
160 | tmp_low = ToneBuf[tmptail]; /* f_Hz low */ |
||
161 | }else{ |
||
162 | |||
163 | return; |
||
164 | } |
||
165 | if ( ToneBufHead != ToneBufTail) { |
||
166 | /* calculate and store new buffer index */ |
||
167 | tmptail = (ToneBufTail + 1) & TONE_BUFFER_MASK; |
||
168 | ToneBufTail = tmptail; |
||
169 | /* get one byte from buffer */ |
||
170 | tmp_high = ToneBuf[tmptail]; /* f_Hz high */ |
||
171 | }else{ |
||
172 | return; |
||
173 | } |
||
174 | |||
175 | f_Hz = (((uint16_t) tmp_high) << 8) | tmp_low; |
||
176 | |||
177 | if ( ToneBufHead != ToneBufTail) { |
||
178 | /* calculate and store new buffer index */ |
||
179 | tmptail = (ToneBufTail + 1) & TONE_BUFFER_MASK; |
||
180 | ToneBufTail = tmptail; |
||
181 | /* get one byte from buffer*/ |
||
182 | volume = ToneBuf[tmptail]; /* volume */ |
||
183 | }else{ |
||
184 | return; |
||
185 | } |
||
186 | |||
187 | if ( ToneBufHead != ToneBufTail) { |
||
188 | /* calculate and store new buffer index */ |
||
189 | tmptail = (ToneBufTail + 1) & TONE_BUFFER_MASK; |
||
190 | ToneBufTail = tmptail; |
||
191 | /* get one byte from buffer */ |
||
192 | tmp_low = ToneBuf[tmptail]; /* low Duratuion */ |
||
193 | }else{ |
||
194 | return; |
||
195 | } |
||
196 | if ( ToneBufHead != ToneBufTail) { |
||
197 | /* calculate and store new buffer index */ |
||
198 | tmptail = (ToneBufTail + 1) & TONE_BUFFER_MASK; |
||
199 | ToneBufTail = tmptail; |
||
200 | /* get one byte from buffer*/ |
||
201 | tmp_high = ToneBuf[tmptail]; /* high Duration */ |
||
202 | }else{ |
||
203 | return; |
||
204 | } |
||
205 | |||
206 | dur_ms = (((uint16_t) tmp_high) << 8) | tmp_low; |
||
207 | |||
208 | sound_timer = dur_ms/10; |
||
209 | // TCNT2=0; |
||
210 | idesfreq= f_Hz; |
||
211 | cvtfreq2frac(); |
||
212 | attendb= volume; |
||
213 | cvtdb2fac(); |
||
214 | |||
215 | if (f_Hz ==0) generate=0; |
||
216 | else { generate = 1; |
||
217 | // TIMSK2 |= _BV(TOIE2); // Interrupt freigeben |
||
218 | } |
||
219 | |||
220 | |||
221 | |||
222 | // printf("tone_handler f_Hz: %i dur_ms: %i volume: %i\n\r", f_Hz,dur_ms,volume); |
||
223 | |||
224 | } |
||
225 | |||
226 | |||
227 | void tone_putc(unsigned char data) |
||
228 | { |
||
229 | unsigned char tmphead; |
||
230 | |||
231 | tmphead = (ToneBufHead + 1) & TONE_BUFFER_MASK; |
||
232 | |||
233 | while ( tmphead == ToneBufTail ){ |
||
234 | ;/* wait for free space in buffer */ |
||
235 | } |
||
236 | |||
237 | ToneBuf[tmphead] = data; |
||
238 | ToneBufHead = tmphead; |
||
239 | |||
240 | }/* tone_putc */ |
||
241 | |||
242 | |||
243 | void playTone(uint16_t f_Hz, uint16_t dur_ms, uint8_t volume) |
||
244 | |||
245 | { |
||
246 | |||
247 | // printf("Playtone f_Hz: %i dur_ms: %i volume: %i\n\r", f_Hz,dur_ms,volume); |
||
248 | |||
249 | tone_putc(LSB(f_Hz)); |
||
250 | tone_putc( MSB(f_Hz)); |
||
251 | tone_putc( volume); |
||
252 | tone_putc(LSB(dur_ms)); |
||
253 | tone_putc(MSB(dur_ms)); |
||
254 | |||
255 | |||
256 | } |
||
257 | |||
258 | |||
259 | //----------------- |
||
260 | void InitSound(void){//main program |
||
261 | |||
262 | |||
263 | |||
264 | ToneBufHead = 0; |
||
265 | ToneBufTail = 0; |
||
266 | |||
267 | initvars(); |
||
268 | initsindat(); |
||
269 | |||
270 | Timer2_Init(); |
||
271 | Timer3_Init(); |
||
272 | |||
273 | |||
274 | |||
275 | |||
276 | } |
||
277 | |||
278 |