All programs assume you are using the Freescale Freedom KL46Z 1. You are to writ
ID: 3707624 • 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
ANSWER:
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)
, 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.