首页 > 代码库 > XS128中文资料之PWM模块

XS128中文资料之PWM模块

 1 //-------------------------------------------------------------// 2 //功能说明:MC9S12XS128--PWM例程 3 //使用说明:实现通道3(PTP3)输出频率为1KHz,占空比为50%的方波,用示波器观察 4 //程序设计:DEMOK工作室(demok.taobao.com) 5 //设计时间:2010.01.21 6 //---------------------------------------------------------------// 7 #include <hidef.h> /* common defines and macros */ 8 #include "derivative.h" /* derivative-specific definitions */ 9 10 //--------------初始化函数----------------//11 //-----时钟初始化程序--------//12 void PLL_Init(void) //PLLCLK=2*OSCCLK*(SYNR+1)/(REFDV+1)13 { //锁相环时钟=2*16*(2+1)/(1+1)=48MHz14 REFDV=1; //总线时钟=48/2=24MHz15 SYNR=2;16 while(!(CRGFLG&0x08));17 CLKSEL=0x80; //选定锁相环时钟18 }19 20 //-----PWM初始化程序------//21 void PWM_Init(void) 22 {23 PWME_PWME3=0x00; // Disable PWM 禁止 24 PWMPRCLK=0x33; // 0011 0011 A=B=24M/8=3M 时钟预分频寄存器设置25 PWMSCLA=150; // SA=A/2/150=10k 时钟设置26 PWMSCLB=15; // SB=B/2/15 =100k 时钟设置 27 PWMCLK_PCLK3=1; // PWM3-----SB 时钟源的选择28 PWMPOL_PPOL3=1; // Duty=High Time 极性设置29 PWMCAE_CAE3=0; // Left-aligned 对齐方式设置30 PWMCTL=0x00; // no concatenation 控制寄存器设置 31 PWMPER3=100; // Frequency=SB/100=1K 周期寄存器设置32 PWMDTY3=50; // Duty cycle = 50% 占空比寄存器设置33 PWME_PWME3=1; // Enable PWM 使能34 }35 36 //-----------------主函数--------------------//37 void main(void) 38 {39 /* put your own code here */40 41 PLL_Init();42 PWM_Init();43 EnableInterrupts;44 45 for(;;) 46 {47 _FEED_COP(); /* feeds the dog */48 } /* loop forever */49 /* please make sure that you never leave main */50 }