[sdiy] Pseudo random noise circuits?
Olivier Gillet
ol.gillet at gmail.com
Fri Sep 17 23:03:34 CEST 2010
// This is run at initialization
void setup() {
// Configure pin 11 for output
pinMode(11, OUTPUT);
// Enable PWM on output A of timer 2 (labelled pin 11 on an arduino).
Set PWM to "phase correct mode"
TCCR2A = _BV(COM2A1) | _BV(WGM20);
// Set prescaler to 512. Timer frequency is thus 16MHz (the CPU clock
of the arduino board) / 512 = 31.25 kHz
TCCR2B = _BV(CS20);
// Enable interrupts when timer 2 overflows
TIMSK2 = _BV(TOIE2);
// This is the interrupt handler for timer 2 overflows, so this chunk
of code is run 31250 times per second
SIGNAL(TIMER2_OVF_vect)
{
// An example of linear feedback shift register (LFSR) we've been
discussing...
state = (state >> 1) ^ (-(state & 1) & 0xd0000001);
// Write the 8 lowest bits of the register to the Timer 2's first
(A) PWM output
OCR2A = state & 0xff;
}
So every 31250th second, an interrupt is raised and the interrupt
service routine updates the state of the LFSR and write this as a PWM
signal on pin 11. You need to apply a lowpass above 15kHz to attenuate
the PWM carrier, and with that, you get some pseudo-random noise
supposedly white in the 1uHz-15kHz band (the spectrum of the resulting
signal is not flat in the very small frequencies since the sequence
has a period of 2^32 / 31250s).
Note that if you want to build the best microcontrolled noise
generator, you'd rather use a proper DAC instead of PWM - this was
just to give you something you can very quickly get to play with (I
tested this with a bare arduino board and a piezzo speaker connected
to GND and pin 11).
Olivier
More information about the Synth-diy
mailing list