Documente Academic
Documente Profesional
Documente Cultură
PWM/Capture
NuMicro@nuvoton.com
2013/3/1
Agenda
• What’s PWM Output
• PWM Timing Control
• What’s Capture Input
• Capture Timing Control
• Interrupt Architecture
• Sample Code
1
What’s PWM Output?
• PWM: Pulse Width Modulation
• PWM Frequency and Duty
• How to set/change frequency and duty cycle?
• PWM Output Channels
• PWM Resolution PWM
(frequency)
• Needs dead-zone insertion? (CN+1)
Duty
• How to set dead-zone?
• Other Functions
(CM+1)
• Single-shot pulse
• PWM Interrupt
2
Features of PWM Generator (I)
• Four PWM Generators, each generator supports
• One 8-bit prescale
• One clock divider
• Two PWM-timers for two outputs, each timer includes
• A 16-bit PWM down-counter
• A 16-bit PWM reload value register (CN)
• A 16-bit PWM compare register (CM)
• One dead-zone generator
• Two PWM outputs.
3
Features of PWM Generator (II)
• 8 PWM channels or 4 PWM paired channels.
• 16 bits resolution.
• PWM Interrupt synchronized with PWM period.
• Single-shot or Continuous mode PWM.
4
PWM/Capture Clock Source
PWM01_S(CLKSEL1[29:28])
PWM01_EN(APBCLK[20])
HIRC
11 PWM01_CLK
HCLK 10
LXT
01
HXT
00
5
Configure PWM Generator
Insert dead-zone for
paired-PWM
DZ01 Dead Zone
Generator0
CLKSEL0
CN, CM,
PWM0_ CTL
1 100 CH0_OE
1/2 000 1
PWM- 1 1
PA.12/ PWM0
1/4 001 Timer0 0
0 0
Clock 1/8 010 Logic
Divider GPIOA_ DOUT[12]
1/16 011 CH0INV
GPIOA_ MFP[12]
1 TMINT0
TMIE0
8- bit 1/2
PWM01_CLK Prescaler DZEN01
1/4
( from clock PWM0_PR
controller) ES,CP01 1/8 CN, CM,
PWM0_ CTL Select
1/16
1 100 Paired-PWM CH1_OE
1/2 000 1
PWM- 1 1
PA.13/ PWM1
1/4 001 Timer1 0
Select proper PWM 0 0
1/8 010 Logic
clock input 1/16 GPIOA_ DOUT[13]
011 CH1INV
GPIOA_ MFP[13]
CLKSEL1 TMIE1 TMINT1
Configure PWM
Set PWM frequency( CN) output channel
PWM Interrupt
Set PWM duty (CM)
Configure PWM setting (PWMx_CTL)
6
PWM Frequency & Duty Control
• PWM Frequency =
PWMxy_CLK/(prescale+1)*(clock divider)/(CN+1);
where xy = 01 or 23, the selected PWM channels.
• Duty ratio = (CM+1)/(CN+1).
Start Update
Initialize new CM
PWM
CM+1 + PWM-Timer CM
Comparator
CN - Output CN
PWM
Ouput
CM >= CN: PWM output high.
CM < CN: PWM output low CN+1
CM+1
7
PWM Double Buffering Illustration
• Why does need the double buffer?
• To output the whole PWM cycle without any destroying.
S/W write new period (CN)
And new duty (CM)
CN=150 CN=199 CN=99 CN=0
CM=50 CM=49 CM=0 CM=XX
Start Stop
PWM
Waveform
51 50 1
PWM-Timer
Inversed Output 1/3 (Internal signal)
Dead-Zone Generator
(Port pin signal)
Output 1/3
11
Operation Timing of Capture Input
PWM Counter 3 2 1 8 7 6 5 8 7 6 5 4
Reload Reload
(If CNx = 8) No reload due to
no CAPIFx
Capture Input x
• The PWM counter will be
CAPCHxEN reloaded with CNx when
a capture interrupt flag
PWMX_CFLx 1 7 (CAPIFx) is set
PWMX_CRLx 5
• The channel low pulse
width is (CNx+1 - CRLx).
CFL_IEx • The channel high pulse
width is (CNx+1 - CFLx).
CRL_IEx
Clear by S/W
CAPIFx Set by H/W
TMINT1 PWM0CH1_INT
CAPIF1
PWM0_INT
TMINT2 PWM0CH2_INT
CAPIF2
Each PWM/
TMINT3 PWM0CH3_INT
Channel4~Channel7 share one
Capture CAPIF3
interrupt vector
channel has
its own TMINT0 PWM1CH0_INT
interrupt flag CAPIF0
TMINT1 PWM1CH1_INT
CAPIF1
PWM1_INT
TMINT2 PWM1CH2_INT
CAPIF2
TMINT3 PWM1CH3_INT
CAPIF3 13
Conditions of Sample Code
• Capture0
• Input Channel:GPA12
• Counter unit: 1usec (((11+1)*1)/12M)
• Capture capability: 32 bits (hardware 16bits+ firmware
16bits)
• PWM2
• Output Channel:GPA14
• Period: 1365.3 usec (((1+1)*1*(8191+1))/12M)
• High level: 682usec, Low level: 682usec
14
Block Diagram of Sample Code
Flash NUC130KE3BN
GPA12
Capture0
Cortex-M0
PWM2
15 GPA14
15
Sample Code (I)
void InitPWM(void)
{
/* Configure pin function */
GCR->PA_H_MFP = (GCR->PA_H_MFP & ~0x0F000000) | 0x01000000;
/* Output enable */
PWM0->OE |= PWM_OE_CH2;
}
16
Sample Code (II)
void InitCapture(void)
{
/* Configure pin function */
GCR->PA_H_MFP = (GCR->PA_H_MFP & ~0x000F0000) | 0x00010000;
/* Enable Capture function pin, capture function, counter reload on both rising and falling edge */
PWM0->CAPCTL = PWM_CAPCTL_CAPCH0EN |
PWM_CAPCTL_CAPCH0PADEN |
PWM_CAPCTL_CAPRELOADREN0 |
PWM_CAPCTL_CAPRELOADFEN0;
17
Sample Code (III)
/* Enable Capture rising and falling edge interrupt */
PWM0->CAPINTEN = PWM_CAPINTEN_CRLIE0 | PWM_CAPINTEN_CFLIE0;
18
Thank you.
19