功能使用方法 - 新茂國際科技股份有限公司

SM39R 系列 PWM 功能使用方法
PWM (Pulse Width Modulation) 功能使用方法
1
適用產品:
1.1 SM39R16A2/ SM39R12A2/ SM39R08A2
2
PWM 使用說明:
2.1 提供 2 組獨立的 PWM 信號
2.2 輸入時脈可由暫存器 PWMCS[2:0]選擇
2.3 PWM 為 10-bit 模式,但最大資料計數可由 PWMMDH 及 PWMMDL 設定為 1~1024
3
PWM 相關的特殊暫存器 PWM Special Function Register (SFR)
Mnemonic
PWMC
PWMD0H
PWMD0L
PWMD1H
PWMD1L
PWMMDH
PWMMDL
Description
Direct
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 Max Data
register high byte
PWM Max Data
register low byte
Bit 7
B5h
Bit 6
Bit 5
PWM
PWMCS[2:0]
BCh
PWMP0
-
-
BDh
Bit 4
Bit 3
Bit 2
-
-
-
PWM1EN PWM0EN
00H
-
-
-
PWMD0[9:8]
00H
BEh
PWMP1
-
-
-
BFh
-
-
-
-
-
CFh
-
PWMCS[2:0]:
-
PWMD1[9:8]
-
PWMMD[9:8]
3
-
時脈選擇位元(PWM clock select)
PWMCS [2:0]
000
001
010
2
-
1
PWM1EN
00H
FFH
Address: B5h
4
-
00H
00H
PWMMD[7:0]
5
RESET
00H
PWMD1[7:0]
CEh
6
PWMCS[2:0]
Bit 0
PWMD0[7:0]
Mnemonic: PWMC
7
Bit 1
0
PWM0EN
Reset
00H
Mode
Fosc
Fosc/2
Fosc/4
Specifications subject to change without notice, contact your sales representatives for the most recent information.
ISSFA-0233
1
Ver A 2010/09
SM39R 系列 PWM 功能使用方法
011
Fosc/6
100
Fosc/8
101
Fosc/12
110
Timer 0 overflow
111
Timer 0 external input (P3.4/T0)
通道 1 致能位元(PWM channel 1 enable control bit)
PWM1EN = 1 – PWM 通道 1 致能.
PWM1EN = 0 – PWM 通道 1 禁能.
通道 0 致能位元(PWM channel 0 enable control bit)
PWM0EN = 1 – PWM 通道 0 致能.
PWM0EN = 0 – PWM 通道 0 禁能.
PWM1EN:
PWM0EN:
Mnemonic: PWMD0H
7
PWMP0
Address: BCh
6
-
5
-
4
-
3
-
2
-
1
0
PWMD0[9:8]
Mnemonic: PWMD0L
7
6
Reset
00H
Address: BDh
5
4
3
PWMD0[7:0]
2
1
0
Reset
00h
PWMP0: 通道 0 閒置準位選擇位元(PWM channel 0 idle polarity select)
“0” –通道 0 閒置時為低準位(PWM channel 0 will idle low)
“1” –通道 0 閒置時為高準位(PWM channel 0 will idle high)
PWMD0[9:0]: 通道 0 資料暫存器(PWM channel 0 data register)
可調整 PWM 通道 0 責任週期(Duty Cycle)
Mnemonic: PWMD1H
7
PWMP1
Address: BEh
6
-
5
-
4
-
3
-
2
-
1
0
PWMD1[9:8]
Mnemonic: PWMD1L
7
6
Reset
00H
Address: BFh
5
4
3
PWMD1[7:0]
2
1
0
Reset
00H
PWMP1: 通道 1 閒置準位選擇位元(PWM channel 1 idle polarity select)
“0” –通道 1 閒置時為低準位(PWM channel 1 will idle low)
“1” –通道 1 閒置時為高準位(PWM channel 1 will idle high)
PWMD1[9:0]: 通道 1 資料暫存器(PWM channel 1 data register)
可調整 PWM 通道 1 Duty Cycle
Mnemonic: PWMMDH
7
-
6
-
5
-
Address: CEh
4
-
3
-
2
-
1
0
PWMMD[9:8]
Reset
00H
Specifications subject to change without notice, contact your sales representatives for the most recent information.
ISSFA-0233
2
Ver A 2010/09
SM39R 系列 PWM 功能使用方法
Mnemonic: PWMMDL
7
6
5
Address: CFh
4
3
PWMMD[7:0]
2
1
0
Reset
FFH
PWMMD[9:0]: 最大資料設定暫存器(PWM Max Data register)
PWM 由 0000 計數至 PWMMD[9:0],當計數等於 PWMMD[9:0]時會產生溢位。
PWMPx = 0
Duty = PWMDx[9:0]
Period = PWMMD[9:0]
Duty
Duty
Duty
Duty
Duty
Duty
PWM period
PWM period =
4
PWMMD + 1
PWM clock
PWM period
Leader pulse =
PWMDx
PWM clock
PWM 中斷
4.1 向量表(Interrupt vectors table)
Interrupt Request Flags
Interrupt Vector Address
Interrupt Number *(use
Keil C Tool)
IE0 – External interrupt 0
0003h
0
TF0 – Timer 0 interrupt
000Bh
1
IE1 – External interrupt 1
0013h
2
TF1 – Timer 1 interrupt
001Bh
3
RI0/TI0 – Serial channel 0 interrupt
0023h
4
TF2/EXF2 – Timer 2 interrupt
002Bh
5
PWMIF – PWM interrupt
0043h
8
SPIIF – SPI interrupt
004Bh
9
Specifications subject to change without notice, contact your sales representatives for the most recent information.
ISSFA-0233
3
Ver A 2010/09
SM39R 系列 PWM 功能使用方法
ADCIF – A/D converter interrupt
0053h
10
KBIIF – keyboard Interface interrupt
005Bh
11
LVIIF – Low Voltage Interrupt
0063h
12
IICIF – IIC interrupt
006Bh
13
RI1/TI1 – Serial channel 1 interrupt
0083h
16
*See Keil C about C51 User’s Guide about Interrupt Function description
4.2 中斷相關暫存器(Interrupt SFR)
Mnemonic
IEN1
IRCON
IP0
IP1
Description
Interrupt Enable 1
register
Interrupt request
register
Interrupt priority
level 0
Interrupt priority
level 1
Direct
Bit 7
Bit 6 Bit 5
Interrupt
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
RESET
B8h
EXEN2
-
IEIIC
IELVI
IEKBI
IEADC
IESPI
IEPWM
00h
C0H
EXF2
TF2
IICIF
LVIIF
KBIIF
ADCIF
SPIIF
PWMIF
00H
A9h
-
-
IP0.5
IP0.4
IP0.3
IP0.2
IP0.1
IP0.0
00h
B9h
-
-
IP1.5
IP1.4
IP1.3
IP1.2
IP1.1
IP1.0
00h
Table: Priority levels
IP1.x
IP0.x
Priority Level
0
0
1
1
0
1
0
1
Level0 (lowest)
Level1
Level2
Level3 (highest)
Table: Groups of priority
Bit
IP1.0, IP0.0
IP1.1, IP0.1
IP1.2, IP0.2
IP1.3, IP0.3
IP1.4, IP0.4
IP1.5, IP0.5
External interrupt 0
Timer 0 interrupt
External interrupt 1
Timer 1 interrupt
Serial channel 0 interrupt
Timer 2 interrupt
Group
Comparator interrupt
-
PWM interrupt
SPI interrupt
ADC interrupt
KBI interrupt
LVI interrupt
IIC interrupt
Specifications subject to change without notice, contact your sales representatives for the most recent information.
ISSFA-0233
4
Ver A 2010/09
SM39R 系列 PWM 功能使用方法
4.3 PWM C 語言程式範例,各通道產生的時序圖,Fosc=25MHz:
5
PWM 中斷應用範例程式
1. PWM 通道 0/1 全部致能,Ch.0 閒置高準位輸出 Ch.1 閒置低準位輸出
Description
C 語言
2. Period: 1024 階(10bit) Duty cycle: Ch.0 512 階 / Ch.1 256 階
//=========================================================================
//
SYNCMOS
TECHNOLOGY
//=========================================================================
#include "SM39R16A2.h"
#include "PWM.h"
#define PWM_VECTOR 8
//PWM Interrupt Vevtor
#define d_PWMCS 0x00
//PWMCS[2:0] (PWM clock select 0~7)
#define d_PWMXEN 0x03
//PWM Channel Enable (0~F)
#define d_PWMP0 0x01
//PWM0 Channel idle polarity select
#define d_PWMP1 0x00
//PWM1 Channel idle polarity select
#define d_PWMIE 0x01
//PWM Interrupt Enable bit
unsigned int PWMMD = 0x03FF; //PWMMD[9:0]=Period (PWM Max Data Register 0~1023)
unsigned int PWMD0 = 0x01FF; //PWMD0[9:0]=Duty (PWM Channel 0 Data Register 0~1023)
unsigned int PWMD1 = 0xFF; //PWMD1[9:0]=Duty (PWM Channel 1 Data Register 0~1023)
//---------------------------------------------------------------------//
void PWM_initialize(void) //Initialize PWM
{
EA = 0;
//Disable All Interrupt Function
IEPWM = (d_PWMIE);
//Enable PWM Interrupt Function
PWMMDH = PWMMD >> 8;
PWMMDL = PWMMD;
EA=1;
//Enable All Interrupt
Specifications subject to change without notice, contact your sales representatives for the most recent information.
ISSFA-0233
5
Ver A 2010/09
SM39R 系列 PWM 功能使用方法
}
//---------------------------------------------------------------------//
void PWM_Output(unsigned int PWMD0,unsigned int PWMD1) //PWM Output Set
{
PWMD0H = (PWMD0>>8) | (d_PWMP0<<7);
PWMD0L = (PWMD0);
PWMD1H = (PWMD1>>8) | (d_PWMP1<<7);
PWMD1L = (PWMD1);
PWMC = PWMC|(d_PWMCS<<5)|(d_PWMXEN);
}
//---------------------------------------------------------------------//
void PWM_Disable(void)
{
IEPWM = 0;
//Disable PWM Interrupt
PWMC = 0;
//Disable PWM Function
}
//---------------------------------------------------------------------//
void PWM_ISR(void) interrupt PWM_VECTOR //PWM Interrupt Subroutine
{
while(PWMIF)
{
PWMIF = 0; //Clear PWM Flag
}
}
//---------------------------------------------------------------------//
void main(void) //Main Function Start
{
PWM_initialize();
//Call PWM Initial Subroutine
PWM_Output(PWMD0, PWMD1);
while(1)
{
;
}
//PWM_Disable();
}
Description
3. PWM 通道 0/1 全部致能,Ch.0 閒置高準位輸出 Ch.1 閒置高準位輸出 不進 PWM 中斷
4. Period: 1024 階(10bit) Duty cycle: Ch.0 256 階 / Ch.1 512 階
Specifications subject to change without notice, contact your sales representatives for the most recent information.
ISSFA-0233
6
Ver A 2010/09
SM39R 系列 PWM 功能使用方法
匯編
//=========================================================================
//
SYNCMOS
TECHNOLOGY
//=========================================================================
#include " SM39R16A2.h "
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
d_Fosc
0x00
d_Fosc_div2
0x20
d_Fosc_div4
0x40
d_Fosc_div6
0x60
d_Fosc_div8
0x80
d_Fosc_div12
0xA0
d_T0_overflow
0xC0
d_T0_EX
0xE0
d_PWMCS
d_Fosc_div12
d_PWMXEN
0x03
d_IDLE_HI
0x80
d_IDLE_LO
0x00
d_PWMP0
d_IDLE_HI
d_PWMP1
d_IDLE_HI
d_PWMIE
0x00
org
0000h
//Start
call
PWM_initialize
Start:
jmp
Start
PWM_initialize:
mov
PWMMDH, #000
mov
PWMMDL, #100
mov
PWMD0H, #d_PWMP0
orl
PWMD0H, #0
mov
PWMD0L, #10
mov
PWMD1H, #d_PWMP1
orl
PWMD1H, #0
mov
PWMD1L, #20
mov
PWMC,
#d_PWMCS
orl
PWMC,
#d_PWMXEN
ret
PWM_Disable:
//mov
IEPWM, #0
mov
PWMC,
#0
ret
end
//PWMCS[2:0] (PWM clock select 0~7)
//PWM0~1 Channel Enable (0~3)
//PWM0 Channel idle polarity select
//PWM1 Channel idle polarity select
//PWM Interrupt Enable bit
//set pwm max period
//set idle polarity
//set pwm0 duty cycle
//set idle polarity
//set pwm1 duty cycle
//PWM CLK select
//PWM channel enable
//Disable PWM Interrupt
//Disable PWM Function
新茂國際科技希望能為客戶減少開發的時間及辛勞,故提供 “Codzard 範例程式產生器"
可於 新茂網站首頁>下載專區> 軟體下載 內下載此軟體,如有任何建議,請來信告知,謝謝!
銷售客服
電子信箱: [email protected]
技術支援
電子信箱: [email protected]
Specifications subject to change without notice, contact your sales representatives for the most recent information.
ISSFA-0233
7
Ver A 2010/09