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

📄 1.txt

📁 PWM电机控制代码,由pwm2,pwm3控制.
💻 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 + -