Professional Documents
Culture Documents
MPBES Lab 7
MPBES Lab 7
LABORATORY SESSION # 7
PWM Generation using STM32F100xx Timer
7.1 OBJECTIVES
Pulse Width Modulation mode allows you to generate a signal with a frequency
determined by the value of the TIMx_ARR register and a duty cycle determined by the
value of the TIMx_CCRx register. The PWM mode can be selected independently on each
channel (one PWM per OCx output) by writing ‘110’ (PWM mode 1) or ‘111’ (PWM mode
2) in the OCxM bits in the TIMx_CCMRx register.
You must enable the corresponding preload register by setting the OCxPE bit in the
TIMx_CCMRx register, and eventually the auto-reload preload register (in upcounting or
center-aligned modes) by setting the ARPE bit in the TIMx_CR1 register.
As the preload registers are transferred to the shadow registers only when an update
eventoccurs, before starting the counter, you have to initialize all the registers by setting
the UG bit in the TIMx_EGR register. OCx polarity is software programmable using the
CCxP bit in the TIMx_CCER register. It can be programmed as active high or active low.
OCx output is enabled by a combination of the CCxE, CCxNE, MOE, OSSI and OSSR bits
(TIMx_CCER and TIMx_BDTR registers).
Refer to the TIMx_CCER register description for more details. In PWM mode (1 or
2), TIMx_CNT and TIMx_CCRx are always compared to determine whether TIMx_CCRx
≤ TIMx_CNT or TIMx_CNT ≤ TIMx_CCRx (depending on the direction of the counter).
The timer is able to generate PWM in edge-aligned mode or center-aligned mode depending
on the CMS bits in the TIMx_CR1 register.
Laboratory Manual EES-07310: MP Based Embedded Systems
7.5 PROCEDURE
- Create new project with the name ‘Lab7’, following the steps in Lab Session 1 including
two C and two H files with the names ‘main.c’, ‘pwm.c’, ‘main.h’ and ‘pwm.h’.
- Write the codes in each file as following:
pwm.c
#include "main.h"
#include "PWM.h"
void PWM_Init(void)
{
RCC_APB2ENR |= 0x00000804; /*Enable clock of Port A + Timer 1 PA8
is connected to CH1 of TIM1*/
Void Duty_Cycle(void)
{
TIM1_CCR1 = 10000; /*Capture/Compare register for channel 1;
defined duty cycle 50%. Similarly we can have 4 different duty cycles by
using CCR1 to CCR4 (frequency will be same)*/
}
Laboratory Manual EES-07310: MP Based Embedded Systems
pwm.h
#ifndef _PWM_H_
#define _PWM_H_
#endif
main.c
#include "main.h"
#include "PWM.h"
int main(void)
{
PWM_Init();
Cyclic_Start(10);
while(1)
{
Duty_Cycle();
Cyclic_Wait();
}
return(1);
}
main.h
#ifndef _MAIN_H
#define _MAIN_H
Students are required to provide the following components in their lab reports.
- Complete software code developed
- HEX file created successfully in Keil µVision 4
- Software tested in Debugger
- Software tested on the hardware
- Waveform of configured pin generated on oscilloscope
- Generate PWMs with different duty cycles (10%, 20%, 30% … 90%).
- Re-write the program to generate a PWM with variable duty cycle; the duty cycle should
automatically change after an interval of 3s.