⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 该程序实现MC9S12DG128B单片机的PWM5口每隔一定时间变换一次输出波形。
💻 C
字号:
//该程序令PWM5输出的矩形波每隔一定时间改变一次占空比
//By CM 2007-4-19

#include <hidef.h>      /* common defines and macros */
#include <mc9s12dg128.h>     /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"

 int counter;

//PWM5初始化函数
void PWM5_Init(void){
  PWME=0;         //关闭PWM
  PWMPRCLK=0x03;  //A_clock=Bus_clock/8=8MHz/8=1MHz
  PWMSCLA=5;      //SA_clock=A_clock/(2*5)=100KHz
  PWMCLK_PCLK5=1; //选SA_clock为PWM5的时钟源
  PWMPOL_PPOL5=1; //输出波形先高后低
  PWMCAE_CAE5=0;  //左对齐
  PWMPER5=100;    //周期=1/100KHz*100=1ms,频率=100KHz/100=1KHz
  PWMDTY5=10;     //占空比=10%
  DDRP=0xff;      //控制输出
  PWME_PWME5=1;   //使能PWM5
}

//时钟函数
void TimerOverflow(void){
  while(TCNT!=0);
  counter+=10;
  PWME_PWME5=0;
  PWMDTY5=counter;
  PWME_PWME5=1;
}
  

void main(void) {
  /* put your own code here */
  TSCR1=0x80;
  TSCR2=0x01; //0x03
  counter=10;
  DDRB=0xff;
  PORTB=0xdb;
  PWM5_Init();
  EnableInterrupts;
  asm{
    nop
  }
  for(;;) 
  {
    TimerOverflow();
    if(counter>=90){
      counter=10;
      TSCR2=0x05;
    }
  } /* wait forever */
  /* please make sure that you never leave this function */
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -