Lab Handout#12
Lab Handout#12
RUBRICS:
USE OF MODERN
DATA ANALYSIS
Performance Metric
PARTICIPATION
OBSERVATION/
CALCULATION
ENGINEERING
CONDUCTING
AND CODING
EXPERIMENT
TEAMWORK
Total Score
PROGRAM
RESULTS
0.5 0.5 0.5 TOOLS
0.5 1 1 1 05
Obtained
OBJECTIVE(S)
The purpose of this lab is to:
# Topic # Of Lectures CLO Taxonomy level
Learn the basic concepts of Output Compare Mode
1 of General-purpose Timer (TIM3)
Initialize the PWM function to control the 3 4,5 P3, A4
2 brightness of the LED on the STM32F0 Discovery
kit.
OUTCOME(S)
a. An ability to use the techniques, skills, and modern PLO5: Modern Tool Usage
engineering tools necessary for engineering practice.
b. An ability to communicate effectively PLO10: Communication
LAB REQUIREMENTS:
Standard PC with Keil uVersion5 and STM32CubeMX software installed with undated
package “STM 32 MCU package for STM32F0 Series”.
STM32F030 Discovery Board
ST Link Utility drivers
PART#01
OUTPUT COMPARE MODE OF GENERAL-PURPOSE TIMER
Output compare is the ability to trigger an output based on a timestamp in memory, without
interrupting the execution of code by a processor or microcontroller. This is a functionality
provided by many embedded systems. The corresponding ability to record a timestamp in
memory when an input occurs is called input capture.
Here is this lab we have to initialize the two LED’s available of board to on and off for specific time
using output compare.
LAB PROCEDURE
1. Open CubeMX, start new project by selecting STM32F030R8Tx discovery kit.
2. First select Pinout and Clear Pinouts, from SYS enable the Debug serial wire and from
Timers select TIM3.
3. In MODE window do the following setting
Clock Source -> Internal Clock
Channe2 -> Output Compare CH2
Channe3 -> Output Compare CH2
4. In Parameter setting of Configuration window as shown in figure 1 do the following
setting
1. Counter Setting
Prescaler (PSC-16bit value): 48000
Counter Mode: Up
Counter period (Autoreload register): 1000
2. Output Compare Channel 3
Mode: Toggle on match
Pulse: 500
3. Output Compare Channel 3
Mode: Toggle on match
Pulse: 1000
7. Build the program and check for the errors and warning.
8. Connect your discovery board (stm32f030x8) with PC and load the program and check the
output.
PART#02
PWM MODE OF GENERAL PURPOSE TIMER
LAB PROCEDURE:
5. Open CubeMX, start new project by selecting STM32F030R8Tx discovery kit.
6. First select Pinout and Clear Pinouts, from SYS enable the Debug serial wire and from
Timers select TIM3.
7. In MODE window do the following setting
Clock Source -> Internal Clock
Channe3 -> PWM Generation CH3
8. In Parameter setting of Configuration window do the following setting
4. Counter Setting
Prescaler (PSC-16bit value): 48000
Counter Mode: Up
Counter period (Autoreload register): 4000
The STM32F030 chip currently runs at 48MHz then the clock frequency supplies for
Timer 3 is: 48MHz/48000 = 1KHz ~ 1ms. From that, Timer3 will take (1ms * 2000) =
2Sec to finish one cycle counting. As a result, PWM Period relies on both Prescaler and
Counter Period (Autoreload register). Besides, Pulse (Capture/Compare register) will
determine the Pulse width.
5. PWM Generation Channel 3:
Mode: PWM mode 1
Pulse (16-bit value): 2000
9. Generate the code and open KEIL uVision5 and edit the code as given below:
10. To Start PWM function add this line before while loop.
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
11. To check the output waveform, connect PB0 (as this pin is on CH3 of TIM3) with
Oscilloscope probe the following output waveform will appear as shown in figure 4.
htim3.Instance = TIM3;
htim3.Init.Prescaler = 24;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 200;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
9. Declare this global variable uint16_t DutyCycle=10; Now add this code in while loop.
while (1)
{
htim3.Instance->CCR3=DutyCycle;
DutyCycle+=10;
if(DutyCycle>100)
DutyCycle=10;
HAL_Delay(1000);
}
10. Now connect an LED with PB0 and observe how LED brightness will change.
REVIEW QUESTION:
Repeat the same lab exercise of Pat#2 with different Prescaler.
Use Channel2 of TIM3 and repeat the same lab exercise of Part#02 with different Duty
cycle values.
LAB TASKS:
1. Repeat the same lab exercise for output compare mode of TIM3 with different counter
Periods and Channel Pulse values.
2. Toggle the four LED’s using output compare mode. Hint: connect LED’s at PC6, PC7,
PC8 and PC9.
3. Generate a waveform with Prescaler=48000, Counter period=5sec and Pulse=1sec.
Attached the CubeMx TIM3 setting window and resulting waveform.
4. Use all four channels of TIM3 connect the LED’s at Pins assign to different channel of
TIM3 and control the brightness of LED’s through Prescaler values=RR (where RR is
your Roll No.) with same Duty cycles. Attached the code and CubeMx TIM3 setting
window.