高级会员
- 积分
- 676
- 威望
- 368
- 贡献
- 180
- 兑换币
- 167
- 注册时间
- 2012-8-31
- 在线时间
- 64 小时
- 毕业学校
- 厦门大学
|
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"
void SetSysCLK_32M (void);
void initPWM(void) ;
void Pwm_init(void);
void main(void)
{
/* put your own code here */
SetSysCLK_32M;
initPWM() ;
EnableInterrupts;
for(;;)
{
// PWMDTY45=2200;
// PWMDTY01=100;
// PWMDTY23=100;
_FEED_COP(); /* feeds the dog */
} /* loop forever */
/* please make sure that you never leave main */
}
void Pwm_init(void)
{
PWMPRCLK=0x22;
PWMSCLA=4;
PWMSCLB=4;
PWMCTL_CON45=1;
PWMCTL_CON23=1;
PWMCTL_CON01=1;
PWMCLK_PCLK1=1;
PWMCLK_PCLK3=1;
PWMCLK_PCLK5=1;
PWMPER45=10000;
PWMPER01=200;
PWMPER23=200;
PWMPOL_PPOL5=1;
PWMPOL_PPOL1=0;
PWMPOL_PPOL3=0;
PWMCAE=0x00;
PWME_PWME5=1;
PWME_PWME3=1;
PWME_PWME1=1;
}
void initPWM()
{
PWME=0x00; //禁止PWM
PWMCTL=0x20; //23级联,其他都不联级
PWMCLK=0xFF; //选择的PWM时钟源为ClockSA、ClockSB
PWMPRCLK=0x22; //ClockB预分频4,ClockA分频4
PWMSCLA=2; //ClockSA预分频4对ClockA的4分频在
PWMSCLB=2; //ClockSB预分频4对ClockB的4分频在
PWMCAE=0; //所有的通道左对齐
PWMPOL=0xFF; //所有PWM一开始输出高电平
PWMPER23=40000; //重点计算的 PWM23周期 32M/(4*4)=2M 1/(2M)*40000=20ms
PWMDTY23=3016; //占空比///高电平的时间
PWMPER5=250; //PWM5周期
PWMDTY5=100; //PWMDTY5大小与反向转速成正比
PWMPER4=250; //PWM4周期
PWMDTY4=100; //PWMDTY4大小与正向转速成正比
PWME_PWME4=1; //允许PWM输出
PWME_PWME5=1; //允许PWM输出
PWME_PWME3=1; //允许PWM输出
}
void SetSysCLK_32M (void)
{
CLKSEL = 0X00; // disengage PLL to system
PLLCTL_PLLON = 1; // turn on PLL
SYNR = 0x40|0x03; // pllclock=2*osc*(1+SYNR)/(1+REFDV)=64MHz;
REFDV = 0x80|0x01;
POSTDIV = 0x00;
_asm (nop); // BUS CLOCK=32M
_asm (nop);
while (!(CRGFLG_LOCK == 1)); // when pll is steady ,then use it;
CLKSEL_PLLSEL = 1; // engage PLL to system;
}
|
|