PDA

View Full Version : Teensy 2.0 PWM frequency



rbino
02-02-2013, 12:02 AM
Hi all, this is my first post!

I've received today two Teensy 2.0 and with one of them I want to make a USB Midi to CV to play my homemade synth from my pc.
I've already tried (and succeeded) to do that with Arduino, and I had to do this

TCCR1B = TCCR1B & 0b11111000 | 0x01;
in the setup so the PWM was ultrasonic and I could low-pass filter it.

Now, I searched but I haven't found the way how to do it on Teensy beside the "beta" function to set the PWM frequency- but will it work even on Teensy 2.0?
If it does not, how can I set the timer frequency? And which Timer controls what, both pin-wise and software-wise (like "delay" or so)?

Thanks in advance for the answer!

Nantonos
02-02-2013, 02:26 AM
Pulse width modulation (PWM) is a pretty crude way to simulate a variable output voltage. Wh not use a proper digital to analog converter (DAC)? For example, breakout boards for an MCP4725 IC 12-bit DAC are readily available and inexpensive. Just add an op-amp buffer to the output and you have your CV out.

Sources:
http://www.pieterfloris.nl/shop/product.php?id_product=963 (Europe)
https://www.adafruit.com/products/935 (US)

PaulStoffregen
02-02-2013, 10:32 AM
On Teensy 2.0, right now the TimerOne and TimerThree libraries are probably the simplest way to set the PWM frequency.

There's 3 main problems with using PWM to synthesize analog output.

1: Power supply noise couples directly to the output. Real DACs have a stable reference voltage. They typically have a "power supply rejection ratio" spec, indicating how much the power supply can impact the output output. Typically it's -60 dB or less.

2: PWM has limited resolution. On 16 MHz AVR, usually you'd use 8 bit PWM to get a 62.5 kHz carrier.

3: Significant filtering is needed to remove the carrier. Usually it's very difficult to remove the carrier without restricting the bandwidth. Maybe it's ok to leave some of the carrier in the audio signal for some applications? Certainly that's not ideal. A real DAC has very little high frequency output to filter away.

Still, it's possible to get voice quality audio output from PWM.

With a DAC, you can do better, but beware of spending all your time updating the DAC. You probably want one that can work with SPI, which is faster than I2C.

rbino
02-02-2013, 03:18 PM
Thanks for the answers!
So, if I understand correctly, if I want a 40kHz PWM with the Timer1 library, I have to do something like

void setup{
...
Timer1.initialize(25);
Timer1.pwm(pin, duty);
...
}

And then use

Timer1.setPwmDuty(pin, duty);
To change the duty in my program. Is it correct? Can the 3 pins controlled by Timer1 have different PWM duties?

Regarding the DAC, I don't think this is necessary, I already tested this kind of thing with an Arduino and a PWM at about 32kHz, low-pass filtered at 100Hz; the carrier was not audible in the CV and a settling time of 10ms is good enough for my purposes.

Edit: Maybe I wasn't clear enough, my purpose is not making audio with the PWM, but controlling an analog synth. So I don't need a very large bandwidth, it's only a control voltage.