PWM Application Note

PWM Application Note
PWM Application Note
1
適用產品:SM59R16A2 / SM59R08A2
2
PWM 規格概述:
2.1
2.2
2.3
2.4
提供 4 組獨立的 PWM 信號
有三組預除器;輸出頻率 FPWM = FOSC/Div
三種資料模式:8, 10, 12 bit mode
4 組 PWM 信號可同時動作(條件為參考頻率 FPWM 相同,如下圖所示)
3
Pin Description
Pin
9
10
22
23
SM59R16A2 / SM59R08A2 Package: TQFP & LQFP
I/O
Description
i/o Bit 0 of port 4 & PWM Channel 2
i/o Bit 1 of port 4 & PWM Channel 3
Bit 5 of port 1 & Timer 2 compare/capture Channel 1
P1.5/PWM1/CC1
i/o
& PWM Channel 1
Bit 4 of port 1 & Timer 2 compare/capture Channel 0
P1.4/PWM0/CC0
i/o
& PWM Channel 0
Name
P4.0/PWM2
P4.1/PWM3
Specifications subject to change without notice, contact your sales representatives for the most recent information.
IRFWX-A073
1
Ver. A 2008/04
PWM Application Note
4
SFR 特殊控制暫存器及特殊狀態暫存器介紹:
4.1 Special Function Register (SFR)
A map of the Special Function Registers is show as below:
Hex\Bin
F8
X000
IICS
X001
IICCTL
X010
IICA1
X011
IICA2
X100
IICRWD
X101
X110
X111
Bin/Hex
FF
F0
E8
E0
D8
B
P4
ACC
BRGS
SPIC1
MD0
SPIC2
MD1
SPITXD
MD2
SPIRXD
MD3
SPIS
MD4
MD5
ARCON
D0
C8
C0
B8
PSW
T2CON
IRCON
IEN1
CCEN
IP1
CRCL
CCL1
S0RELH
CRCH
CCH1
S1RELH
TL2
CCL2
PWMD0H
TH2
CCH2
PWMD0L
CCL3
PWMD1H
CCH3
PWMD1L
B0
A8
A0
98
P3
IEN0
P2
S0CON
PWMD2H
IP0
PWMD2L
S0RELL
PWMD3H
ADCC1
PWMD3L
ADCC2
PWMC
ADCDH
WDTC
ADCDL
WDTK
CLKR
S0BUF
IEN2
S1CON
S1BUF
S1RELL
90
88
80
P1
TCON
P0
P5
TMOD
SP
DPS
TL0
DPL
KBLS
TL1
DPH
KBE
TH0
DPL1
KBF
TH1
DPH1
IFCON
PCON
F7
EF
E7
DF
D7
CF
C7
BF
B7
AF
A7
9F
97
8F
87
Note: Special Function Registers reset values and description for SM59R16A2/SM59R08A2
4.2 Pulse Width Modulation (PWM)
Mnemonic
PWMC
PWMD0H
PWMD0L
PWMD1H
PWMD1L
PWMD2H
PWMD2L
PWMD3H
PWMD3L
Description
PWM Control
register
PWM 0 Data
register high byte
PWM 0 Data
register low byte
PWM 1 Data
register high byte
PWM 1 Data
register low byte
PWM 2 Data
register high byte
PWM 2 Data
register low byte
PWM 3 Data
register high byte
PWM 3 Data
register low byte
Direct
Bit 7
Bit 6
B5h
-
-
BCh
-
-
Bit 5
PWM
PWMM[1:0]
-
BDh
BEh
-
-
-
B4h
Bit 2
Bit 1
Bit 0
RESET
PWM3
EN
PWM2
EN
PWM1
EN
PWM0
EN
00h
-
PWMD0[11:8]
-
-
-
-
-
PWMD1[11:8]
-
-
PWMD3[7:0]
00h
00h
PWMD2[11:8]
PWMD2[7:0]
-
00h
00h
PWMD1[7:0]
B2h
B3h
Bit 3
PWMD0[7:0]
BFh
B1h
Bit 4
00h
00h
PWMD3[11:8]
00h
00h
Specifications subject to change without notice, contact your sales representatives for the most recent information.
IRFWX-A073
2
Ver. A 2008/04
PWM Application Note
Mnemonic: PWMC
7
6
5
4
PWMM[1:0]
3
PWM3EN
2
PWM2EN
1
PWM1EN
Address: B5h
0
Reset
PWM0EN
00h
PWMM[1:0]: PWM 模式旗標:
PWM output freq. = Fosc / Div.
PWMM[1:0]
Mode
Div.
8-bit mode
256
00
10-bit mode
1024
01
12-bit mode
4096
10
8-bit mode
256
11
here Fosc is the external crystal or oscillator frequency
PWM3EN: PWM Channel 3 致能旗標:
PWM3EN = 1 – PWM Channel 3 致能.
PWM3EN = 0 – PWM Channel 3 禁能.
PWM2EN: PWM Channel 2 致能旗標:
PWM2EN = 1 – PWM Channel 2 致能.
PWM2EN = 0 – PWM Channel 2 禁能.
PWM1EN: PWM Channel 1 致能旗標:
PWM1EN = 1 – PWM Channel 1 致能.
PWM1EN = 0 – PWM Channel 1 禁能.
PWM0EN: PWM 0 Channel 0 致能旗標:
PWM0EN = 1 – PWM Channel 0 致能.
PWM0EN = 0 – PWM Channel 0 禁能.
Mnemonic: PWMD0H
7
6
5
Mnemonic: PWMD0L
7
6
5
4
-
3
4
3
PWMD0[7:0]
2
1
PWMD0[11:8]
2
1
Address: BCh
0
Reset
00h
Address: BDh
0
Reset
00h
PWMD0[11:0]: PWM channel 0 資料暫存器:
可調整 PWM Channel 0 Duty Cycle
Mnemonic: PWMD1H
7
6
5
Mnemonic: PWMD1L
7
6
5
4
-
3
4
3
PWMD1[7:0]
2
1
PWMD1[11:8]
2
1
Address: BEh
0
Reset
00h
Address: BFh
0
Reset
00h
PWMD1[11:0]: PWM channel 1 資料暫存器:
可調整 PWM Channel 1 Duty Cycle
Specifications subject to change without notice, contact your sales representatives for the most recent information.
IRFWX-A073
3
Ver. A 2008/04
PWM Application Note
Mnemonic: PWMD2H
7
6
5
Mnemonic: PWMD2L
7
6
5
4
-
3
4
3
PWMD2[7:0]
2
1
PWMD2[11:8]
2
1
Address: B1h
0
Reset
00h
Address: B2h
0
Reset
00h
PWMD2[11:0]: PWM channel 2 資料暫存器:
可調整 PWM Channel 2 Duty Cycle
Mnemonic: PWMD3H
7
6
5
Mnemonic: PWMD3L
7
6
5
4
-
3
4
3
PWMD3[7:0]
2
1
PWMD3[11:8]
2
1
Address: B3h
0
Reset
00h
Address: B4h
0
Reset
00h
PWMD3[11:0]: PWM channel 3 資料暫存器:
可調整 PWM Channel 3 Duty Cycle
說明:
以PWM 8-bit模式,以PWMD3資料暫存器為例,PWMD3值愈小duty cycle愈小,值愈大則反之。
圖示如下:
Specifications subject to change without notice, contact your sales representatives for the most recent information.
IRFWX-A073
4
Ver. A 2008/04
PWM Application Note
5
PWM 應用流程圖:
6
程式範例:
Describe:
main
Program:
//===============================
//INCLUDE FILES
//===============================
#include "..\h\SM59R16A2.h"
#include "..\[Demo]\Demo.h"
#include "..\PWM\pwm.h"
//===============================================================
//MAIN
//===============================================================
void main()
{
unsigned char Press_SW;
while(1)
{
Press_SW = ScanKeypad();
if(!Press_SW)
//SW3 Press
{
// sel 8-bit mode=Fosc/256, enable channel 3,2,1,0
PWMC = 0x0F ;
//Enable PWM
PWM0Duty(0x00, 0xff);
PWM1Duty(0x00, 0x7f);
PWM2Duty(0x00, 0x3f);
PWM3Duty(0x00, 0x1f);
//MPU Pin 23 full Cyc.
//MPU Pin 22
//MPU Pin 09
//MPU Pin 10
}
else
{
PWMC = 0x00;
}
}// End of while(1)
//Disable PWM
}
//===============================================================
void PWM0Duty(unsigned char PWM0_H, unsigned char PWM0_L)
{
PWMD0H = PWM0_H;
PWMD0L = PWM0_L;
}
Specifications subject to change without notice, contact your sales representatives for the most recent information.
IRFWX-A073
5
Ver. A 2008/04
PWM Application Note
Describe:
main
Program:
//===============================================================
void PWM1Duty(unsigned char PWM1_H, unsigned char PWM1_L)
{
PWMD1H = PWM1_H;
PWMD1L = PWM1_L;
}
//===============================================================
void PWM2Duty(unsigned char PWM2_H, unsigned char PWM2_L)
{
PWMD2H = PWM2_H;
PWMD2L = PWM2_L;
}
//===============================================================
void PWM3Duty(unsigned char PWM3_H, unsigned char PWM3_L)
{
PWMD3H = PWM3_H;
PWMD3L = PWM3_L;
}
Specifications subject to change without notice, contact your sales representatives for the most recent information.
IRFWX-A073
6
Ver. A 2008/04