PWM應(yīng)用程序
/*
程序?qū)崿F(xiàn)功能:PP1口輸出PWM方波
程序說明:通過改變duty和period ,從而控制PWM周期和占空比
duty cycle=duty/period
PWM frequency=1M/(2*period)(Fbus=24M,scla=24)
*/
#include 《hidef.h》 /* common defines and macros */
#include “derivative.h” /* derivative-specific definitions */
void SetBusClock_24MHZ(void);
void PWMDisable(byte channel);
void PWMEnable(byte channel);
void PWMSinglePortSetting(byte channel ,byte period ,byte duty) ;
void PWMsinglePortInitial(byte channel, byte clkab,byte clock, byte polarity,byte align) ;
void Service_WD(void);
void PWMGeneralInitial(byte prclk,byte scla,byte sclb,byte ctl);
void PWMConcatenateSetting(byte channel,word period,word duty);
void main(void)
{
/* put your own code here */
//總線時(shí)鐘頻率設(shè)置:24M
SetBusClock_24MHZ();
//對(duì)預(yù)分頻時(shí)鐘,分頻時(shí)鐘A,分頻時(shí)鐘B和控制寄存器的配置
//0分頻 01級(jí)聯(lián)
PWMGeneralInitial(0,24,0,0x10);
//PWM端口寄存器的配置
// 1通道 SA時(shí)鐘 起始高電平 左對(duì)齊
PWMsinglePortInitial(1,0,1,1,0);
//PWM級(jí)聯(lián)輸出配置
//50HZ 占空比12.5%
PWMConcatenateSetting(1,10000,250);
//EnableInterrupts;
for(;;) {
_FEED_COP(); /* feeds the dog */
} /* loop forever */
/* please make sure that you never leave main */
}
//*********************************************
//函數(shù)名:PWMEnable
//函數(shù)功能:PWM單個(gè)端口使能
//函數(shù)參數(shù):一個(gè) byte 類型channel 代表PWM通道號(hào)
// 返回值:無
//********************************************
void PWMEnable(byte channel)
{
if(channel》7) channel=7;
PWME|=(1《《channel); //選擇使能位
}
//**********************************************
//函數(shù)名稱:PWMDisable
//函數(shù)功能:PWM單個(gè)端口禁止
//函數(shù)參數(shù):一個(gè)byte類型 channel 代表PWM通道號(hào)
//返回值:無
//***********************************************
void PWMDisable(byte channel)
{
if(channel》7) channel=7;
PWME&=~(1《《channel); //選擇禁止位
}
//函數(shù)功能:啟動(dòng)看門狗
void Service_WD(void)
{
CPMUARMCOP=0x55;
CPMUARMCOP=0xAA;
}
//函數(shù)功能:總線時(shí)鐘設(shè)置
void SetBusClock_24MHZ(void)
{
CPMUOSC_OSCE=1; //enable osc
/*
時(shí)鐘倍頻:24MHz BusClock
48MHz VCO
48MHz PLL
*/
CPMUSYNR=0x00|0x05; //VCOFRQ[1:0],SYNDIV[5:0]
CPMUREFDIV=0x20|0x03;//REFFRQ[1:0],REFDIV[3:0]
CPMUPOSTDIV=0x00; //POSTDIV=0;
while(!CPMUFLG_LOCK)//等待VCO穩(wěn)定
Service_WD(); //看門狗
CPMUCLKS_PLLSEL=1;
}
//*********************************************
//函數(shù)名稱: PWMSinglePortSetting
//函數(shù)功能:實(shí)現(xiàn)PWM周期寄存器和占空比寄存器通道的單獨(dú)輸出
//函數(shù)參數(shù):3個(gè) byte類型
//參數(shù)1: channel代表了當(dāng)前配置的PWM通道
//參數(shù)2: period 周期配置參數(shù)
/*
Left aligned output (CAEx = 0) PWMx Period = Channel Clock Period * PWMPERx
Center Aligned Output (CAEx = 1) PWMx Period = Channel Clock Period * (2 * PWMPERx)
*/
//參數(shù)3: duty 占空比配置參數(shù)
/*
Polarity = 0 (PPOL x =0) Duty Cycle = [(PWMPERx-PWMDTYx)/PWMPERx] * 100%
Polarity = 1 (PPOLx = 1) Duty Cycle = [PWMDTYx / PWMPERx] * 100%
*/
//返回值:無
//**********************************************
void PWMSinglePortSetting(byte channel ,byte period ,byte duty)
{
if(channel》7) channel=7;
PWMDisable(channel); //禁止該通道
switch(channel)
{
case 0:
PWMPER0=period; //設(shè)置周期寄存器
PWMDTY0=duty; //設(shè)置占空比寄存器
break;
case 1:
PWMPER1=period; //設(shè)置周期寄存器
PWMDTY1=duty; //設(shè)置占空比寄存器
case 2:
PWMPER2=period; //設(shè)置周期寄存器
PWMDTY2=duty; //設(shè)置占空比寄存器
break;
case 3:
PWMPER3=period; //設(shè)置周期寄存器
PWMDTY3=duty; //設(shè)置占空比寄存器
break;
case 4:
PWMPER4=period; //設(shè)置周期寄存器
PWMDTY4=duty; //設(shè)置占空比寄存器
break;
case 5:
PWMPER5=period; //設(shè)置周期寄存器
PWMDTY5=duty; //設(shè)置占空比寄存器
break;
case 6:
PWMPER6=period; //設(shè)置周期寄存器
PWMDTY6=duty; //設(shè)置占空比寄存器
break;
case 7:
PWMPER7=period; //設(shè)置周期寄存器
PWMDTY7=duty; //設(shè)置占空比寄存器
break;
default:break;
}
PWMEnable(channel);
}
//*********************************************
//函數(shù)名:PWMSinglePortInitial
//函數(shù)功能:PWM端口寄存器的配置
//函數(shù)參數(shù):5個(gè)byte類型
//參數(shù)1:channel 代表了當(dāng)前配置的PWM通道
//參數(shù)2:clkab 參數(shù)2,3決定了時(shí)鐘源的選擇
//參數(shù)3: clock
/*
PWM Channel 0,1,4,5
PCLKAB[0,1,4,5] PCLK[0,1,4,5] Clock Source Selection
0 0 Clock A
0 1 Clock SA
1 0 Clock B
1 1 Clock SB
PWM Channel 2,3,6,7
PCLKAB[2,3,6,7] PCLK[2,3,6,7] Clock Source Selection
0 0 Clock B
0 1 Clock SB
1 0 Clock A
1 1 Clock SA
*/
//參數(shù)4:polarity PWM極性選擇
// 0 開始為低電平,周期計(jì)數(shù)開始為高電平
// 1 開始為高電平,周期計(jì)數(shù)開始為低電平
//參數(shù)5:align PWM對(duì)齊方式選擇
// 0 輸出左對(duì)齊
// 1 輸出中心對(duì)齊
//返回值:無
//**********************************************
void PWMsinglePortInitial(byte channel, byte clkab,byte clock, byte polarity,byte align)
{
if(channel》7) channel=7;
//禁止該通道
PWMDisable(channel);
// PWM 時(shí)鐘A/B 選擇
if(clkab==0) PWMCLKAB&=~(1《《channel);
else PWMCLKAB|=(1《《channel);
// PWM 時(shí)鐘選擇寄存器設(shè)置
if(clock==0) PWMCLK&=~(1《《channel);
else PWMCLK|=(1《《channel);
//PWM 極性選擇設(shè)置
if(polarity==0) PWMPOL&=~(1《《channel) ;
else PWMPOL|=(1《《channel);
//PWM 對(duì)齊方式設(shè)置
if(align==0) PWMCAE&=~(1《《channel);
else PWMCAE|=(1《《channel);
}
//**********************************************************
//函數(shù)名:PWMGeneralInitial
//函數(shù)功能:對(duì)預(yù)分頻時(shí)鐘,分頻時(shí)鐘A,分頻時(shí)鐘B和控制寄存器的配置
//函數(shù)參數(shù):4個(gè)byte類型
//參數(shù)1 prclk
/*
Clock A or Clock B Prescaler Selects
PCKA/B2 PCKA/B1 PCKA/B0 Value of Clock A/B
0 0 0 Bus clock
0 0 1 Bus clock / 2
0 1 0 Bus clock / 4
0 1 1 Bus clock / 8
1 0 0 Bus clock / 16
1 0 1 Bus clock / 32
1 1 0 Bus clock / 64
1 1 1 Bus clock / 128
*/
//參數(shù)2: scla
// Clock SA = Clock A / (2 * PWMSCLA)
//參數(shù)3: sclb
// Clock SB = Clock B / (2 * PWMSCLB)
//參數(shù)4: ctl
/*
control[CON67,CON45,CON23,CON01,PSWAI,PFRZ]
PWM級(jí)聯(lián)控制寄存器 CON67,CON45,CON23,CON01
0 單獨(dú)一個(gè)通道
1 兩個(gè)通道級(jí)聯(lián)
PSWAI 0 等待模式禁止時(shí)鐘輸入
1 等待模式允許時(shí)鐘輸入
PFRZ 0 凍結(jié)模式允許PWM時(shí)鐘輸入
1 凍結(jié)模式禁止PWM時(shí)鐘輸入
//返回值:無
*/
//**************************************************************
void PWMGeneralInitial(byte prclk,byte scla,byte sclb,byte ctl)
{
//禁止所有的PWM通道
PWME=0x00;
//設(shè)置預(yù)分頻參數(shù)
PWMPRCLK=prclk;
//設(shè)置A分頻參數(shù)
PWMSCLA=scla;
//設(shè)置B分頻參數(shù)
PWMSCLB=sclb;
//級(jí)聯(lián)配置
PWMCTL=ctl;
}
//***********************************************************
//函數(shù)名稱:PWMConcatenateSetting
//函數(shù)功能:PWM級(jí)聯(lián)輸出配置
//函數(shù)參數(shù):1個(gè)byte類型,2個(gè)word類型
//參數(shù)1: channel代表了當(dāng)前配置的PWM通道
//參數(shù)2: period 周期配置參數(shù)
/*
Left aligned output (CAEx = 0) PWMx Period = Channel Clock Period * PWMPERx
Center Aligned Output (CAEx = 1) PWMx Period = Channel Clock Period * (2 * PWMPERx)
*/
//參數(shù)3: duty 占空比配置參數(shù)
/*
Polarity = 0 (PPOL x =0) Duty Cycle = [(PWMPERx-PWMDTYx)/PWMPERx] * 100%
Polarity = 1 (PPOLx = 1) Duty Cycle = [PWMDTYx / PWMPERx] * 100%
*/
//返回值:無
//**************************************************************
void PWMConcatenateSetting(byte channel,word period,word duty)
{
if(channel》7) channel=7;
switch(channel)
{
case 0:
case 1:PWMDisable(0); //禁止通道0
PWMDisable(1); //禁止通道1
PWMPER01=period; //設(shè)置周期寄存器
PWMDTY01=duty; //設(shè)置占空比寄存器
PWMEnable(0); //使能通道0;
PWMEnable(1); //使能通道1;
break;
case 2:
case 3:PWMDisable(2); //禁止通道2
PWMDisable(3); //禁止通道3
PWMPER23=period; //設(shè)置周期寄存器
PWMDTY23=duty; //設(shè)置占空比寄存器
PWMEnable(2); //使能通道2;
PWMEnable(3); //使能通道3;
break;
case 4:
case 5:PWMDisable(4); //禁止通道4
PWMDisable(5); //禁止通道5
PWMPER45=period; //設(shè)置周期寄存器
PWMDTY45=duty; //設(shè)置占空比寄存器
PWMEnable(4); //使能通道4;
PWMEnable(5); //使能通道5;
break;
case 6:
case 7:PWMDisable(6); //禁止通道6
PWMDisable(7); //禁止通道7
PWMPER67=period; //設(shè)置周期寄存器
PWMDTY67=duty; //設(shè)置占空比寄存器
PWMEnable(6); //使能通道6;
PWMEnable(7); //使能通道7;
break;
default:break;
}
}
評(píng)論
查看更多