智能车制作

 找回密码
 注册

扫一扫,访问微社区

查看: 1507|回复: 0
打印 上一主题 下一主题

PWM求解啊!!!

[复制链接]

12

主题

55

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1473
QQ
威望
745
贡献
386
兑换币
189
注册时间
2011-11-18
在线时间
171 小时
跳转到指定楼层
1#
发表于 2012-4-3 15:41:53 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
为什么写的这程序下载后,PWM口出不了出不了波形呢?

#include <hidef.h>      /* common defines and macros */
#include <MC9S12XS128.h>     /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"
void delayms(int ms)
{   
   int ii,jj;
   if (ms<1) ms=1;
   for(ii=0;ii<ms;ii++)
     for(jj=0;jj<3338;jj++);    //40MHz--1ms      
}
void SetBusCLK_40M(void)
{   
    CLKSEL=0X00;    //disengage PLL to system
    PLLCTL_PLLON=1;   //turn on PLL
    SYNR =0xc0 | 0x04;                        
    REFDV=0x80 | 0x01;
    POSTDIV=0x00;       //pllclock=2*osc*(1+SYNR)/(1+REFDV)=80MHz;
    _asm(nop);          //BUS CLOCK=40M
    _asm(nop);
    while(!(CRGFLG_LOCK==1));   //when pll is steady ,then use it;
    CLKSEL_PLLSEL =1;          //engage PLL to system;
}
void SetBusCLK_48M(void)
{   
    CLKSEL=0X00;    //disengage PLL to system
    PLLCTL_PLLON=1;   //turn on PLL
    SYNR =0xc0 | 0x05;                        
    REFDV=0x80 | 0x01;
    POSTDIV=0x00;       //pllclock=2*osc*(1+SYNR)/(1+REFDV)=96MHz;
    _asm(nop);          //BUS CLOCK=48M
    _asm(nop);
    while(!(CRGFLG_LOCK==1));   //when pll is steady ,then use it;
    CLKSEL_PLLSEL =1;          //engage PLL to system;
}
void PWM_init(void)   //脉宽调制模块的初始化
{
  
  //SB,B for ch2367
  //SA,A for ch0145
  PWME_PWME0=0X00;
  PWMPRCLK=0X44; // 0100 0100   A=B=48M/16= 3M
  PWMSCLA=150;
  PWMSCLB=150;  // SB=B/2/150=10K
  PWMCLK_PCLK0=1;  //  PWM3---SB
  PWMPOL_PPOL0=1;  // duty = high time
  PWMCAE_CAE0=0;  //  left-aligned
  PWMCTL=0X00;
  PWMPER0=100;  // frequency = SB/100 = 100Hz   100ms
  PWMDTY0=16;  //  duty cycle =
  PWME_PWME0=1;
}     

void main(void) {
  /* put your own code here */
  unsigned char LedCnt=0;
  
  SetBusCLK_48M();    // 设定总线频率
  DDRB=0xff;
  DDRA=0x01;
  PORTA=0XFE;
  PORTB=0XFE;
  
  PWM_init();
  
  EnableInterrupts;
  for(;;) {
    LedCnt=(LedCnt>0XFE?0:++LedCnt);
    delayms(500);   // 修改延时以修改数据发送频率
   
    //低电平灯亮用这句,注释掉下面那句
    PORTB=~LedCnt;         // 测试清华版系统板
    PORTA_PA0=~PORTA_PA0;  // 测试龙丘最小系统板
    //PORTB_BIT7=~PORTB_BIT7;
    //高电平灯亮用这句,注释掉上面那句   
    //PORTB=LedCnt;
  
  } /* wait forever */
  /* please make sure that you never leave this function */      
}

您需要登录后才可以回帖 登录 | 注册

本版积分规则

关于我们|联系我们|小黑屋|智能车制作 ( 黑ICP备2022002344号

GMT+8, 2024-9-20 20:43 , Processed in 0.043300 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表