All programs assume you are using the Freescale Freedom KL46Z 1. You are to writ
ID: 3707062 • Letter: A
Question
All programs assume you are using the Freescale Freedom KL46Z 1. You are to write a program that will input analog samples via pins PTB1 and PTB2 and output a PWM signal via pin PTE21. The frequency of the PWM signal is to be variable between a minimum of 1 Hz and a maximum of 10,001 Hz. The PWM frequency should be at minimum (1 Hz) when the analog sample read from PTB1 is at its maximum value and change proportionally with the analog sample so that the frequency reaches its maximum (10,001 Hz) when the analog sample reaches its minimum. The duty cycle of the PWM signal should be at its minimum (0%) when the sampled value on PTB2 is at minimum and should increase proportionately until the duty cycle reaches a maximum when PTB2 is at maximum. Analog samples are to be taken and the PWM frequency subsequently adjusted 10 times per second. example: if PTBI reads = 0.0 ?frequency = 10,001 Hz. if PTB1 reads-0.5frequency 5,001 Hz, if PTBI reads -1.0frequency -1 Hz 2. An anemometer is used to measure wind speed. Assume that the anemometer that will be used operates by sending a series of pulses at a speed proportional to the speed of the wind. In particular, itExplanation / Answer
In waveforms above, both "a" and "b" have duty cycles of 50% since they are "on" fifty percent of the time. However, their periods are different, with waveform "b" having half the period (twice the frequency) of waveform "a". Waveforms "c" and "d" both have the same periods, but waveform "c" has a duty cycle of 10%, and waveform "d" has a duty cycle of 80%. As a pulse approaches 100% "on" time, the duty cycle approaches 100%, and as it approaches being always "off", the duty cycle approaches 0%.
The PWM waveforms can be left aligned (default) or center-aligned. We use left-aligned in this demo and as shown above. Center-aligned PWM waveforms are fully described in the PWM Manual(S12PWM_8B8CV1.pdf)
The PWM output from the Dragon12 board is from the PP0 - PP7 pins, corresponding to the PWM0 - PWM7 outputs from the PWM unit. The output of each of these pins is taken relative to the chip ground, such as the VSS2 pin (see Dragon12 Schematic 1 for the pin-outs, ground locations, etc dragon12_1_revE.jpg). So, if you generate a logical 0-1-0-1-... sequence for PWM Channel 0 (PWM0) and measure the voltage between pins PP0 and VSS2, you will see that it alternates between 0 and about 5 V.
While PWM can be used to generate digital waveforms, such as used for stepping a stepper motor or controlling other digital circuits, PWM output can also generate analog voltages, albeit at low power. For instance, if you connect a low-current LED (plus resistor of course) to a PWM output, you will find that the intensity increases as the duty cycle goes from 0% to 100%, assuming that you have a relatively fast period, say, 10 msec. As another example, you could use PWM output to control a DC motor. You first hook up the high-frequency PWM output to an H-bridge or some other type of motor driver that can take small input currents and can generate large output currents (there is not enough current from the HCS12 PWM pins to drive a motor). As you increase the duty cycle form the PWM, the motor speeds up in direct proportion to the duty cycle. You can even use PWM to generate audio signals if you use some low-pass filtering to get rid of the high-frequency harmonics present in the PWM waveforms.
In the HCS12, we control the PWM waveforms by writing to different registers. This is discussed in more detail in the DBug12 section below. Note that these are 8 bit registers, giving 8 bit resolution. If finer resolution is needed, PWM registers can be concatenated to give 16 bit resolution. The PWM manual for the PWM module contains full details (S12PWM_8B8CV1.pdf).
*** It will be ESSENTIAL for you to work with the HCS12 manual in this demo to get register addresses, register descriptions, etc. You can use the online manual noted just above, or you can use one of the printed manuals in the lab.***
The main PWM registers we need to use in this demo are noted below:
* PWME - The PWM Enable Register is at address $00A0 and is used to enable the various PWM channels. Once we enable a PWM channel, it will begin running. To enable a channel, write a binary "1" to its bit position. For instance, if we type ">mm a0 01" at the DBug12 prompt ($01 = %0000 0001), this will write a binary "1" to bit 0 of address $00A0, and will thus start (enable) PWM channel 0. If we wrote ">mm a0 55" at the DBug12 prompt ($55 = %0101 0101), we would enable PWM channels 6,4,2 and 0 . See the manual for full details.
* PWMCLK - PWM Clock Select Register is at address $00A2. This selects which clock (A, SA, B or SB) will be used as the main time unit for each PWM channel. We use two clocks for each PWM channel: Clocks A and B are for coarse control, and clocks SA and SB are for fine control. We use the SA clock in this demo. For each clock, we will use a "prescaler" or "divider" value, which basically takes our main E clock and slows it down by the divider factor.
* PWMPRCLK - PWM Prescale Clock Select Register is at address $00A3. This is a prescaler (divider) for the clock A in PWMCLK, and in our case sets the frequency of the A clock. For instance, if we write %00000111 =$07 to address $00A3, we set the A clock to run at a frequency of A = E/128, where E is the E-clock frequency (24 MHz on the Dragon12 in the configuration we use). The clock rate divider is actually given by 2^x, where "x" is the value we wrote to the register (eg, "x" = 7 gives divider of 2^7 = 128, "x" = 5 gives a divider of 2^5 = 32, etc.).
* PWMSCLA - PWM Scale A Register is at address $00A8. This sets the scaling rate for how clock SA is generated from clock A via a prescaler: SA freq = A/(2*PWMSCLA). For instance, if clock A is set at A=E/128 as above, then writing %00000100 =$04 to the PWMSCLA register divides the A clock rate by (2*4). The SA clock frequency is now SA = A/(2*4) = (E/128)/(2*4) = E/(128*2*4)= 23,437.5 Hz. If we wrote a $00 to the PWMSCLA register, we have a special case where we divide clock A by (2*256).
* PWMPERx - PWM Channel Period Registers at addresses $00B4 (PWM0) - $00BB (PWM7). This register sets the period of the PWM channel. The PWMx period = Channel Clock Period * PWMPERx, where the Channel clock period is the SA or SB period (inverse of SA/SB freq.) and PWMPERx is the binary value in the register. For instance, if we write %00001000 =$08 to address $00B4, and the SA clock frequency is SA = 23,437.5 Hz as in the example above, then the SA clock period is 1/23,437.5 Hz = 42.7 microseconds. So the period of PWM Channel 0 is then 8 * 42.7 microseconds = 341 microseconds = 0.341 msec. In terms of frequency, the PWM Channel 0 frequency is SA/8 = 23,437.5 Hz/8 = 2930 Hz.