Go Down

### Topic: PWM Sample Rate = 490Hz not 30769 Hz ??? (Read 1 time)previous topic - next topic

#### Martijn_Deluxe

##### Feb 26, 2007, 10:17 pm
Hey,

I was trying to get some basic oscillators going on the PWM outputs.

If i do a analogWrite(speakerOut, 128); i get a squarewave @ 490 Hz...

So it seems that the PWM Sample Rate is at 490Hz instead of the 30769 Hz the reference mentioned.

Is there a way to change the PWM Sample rate?

Is there a way to access the PWM clock so i sync my OSC the samplerate of the PWM generator?

I was thinking kinda like:

loop{
CalcNextSample;l
WaitForPWMClockWrap;
WriteSample;
}

GreetZ,

-Martijn-

#### mellis

#1
##### Feb 26, 2007, 11:34 pm
Whoops.  I forgot to update the reference when I changed the frequency.  Thanks for pointing that out; it's fixed now.

You can change the frequency by playing with the timer registers.  Try searching for TCCR1B on the forum or the Arduino homepage.  I'm going to try add a function to do this sometime soon.

#### Martijn_Deluxe

#2
##### Feb 27, 2007, 07:45 am
ThanX ;-)

That did help !!!

Meanwhile this is my piece of code so far now. It generates a parabolic sinoid wave. Still no clue how to sync to sample rate clock. But maybe i'm to slow to make it in time anyway...

So i tried to do a pot read(see //...) but it seems the analogRead blocks for 2.5 ms or so lowering the loop rate to +/-400hz... witch is totaly to slow for a Oscillator...

Do ya think it could work, or am i just trying something that i shouldnt?

CheerZ,

-M-

*****

int potPin = 2;
int speakerOut = 9;

long c=0;
long i=519;
int  x=0;

void setup() {
TCCR1A = 9;
TCCR1B = 1;
}

void loop() {
c+=i;
if(c>=65536)c-=65536;
x=(c>>-128;
x = (x*(128-(abs(x)))>>6)+128;
analogWrite(speakerOut,x);
}

#### mellis

#3
##### Feb 27, 2007, 09:29 pm
If you look at analogRead() in wiring.c in the lib/targets/arduino/ subdirectory of the Arduino directory, you'll see a delay(1).  You can try commenting it out, as it will probably be removed in the next version of Arduino.  That should speed things up.

#### Martijn_Deluxe

#4
##### Mar 01, 2007, 03:59 am
ThanX,

now its much faster

Any hints on getting the oscillator asyngroniut on a PWM timer based interrupt?

GreetZ,

-Martijn-

Go Up