Monday, February 13, 2012

DIGITAL SQUARE WAVE GENERATOR

I call this a digital square wave generator because it does not rely on a potentiometer and a resistor-capacitor circuit to generate square waves. Instead it uses a FREESCALE MC9S08QD4 MCU timer module (in edge aligned PWM mode) to output square waves from 2Hz to 20KHz at 50% duty cycle in four overlapping frequency ranges.
A rotary encoder is used to adjust the frequency output. The encoder is a BOURNS PE12LS (2-bit quadrature output), with 24 pulses per rotation and also has an illuminated shaft.
The encoder is used in conjunction with the key board interrupt to increase or decrease the value of the timer (CH0) value register (TPMC0V) from a value of 15625 cycles (minimum frequency) to 100 cycles (maximum frequency). If the value of the timer modulo registers (TPMMOD) is kept at twice the value of the timer value register (TPMC0V), a constant 50% duty cycle is created. The encoder increases or decreases the frequency output in either 100 cycles per detent (COARSE ADJ) or 1 cycle per detent (FINE ADJ).
The slight drawback to this method is that there is some loss in resolution when setting a high frequency value due to the smaller ratio between the adjustment value and the timer value register.
The four different frequency bands and also the increment value (coarse or fine) are selected using a single pushbutton in conjunction with another interrupt routine. The frequency value is maintained when switching from coarse to fine adjustment. When the frequency band is changed, the output starts at the minimum value.
OPERATION OF FREQUENCY BAND SELECT INTERRUPT (PSEUDOCODE)

START (DEFAULT)
2 Hz (RANGE) / COARSE ADJUST VALUE (100) - “FINE ADJ” LED OFF
PRESS/RELEASE “FREQ RANGE” BUTTON
2 Hz (RANGE) / FINE ADJUST VALUE (1) - “FINE ADJ” LED ON

PRESS/RELEASE “FREQ RANGE” BUTTON
8 Hz (RANGE) / COARSE ADJUST VALUE (100) - “FINE ADJ” LED OFF
PRESS/RELEASE “FREQ RANGE” BUTTON
8 Hz (RANGE) / FINE ADJUST VALUE (1) - “FINE ADJ” LED ON

PRESS/RELEASE “FREQ RANGE” BUTTON
32 Hz (RANGE) / COARSE ADJUST VALUE (100) - “FINE ADJ” LED OFF
PRESS/RELEASE “FREQ RANGE” BUTTON
32 Hz (RANGE) / FINE ADJUST VALUE (1) - “FINE ADJ” LED ON

PRESS/RELEASE “FREQ RANGE” BUTTON
128 Hz (RANGE) / COARSE ADJUST VALUE (100) - “FINE ADJ” LED OFF
PRESS/RELEASE “FREQ RANGE” BUTTON
128 Hz (RANGE) / FINE ADJUST VALUE (1) - “FINE ADJ” LED ON

PRESS/RELEASE “FREQ RANGE” BUTTON
RETURN TO START

Below is the timer register values used to determine the frequency output.
The MCU bus speed is fixed at 4MHz. The timer module further divides the bus speed by 64, 16, 4 or 1 to determine the timer rate. To maintain a constant 50% duty cycle, the TPMMOD register must be maintained at twice the value of the TPMCOV register.  

TPMSC = 0x0E, RANGE 2Hz TO 310Hz (DEFAULT)
4MHz / 64 (divide bus by 64) = 62500
62500 / 31250 (TPMMOD) with a 15625 (TPMC0V) = 2Hz minimum
62500 / 200 (TPMMOD) with a 100 (TPMC0V) = 310Hz maximum

TPMSC = 0x0C, RANGE 8Hz TO 1250Hz
4MHz / 16 (divide bus by 16) = 250000
250000 / 31250 (TPMMOD) with a 15625 (TPMC0V) = 8Hz minimum
250000 / 200 (TPMMOD) with a 100 (TPMC0V) = 1250Hz maximum

TPMSC = 0x0A, RANGE 32Hz TO 5000Hz
4MHz / 4 (divide bus by 4) = 1000000
1000000 / 31250 (TPMMOD) with a 15625 (TPMC0V) = 32Hz minimum
1000000 / 200 (TPMMOD) with a 100 (TPMC0V) = 5000Hz maximum

TPMSC = 0x08, RANGE 128Hz TO 20000Hz
4MHz / 1 (divide bus by 1) = 4000000
4000000 / 31250 (TPMMOD) with a 15625 (TPMC0V) = 128Hz minimum
4000000 / 200 (TPMMOD) with a 100 (TPMC0V) = 20000Hz maximum

This board can be easily reprogrammed for other uses. The three pin header that is located on the right edge has GND, +5VDC and SIGNAL outputs that are standard servo connections. Other possibilities include an adjustable PWM output to control a motor or other pulse/burst waveforms triggered by an interrupt using the on board push button.


DIGITAL SQUARE WAVE GENERATOR SCHEMATIC (PDF)

CLICK ON FOR LARGER IMAGE


DIGITAL SQUARE WAVE GENERATOR PCB (PDF)

CLICK ON FOR LARGER IMAGE


FINISHED PCB