Lab4_2023
Lab4_2023
Aim
To use PWM to vary the intensity of the D2 LED output on PN0. The PWM concept will be demonstrated using a SysTick
interrupt to manually create the PWM signal.
Timers can be used to not only periodically trigger sampling or to read GPIO inputs, but can be used to generate PWM
outputs. These PWM signals can be used to drive DC motors at different speeds, create sound, or vary the intensity of
lights such as the LEDs on-board the Tiva. This can be achieved by varying the match value of the subsystem, which
changes the duty cycle of the PWM output. In this experiment, the duty cycle will be varied between 0 and 100% of the
reload value, according to the value entered from the terminal (Serial).
Figure 1: Use of LEDs controlled with PWM and the TM4C1294XL Development Board
PWM can be achieved using counters or timers as shown in Figure 1. The waveform repeats at a given frequency
which is set by the period. Within the waveform, the duty cycle is determined by setting the output High until the Match
is met and then the output is set Low.
Figure 1: Use of LEDs controlled with PWM
Where
tHIGH is the duration of the HIGH Pulse
TWaveform is the duration of the repeating waveform (Period)
Preliminary 1 mark
The template provided on L@G is setup for the Tiva microcontroller and Keil uVision 5.
For this experiment, you will again make use of Tera Term (or Putty), and use the following communication parameters:
Baud rate = 115200, 8 bit data, no parity, 1 stop, no flow control. (115200,8,N,1)
You will need to submit your code at the end of the session.
In this experiment, you will use a PWM signal to flash the D2 LED (PN0) at a given duty cycle. The duty cycle of the timer
PWM output on the D2 LED will result in different intensity of the LED being visible.
You MUST have a separate project for each part. You will be marked on programs that you
can demonstrate.
• Once you have finished with a Part ,zip up the folder containing the project (see the Submission section near the
end of this document for instructions on how to zip your files.
• Next copy your folder to a new folder with a new name eg copy folder L4PA to L4PB. You can then work on the
project in the L4PB folder.
• You can rename the .uvprojx and .uvoptx file to whatever you want to call the new project.
• You can then open your new project and edit it. You will need these skills for when you do your project.
Use this interrupt to produce a 1Hz 50% duty cycle waveform on Port N Pin 0 (D2 LED). The LED should flash on for ½
second and off for ½ second. This is equivalent to manually creating a PWM waveform. See Figure 1 to help you
understand the concept.
Basic Structure:
Turn Led On initially
Repeat:
At ½ second worth of 1ms interrupts turn LED OFF
At 1 second worth of 1ms interrupts turn LED ON
Hint: Think about counting interrupts. i.e. how many milliseconds corresponds to 1 second
Do NOT use the supplied delay function to produce the delay – you must use the SysTick interrupt to give the 1 second
Expected Output: The LED should flash ON for ½ second and OFF for ½ second.
B. When Duty Calls: 2 marks
Modify Part A so that the duty cycle can be changed to a value between 0 and 100% duty cycle using a function. Refer to
Figure 1 and the accompanying text to see how to set the Duty Cycle.
• You should create a function called Set_Duty_Cycle (percent). Which will take a parameter that is a
percentage between 0 and 100 and set the duty cycle of the waveform to the LED accordingly. The function
should not accept a value outside of these ranges.
• Use the supplied function called readDec() which reads a decimal number typed in from the terminal and
converts it to an integer. Eg typing “12<enter>” returns an integer which holds the value 12
• Test your function by watching how long the LED stays on as the duty cycle changes.
Hint: Think about the relationship between your counter value in Part A and the required duty cycle (Fig 2).
Expected Output: The LED should flash ON for the Duty cycle given eg 50% should blink the LED with ½ second ON
and ½ second OFF, 25% should blink the LED with ¼ second ON and 3/4 second OFF, 75% should blink the LED with
¾ second ON and ¼ second off, 0% OFF the whole time, and 100% ON the whole time. Basically the LED should be ON
for the DUTY Cycle fraction of 1 second.
Expected Output: The brightness of the D2 LED will change from OFF to fully ON with different levels of brightness in
between. The process of the brightness changing from OFF to ON will take 4 seconds.
Expected Output: Sweeping the Duty Cycle from 0% to 100% will cause all the LEDs to change their brightness in
unison.
E. Pressing on 3 marks
Modify Part D so that pressing SW1 increases the intensity of the LEDs by 10% and pressing SW2 decreases the
intensity of the LEDs by 10%. The value of the intensity in percent (5) should be written to the Terminal using the
supplied serial functions. At all times the intensity must remain within the 0 to 100% range where 0% is OFF and 100% is
ON. You should start the program with the LEDs OFF ie at 0%.
Note 1: The Switches will require a Pull Up resistor. Think carefully about the sense of the Switches are they High when
the switch is pressed or are they LOW when the switch is pressed. It will help you to think about the purpose of the Pull
Up resistor.
Hint: You may find a delay function useful to help with the debouncing of the switches.
Expected Output: When the SW1 button is pressed the brightness of all the LEDs should increase and when the SW2
button is pressed the brightness of all the LEDs should decrease. The value of the intensity of the LEDs should be written
to the Terminal.
Eg if the intensity was 50
• SW1 pressed the brightness will be higher and the Terminal will display 60%
• SW2 pressed the brightness will be lower and the Terminal will display 40%
• The range of the intensity is locked between 0% and 100% which will be evident from the terminal display.
Experiment Submission
Before leaving the lab you need to submit all code you have written for each part of the experiment to the course
website.
Ensure you have shown your demonstrator the correct operation of each completed part of this lab,
and have completed the online submission before leaving the laboratory.
Appendix
The functions provide in the library file can be found in the header file Lib4.h. Please read the header
file to determine the names and the arguments for the required functions.
#include "Lib4.h"
Lib4.h can be found under “Source Code 1” in the Project Pane of the Template. Double click on Lib4.h to get its contents
to appear in the editor.
Note: This is a common approach to find out the functions and the defines for a Library.