Subversion Repositories Projects

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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