?? 1.txt
字號:
下面用PWM2和PWM3來控制。
#include <hidef.h> /* common defines and macros */
#include <mc9s12dg128.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"
int cnt2=80,cnt3=20;
void init_pwm(void)
{
PWME=0X00; //PWM禁止
PWMPRCLK=0x10; //Clock B=8M/2=4MHz
PWMSCLB=200; //Clock SB=Clock B/2/200=10kHz
PWMCLK=0x0c; //設置PWM2、PWM3的時鐘源均為Clock SB
PWMPOL=0x0c; //設置PWM2、PWM3的極性均為1
PWMCAE=0x00; //設置PWM2、PWM3的對齊方式為左對齊
PWMPER2=200;
PWMDTY2=cnt2; //設置PWM2的周期內高電平時間
PWMPER3=200;
PWMDTY3=cnt3; //設置PWM3的周期內高電平時間
PWME=0x0c; //PWM2、PWM3使能
}
void init_ect(void)
{
TSCR1_TEN=0; //計數器清零
TSCR2_PR=7; // Clock=Bus/128
TIOS_IOS1=1; //設置ECT1口為輸出比較
TC1=406250; //設置每6500ms產生定時中斷
DLYCT=0x01; //延遲256個Bus周期,即32us
PACN1=0; //清零TC1寄存器
TIE_C1I=1; //設置ECT1口中斷使能
TSCR1_TEN=1; //計數開始
}
void interrupt 9 run(void) //計數650ms后中斷
{
TFLG1_C1F=1; //清除ECT1的中斷標志
TC1=TC1+406250;
cnt2=cnt2-5;
cnt3=cnt3+5;
PWMPER2=cnt2;
PWMPER3=cnt3;
if (cnt2==0)
init_pwm();
}
void main(void)
{
DDRB=0XFF;
PORTB=0x04;
init_pwm();
init_ect();
EnableInterrupts;
for(;;)
{ }
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -