Thursday, April 5, 2012

IR TRANSMITTER

IR CONTROL

Using pulsed IR (InfraRed) is a way to provide short distance command and control to a variety of applications. Some advantages to IR control are low cost, freedom from most types of interference and the ability to make secure the operation by designing a proprietary transmission code. An operational distance of up to 25 feet is easy to achieve and longer distances (>100 feet) are possible using lenses. Line of sight between the transmitter and receiver is necessary for operation.

Infrared radiation can be broadly described as electromagnetic radiation that starts at the limit of visible light at the red end of the spectrum and extends to the upper limit of the radio spectrum. The infrared spectrum, which has a wavelength spanning  from about 700 nanometers (0.7 micron) to about 1000 microns, can be further divided into four different bands; Near Infrared, Middle Infrared, Far Infrared and Extreme Infrared.

IR TRANSMITTER DESIGN

This is a simple IR transmitter that uses a FREESCALE MC9S08SH4 microcontroller and has three selectable transmission channels. Each channel has an indicator LED to indicate the active channel. It is possible to have up to a total of eight channels by lighting more than one LED at a time to indicate channel selection. A Bourns PEL12S encoder is used to select channels. This encoder has an LED lighted shaft that is used as a power on indicator. An encoder was chosen since it is cheaper and more versatile than a miniature rotary switch and can be used to create any number of functions through MCU programming.

Using the MC9S08SH4’s TIMER (TMR1, Channel 0) in output compare mode, the timer is programmed to create a burst of exactly fifty pulses at a precise period (frequency) with a duty cycle of 50%. Channel one has a period of 250 microseconds (4000 Hz). Channel two has a period of 500 microseconds (2000 Hz). Channel three has a period of 1000 microseconds (1000 Hz).

A hardware interrupt controlled by a normally open push button switch is used to send the pulse data. Each time the pushbutton is pressed, a burst of fifty pulses of the selected frequency is sent to the IR LED’s. A red LED provides a visual indicator that the pulses are being sent. Using this method, the IR receiver would be configured to measure and average the period of the pulses.

Three Vishay VSLY5080 Hi-Power IR LED’s in series are used as the optical output. These LED’s have a peak output wavelength of 850 nm and are suitable for high pulse current operation. The LED’s are switched on and off to ground by a 2N2222 transistor. A 30 ohm resistor limits the LED current to about 100 mA at 8 VDC.

The power supply consists of a 9VDC battery with a 78M05 regulator used to provide +5VDC to the microcontroller. A 9V battery was used because of its reasonably small size and also allows series operation of the IR LED’s.


IR TRANSMITTER SCHEMATIC (PDF)


CLICK ON FOR LARGER IMAGE


IR TRANSMITTER PCB (PDF)






FINISHED TRANSMITTER PCB




Wednesday, March 14, 2012

PWM CONTROLLER

Pulse Width Modulation (PWM) is an electronic means of controlling power to a load such as a DC motor, lamp or LED. The basic idea of PWM is that by applying maximum voltage to a load for only a fraction of a period of time - relating to the PWM frequency - the result will be an average voltage being delivered to the load which is proportional to the PWM duty cycle.

I created a simple adjustable PWM control that has a range of 0 to 100%. The duty cycle can be adjusted in either 1% or 0.1% increments. There are eight different selectable PWM frequency values; 31Hz, 62Hz, 125Hz, 250Hz, 500Hz, 1000Hz, 2000Hz and 4000Hz. The output driver can supply up to 1.5A at 15VDC.

MICROCONTROLLER CIRCUIT
The microcontroller used was a FREESCALE MC9S08D4. Timer channel 0 was configured to provide edge aligned PWM. The frequency (period) of the PWM output is determined from the value in the timer modulus register (TPMMOD). In this case, 1000 was chosen to provide 1000 steps in the PWM range from full off to full on.

The PWM frequency is varied by changing the timer clock divisor value. The timer modulus register always remains at 1000.  

The duty cycle is determined by the value in the timer value register (TPMC0V). A value of 0 in the timer value register will result in a duty cycle of 0%. A value greater than the modulus (1000) will result in a duty cycle of 100%.

Also, there is a bicolor LED that provides a visual indication of the PWM output, changing from red at either FULL OFF or small PWM values to yellow at intermediate PWM values to green at either FULL ON or high PWM values.

A green LED (PTA4) is provided to indicate when the PWM output is either at a value of exactly 50% or is at a value of exactly 100% (FULL ON).

A hardware interrupt (IRQ) controlled by a pushbutton switch is used to select both the PWM frequency and the PWM increment value of either 1% or 0.1% in an alternating fashion. When a new frequency is selected, the increment value is set at 1%. Pressing the pushbutton again will keep the same PWM frequency and duty cycle but the increment value is changed to 0.1%.

Pressing and releasing the pushbutton will scroll the frequency selection from 31Hz to 4000Hz in eight steps alternating with the increment value and then the selection will loop back to the starting point (31Hz) and repeat. A yellow LED (PTA3) is used to indicate when the increment value is at 0.1%.

A BOURNS PEL12S lighted encoder is used to adjust the PWM value by changing the timer value register (TPMC0V). I used the keyboard interrupt (PORT PTA1/KBIP1) connected to the “A” output. The “B” output was connected to PORT PTA2. The interrupt function compares the pre-interrupt encoder value (located in main function) to the encoder value found during the interrupt routine.  There are four switch / case statements to determine if the rotation is clockwise or counter-clockwise and also increments or decrements the PWM value by either 10 (1%, yellow “RATE” LED off) or 1 (0.1%, yellow “RATE” LED on).

OUTPUT DRIVER
The output driver consists of a 4N37 optoisolator which provides complete isolation between the microcontroller circuit and the output load. This also allows the load to have its own power source separate from the microcontroller power supply. The 4N37 output provides gate drive to a pair of 2N7000 MOSFETS in parallel.

The MOSFETS supply the base drive to a 2N3055 power transistor. A 50 ohm 3W resistor limits the base current to about 300mA at 15VDC. The 2N3055 controls the ground side of the load.

A PTC fuse rated at 1.6A (3.2A TRIP) provides load protection. A load switch allows disconnecting the load without having to change the PWM value.

MICROCONTROLLER CIRCUIT (PDF)


OUTPUT DRIVER CIRCUIT (PDF)




PWM CONTROLLER PCB




FINISHED PCB


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


Monday, January 30, 2012

MC9S08QD4 MCU TIMER MODULE

MCU OVERVIEW

The FREESCALE MC9S08QD4 is a low cost 8 bit microcontroller available in an 8 pin DIP or SOIC package. It can operate from a voltage range of 2.7VDC to 5.5VDC. The QD4 version has 4kb of Flash memory and 256b of Ram.
The CPU core operates at 16MHz with a bus speed of 4MHz when operating at 5VDC. The peripherals include a four channel, 10 bit ADC, two Timer modules and a 4 pin Key Board Interrupt module.
Also, there are four general purpose input / output pins, one input only pin and one output only pin with an output current level of 10mA per pin (60mA total for package). A single wire program and debug interface is used for programming the flash memory.
The Timer channels are configured as follows;
TIM1:  2-channel timer / pulse-width modulator
TIM2:  1-channel timer / pulse-width modulator

Each channel can be used for input capture, output compare, buffered edge-aligned PWM, or buffered center-aligned PWM.

INTERNAL CLOCK SOURCE CALIBRATION

In order to create precise timer waveforms it is important to trim the 32KHz internal reference clock source that is used in the MC9S08QD4. The digital oscillator (FFL) output is post divided by two resulting in a buss clock of 4MHz. The Timer is configured to divide the buss clock by 4 resulting in a main counter running at 1MHz.

The internal reference frequency can be modified by writing a value to the ICSTRM register. This register requires a signed 8 bit value. Increasing the trim value decreases the reference frequency, while decreasing the value increases the frequency.

It is possible to set the reference manually. However, this would require configuring the timer to output a calibration frequency, such as a 1KHz or 10KHz square wave and adjusting the reference while measuring the square wave with a calibrated oscilloscope or frequency counter.

A much better and easier way is to use the BDM tool (P&E MULTILINK BDM) that is used to program the flash memory. The BUSCLK is measured and corrected by a special function during flash programming and the corresponding trim value is written to flash memory. The application just needs to copy the value to the ICSTRM trim register.  Note: There is another fine trim register named ICSSC. However, I have found that just writing to the ICSTRM register works very well.

This statement defines the trim value location in flash memory.
#define NV_ICSTRM (*(const char * _FAR)0x0000FFAF)

This statement copies the trim value from flash memory to the trim register and should be located in the main C function.
ICSTRM = NV_ICSTRM;


TIMER OVERVIEW

Both the Input Capture and Output Compare modes are based on the timers main 16 bit up counter. As an up counter, the counter counts from 0x0000h to either 0xFFFFh or a 16 bit modulus value stored in the TPMMODH:TPMMODL registers.

There is an interrupt flag associated with this main counter. The timer overflow flag (TOF) is set whenever the timer overflows from 0xFFFFh to 0x0000h.

If a modulus value is used, the timer overflow flag (TOF) is set when the timer overflows from the modulus value to 0x0000h. The (TOF) can be monitored by software polling (TOIE = 0) or can generate a hardware interrupt (TOIE = 1).

Since the MCU has an 8 bit data width and the timer is a 16 bit timer, there is a built in coherency mechanism where values are transferred only after both 8 bit bytes of a 16 bit register have been written.


INPUT CAPTURE

The Input Capture function can be used to capture the time at which an external event occurs. When an active edge occurs on the active input capture channel, the timer module latches the value of the timer counter into the timer channel value registers (TPMCnVH:TMPCnVL). Rising edges, falling edges or any edge can be selected as the active input capture edge.
An input capture event sets a flag bit (CHnF) that can be used to trigger a CPU interrupt request (CHnIE = 1).  This flag can also be monitored by software polling (CHnIE = 0). 

In operation, the difference between two consecutive captures (two rising or falling edges captured) indicates the period of the external signal or with two adjacent edges captured, the pulse width. It is important to remember that if the time between two captures is longer than the modulo setting of the timer, then the number of timer rollovers must be kept track of using the timer overflow flag (TOF). This value is then added to the capture time.

Other applications for the Input Capture mode are;

1.) Event arrival-time recording: Can be used to compare the arrival time of several consecutive input capture events.

2.) Interrupt Generation: Input Capture pins can be used as an edge sensitive interrupt source.

INPUT CAPTURE PROGRAM EXAMPLE (CLICK HERE FOR LINK)

DESCRIPTION
Example shows the input capture portion of a program that displays the pulse width input on timer 1, channel 0 (in microseconds).
Display of 1000 = 1000 microseconds or 1 millisecond
Display of 10000 = 10000 microseconds or 10 milliseconds


These two pictures show the Input Capture measuring a 5kHz square wave. A 5kHz square wave has a period of 200uS which would correspond to a pulse width of 100 uS (as indicated on oscilloscope at 50uS/div) at 50% duty cycle.







 
OUTPUT COMPARE MODE

In Output Compare, the timer can generate timed pulses with adjustable position, polarity, duration and frequency.  The Output Compare mode uses the same 16 bit counter and channel value registers as described for the Input Capture function.  In Output Compare mode, the timer can set, clear or toggle the timer channel pin being used when the counter reaches the value located in the channel value registers (TPMCnVH:TMPCnVL).

Applications for the Output Compare function include the generation of a single pulse, a square wave or a specific time delay.

OUTPUT COMPARE PROGRAM EXAMPLE (CLICK HERE FOR LINK)

DESCRIPTION
Example shows an output compare example that creates pulses from 1.0mS to 2.0mS with a period of 20mS that can be used to control a servo.


EDGE ALIGNED PWM

Using Pulse Width Modulation (PWM) is an excellent way to control the speed of a brush motor, the brightness of a lamp or LED and even to position a SERVO (servo control is usually referred to as PPM or Pulse Position Modulation).
Most MCU’s today have a timer module that can be programmed to provide a PWM output with little MCU overhead. The PWM output described here is a fixed period (frequency) output with an adjustable duty cycle or pulse width. By varying the pulse width to a motor, the average voltage delivered to the motor changes and so does its speed.
Edge aligned PWM mode uses the normal up counting mode of the timer counter.
The period of the PWM is based on the value in the modulus register (TPMMODH:TPMMODL). The duty cycle is determined by the value in the timer value registers (TPMCnVH:TMPCnVL).


Duty cycles from 0% to 100% are possible. A timer value of 0x0000 will result in a duty cycle of 0%. A duty cycle of 100% results when the timer value is greater than the modulus value. The PWM output can be either High-True or Low-True (inverted)   pulses.

EDGE ALIGNED PWM (PPM) PROGRAM EXAMPLE (CLICK HERE FOR LINK)

DESCRIPTION
Example shows a PWM (PPM) servo output with a fixed period of 20mS that cycles between 1.0 milliseconds (pulse width) and ramping up to 2.0 milliseconds (pulse width)  and then ramping back down to 1.0 milliseconds (pulse width) in 50uS increments.


FREQUENCY COUNTER

A frequency counter can be created by combining the use of the Input Capture and Output Compare functions. The Output Compare function is used to create a one second time base by setting the timer modulo to 10mS and using a timer overflow flag to track the looping of this modulo 100 times (10mS x 100 = 1 second).

The Input Capture is used to track the total number of rising edges during each one second interval. An interrupt service routine is used to increase the edge count.

FREQUENCY COUNTER PROGRAM EXAMPLE (CLICK HERE FOR LINK)

DESCRIPTION
Example shows the one second time base and interrupt service routine.

These two pictures show the frequency counter with an input frequency of 1kHz.
The oscilloscope indicates a period of 1mS, (1kHz) at 200uS/div.





Conclusion: The MC9S08QD4 Timer is very versatile and can be used for numerous applications requiring precise timing and waveform generation.