instructions in all cases you may use register definitions like ddrb a
Search for question
Question
INSTRUCTIONS: In all cases you may use register definitions, like DDRB, ADMUX etc. to
configure the register. Please comment your code as much as possible and make your code
readable. Please submit one PDF for this exam. The PDF can include drawings, code segments
and/or tables and short answers. Please use the exam numbering to specify your responses
(like 1.a or 1.c.l). You may introduce your own variables throughout and can use any other
coding techniques, unless otherwise specified.
1- In our lectures, we focused on the uses of GPIO, timers and ADCs. One demonstration
we did during recitation was implementation of how a timer can be used to generate a
PWM output on one of the microcontroller pins. The objective of this question is to use a
timer and PWM to provide a clock signal to an external peripheral. A clock signal is
nothing more than a 50% duty cycle PWM. The key here is to use a potentiometer to
control the frequency of the PWM signal so that some other device can use the clock to
control its functions. Start a project in PlatformIO, choose your Playground Classic
microcontroller and let's build the main.cpp file.
a. Because we want high resolution of the frequency of the PWM, we need to use a
16 bit timer. Choose one of the 16 bit timers on our 32U4 microcontroller to
provide the PWM timing. Add a comment that specifies what timer you have
chosen.
b. Identify the pin that is associated with your PWM output. Add a comment that
states the pin you will use along with the rational for selecting that pin. Keep in
mind, you may use a pin that is available on the 32U4 but NOT available on your
Playground Classic.
c. Suppose the range of clock frequencies you would like to generate are between
1kHz and 4kHz. Write a C function called Setup TimerPWM() that sets up the
timer you selected to output a 50% duty cycle PWM signal at 1kHz (for now).
This requires setting up the TCCR and OCR registers.
d. Write a C function called SetPWMFreq(uint16_t freqlnHz). This function takes in
a single 16 bit integer representing the desired frequency of the PWM in Hz.
Therefore, the resolution of this frequency value is 1Hz. This function should
adjust the PWM frequency to "freqlnHz” Hz, while maintaining a duty cycle of
50%. Be sure to limit this value between 1000 and 4000Hz.
e. Now, set up the ADC to read an analog input of your choice. Add a comment to
your code that specifies which ADC pin you are using. Using a 10k Ohm
potentiometer (variable resistor ranging from 0 Ohm to 10k Ohm) in series with a
10k Ohm resistor, use the potentiometer to control the frequency of the PWM you
generated in c. When the potentiometer is 0 Ohm, let that represent 1kHz
frequency of the PWM, while when 10k Ohm represents 4kHz. Write a C function
called SetupADC() that sets up the ADC registers to sample the selected analog
pin in "free running" mode. The idea is that as you rotate the potentiometer,
varying its resistance, the PWM signal created at the pin will range in frequency
from 1kHz to 4kHz.
f. Write a C function that translates the read ADC value to a frequency and returns
the necessary value to pass to d. called uint16_t translateADCtoFreq(uint16_t
ADCval).
g. Lastly, we need a visual to indicate the frequency (or the position of the
potentiometer). We can use LED's for this. Design a creative way to use one or more LEDs to indicate the frequency value of the PWM. You can use something
like a "progress bar” of LEDs for example. Its your choice, but the objective is to
"at a glance" be able to determine generally where in the frequency range we
currently are. Right a C function called SetupLEDs() that sets up the relevant
DDR/PORT registers. Then write a C function called UpdateLEDS(uint8_t
Percent), where Percent is 0 to 100 mapping linearly to the PWM frequency of
1KHz to 4kHz. This function should update the LEDs to inform the user of the
currently set frequency. Your code should look something like this:
void updateLEDs(uint8_t Percent)
{
}
void Setup TimerPWM()
{
}
void setupADC()
{
}
void SetPWMFreq(translateADCtoFreq(uint16_t freqlnHz)
{
}
void updateLEDs (uint8_t Percent)
{
}
uint8_t translateADCto Freq(uint16_t ADCval)
{
} void setup()
}
SetupLEDs();
Setup TimerPWM();
SetupADC();
}
void loop()
{
uint8_t FreqPercent;
//Add a line here that calculates Freq Percent from the value of ADCW
SetPWMFreq(translateADCto Freq (ADCW); //Read ADC and set the PWM Freq
updateLEDS(Freq Percent);
delay(100); //update 10 times a sec.
}
Good luck Everyone!