0% found this document useful (0 votes)
0 views

Lab Handout#12

The lab experiment focuses on analyzing the General-purpose TIMER in Output Compare and PWM Mode using the STM32F0 Discovery kit. Students will learn to initialize and control LED brightness through PWM and toggle LEDs using output compare functionality. The lab requires specific software and hardware setups, and includes detailed procedures for implementing the experiments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Lab Handout#12

The lab experiment focuses on analyzing the General-purpose TIMER in Output Compare and PWM Mode using the STM32F0 Discovery kit. Students will learn to initialize and control LED brightness through PWM and toggle LEDs using output compare functionality. The lab requires specific software and hardware setups, and includes detailed procedures for implementing the experiments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

MEHRAN UNIVERSITY OF ENGINEERING AND TECHNOLOGY, JAMSHORO

DEPARTMENT OF ELECTRONIC ENGINEERING

EMBEDDED SYSTEMS DESIGN (ES-423)


Batch: 21ES (7th Semester)

Lab Experiment #12


To analyze the working of General-purpose TIMER in Output Compare and
PWM Mode
Name Roll #
Signature of Lab Tutor Date

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

Figure 1: Configuration window


5. Generate Code and open Project in KEIL-MDK
6. Start the timers channels
i. Open Functions – Timers library – Open HAL_TIM_OC_Start
ii. Check the arguments of function and copy the function into main.c file

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

Pulse width modulation, or pulse-duration modulation, is a method of reducing the average


power delivered by an electrical signal, by effectively chopping it up into discrete parts. As you
may hear about this function somewhere before, PWM (Pulse Width Modulation) is mostly
used for controlling the speed of DC motor or changing the brightness of LED or even mixing
colors for RGB LED. In this part, in this lab tutorial we will use how to initialize the PWM
function to control the brightness of the Blue LED on the STM32F0 Discovery kit.
Digital control is used to create a square wave, a signal switched between on and off. This on-
off pattern can simulate voltages in between full on (3.3 Volts) and off (0 Volts) by changing
the portion of the time the signal spends on versus the time that the signal spends off. The
duration of “on time” is called the pulse width. So basically, when talking about PWM, we
need to know about two factors; Pulse width and Period. From these two parameters, we can
determine the Duty Cycle (%) of PWM signal. Low duty cycle will result in low brightness
(LED) or low speed (DC motor) and vice versa.

Figure 2: PWM example with different duty cycle


Similarly, to Input Capture function, PWM generation uses the same Capture/Compare
register. The only difference is that one capture the pulse width and store into the register
while the other compare the register with the counter to trigger the output pin. PWM also uses
pins which has TIMx_CH1 to CH4 function. It means that one Timer can generate maximum 4
PWM outputs.

Figure 3: Auto-reload and Capture/Compare register

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

Figure 4: Tim3 setting window

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.

Figure 5: output waveform


Follow the following steps to calculate prescaler and counter period base on PWM resolution
and frequency:
 Determine the desired PWM resolution (for example: 100 steps, 200 steps, 1000…)
 Determine the desired PWM frequency (for example: 1kHz, 10kHz, 40kHz,…)
 Set the Counter Period at the same value as the resolution
 Set the Prescaler = 48000000/(PWM frequencyPWM resolution) – 1
 Set the Pulse equal to the desired Pulse width where the value varies from 0 to
Counter Period.
8. Now again edit the code go to static void MX_TIM3_Init(void) function in main file
and edit the code as given below. You can also make these changes by CubeMx as
given in figure 3, in Counter setting: set prescale value 24, counter period=200 and in
PWM Generation Channel 3: Pulse (16-bit value =0).

htim3.Instance = TIM3;
htim3.Init.Prescaler = 24;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 200;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

Also edit in sConfigOC.Pulse, intead of 2000 write 0.

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.

You might also like