using the Timer1 library for PCM audio output
hi all,
i'm trying generate audio arduino, using pulse code modulation.
if you're not familiar technique, take waveform (let's sine wave), divide 48 samples, , encode it's level @ point char array. then set interrupt on timer1 every time fires, sets 1 of timer2 pwm pins current sample point, increments array index. i've changed prescaler on timer2 pwm frequency 62,500 hz, once put output through low-pass filter sounds...surprisingly decent, actually.
so works expected static frequency. on oscilloscope, can see waveform i've encoded, , can hear on speaker.
now, should able vary frequency changing how timer1 interrupt called, i.e. how steps through array of samples. i'm using timer1 library in arduino playground, when try vary interrupt period, squat-- no output @ all.
has used library varying interrupt frequencies? or had success generating sound method? i'd use library because manually configuring timers makes me
i'm trying generate audio arduino, using pulse code modulation.
if you're not familiar technique, take waveform (let's sine wave), divide 48 samples, , encode it's level @ point char array. then set interrupt on timer1 every time fires, sets 1 of timer2 pwm pins current sample point, increments array index. i've changed prescaler on timer2 pwm frequency 62,500 hz, once put output through low-pass filter sounds...surprisingly decent, actually.
so works expected static frequency. on oscilloscope, can see waveform i've encoded, , can hear on speaker.
now, should able vary frequency changing how timer1 interrupt called, i.e. how steps through array of samples. i'm using timer1 library in arduino playground, when try vary interrupt period, squat-- no output @ all.
has used library varying interrupt frequencies? or had success generating sound method? i'd use library because manually configuring timers makes me

sorry, example code here:
code: [select]
#include "timerone.h"
char sineovertones[] = {0,84,106,99,113,127,107,79,71,51,1,-26,7,43,23,-17,-26,-23,-43,-60,-49,-44,-66,-63,0,63,66,44,49,60,43,23,26,17,-23,-43,-8,26,-1,-51,-71,-79,-107,-127,-113,-99,-106,-84};
char sine[] = {0, 22, 44, 64, 82, 98, 111, 120, 126, 128, 126, 120, 111, 98, 82, 64, 44, 22, 0, -22, -44, -64, -82, -98, -111, -120, -126, -128, -126, -120, -111, -98, -82, -64, -44, -22};
//a simple sine wave 36 samples
long analog0 = 160; //variable store input analog input pin 0
byte speakerpin = 3; //audio playback on pin 3. this can set pin 11.
volatile byte waveindex = 0; //index variable position in waveform array sine[]
volatile byte currentvalue = 0;
void setup() {
serial.begin(57600);
/************************** pwm audio configuration ****************************/
// configures pwm on pins 3 , 11 run @ maximum speed, rather default
// 500hz, useless audio output
pinmode(speakerpin,output); //speaker on pin 3
cli(); //disable interrupts while registers configured
bitset(tccr2a, wgm20);
bitset(tccr2a, wgm21); //set timer2 fast pwm mode (doubles pwm frequency)
bitset(tccr2b, cs20);
bitclear(tccr2b, cs21);
bitclear(tccr2b, cs22);
/* set prescaler /1 (no prescaling). the timer overflow every
* 62.5ns * 256ticks = 16us, giving frequency of 62,500hz, think. */
sei(); //enable interrupts registers have been set
/************************* timer 1 interrupt configuration *************************/
timer1.initialize(160); // initialize timer1, , set 1/2 second period
timer1.attachinterrupt(playtone); // attaches playtone() timer overflow interrupt
}
void playtone() {
/* function called timer1 interrupt. every time called sets
* speakerpin next value in sine[]. frequency modulation done changing
* the timing between successive calls of function, e.g. 1khz tone,
* set timing runs through sine[] 1000 times second. */
if (waveindex > 47) {
waveindex = 0;
}
analogwrite(speakerpin, sineovertones[waveindex] + 128);
waveindex++;
}
void loop()
{
analog0 = analogread(0) / 2;
serial.println(analog0);
delay(100);
timer1.setperiod(analog0);
}
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > using the Timer1 library for PCM audio output
arduino
Comments
Post a Comment