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.
No comments:
Post a Comment