📄 speedandmonitor.txt
字号:
#include <hidef.h> /* common defines and macros */
#include <MC9S12XS128.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"
/***************时钟初始化 *****************************/
#include <hidef.h> /* common defines and macros */
#include <MC9S12XS128.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"
#define uint unsigned int
void InitPLL(void);
void InitPWM (void);
void delay(uint k);
void main(void)
{
InitPLL();
InitPWM();
DDRB=0xff; /* PTB as output */
PORTB=0xff; /* LEDs off */
for (;;)
{
delay(1);
PORTB_PB0=~PORTB_PB0;
// delay(2);
// PWMDTY01=2040;
// delay(2);
// PWMDTY01=2550;
// delay(2);
// PWMDTY01=3070;
// delay(2);
//PWMDTY01=3300;
delay(20);
PWMDTY01=2500;
PWMDTY23=4699;
delay(20);
PWMDTY23=4999;
//delay(2);
// PWMDTY01=4100;
//delay(2);
//PWMDTY01=4600;
// delay(20);
// PWMDTY01=3500;
// delay(2);
// PWMDTY01=5100;
// delay(2);
// PWMDTY01=6600;
// delay(2);
// PWMDTY01=7050;
// delay(2);
//PWMDTY01=5400;
}
}
/***************时钟初始化 *****************************/
void InitPLL(void)
{
//内部总线频率=OSCCLK/2
CLKSEL = 0x00; //BusClock=OSCCLK/2
PLLCTL = 0xb1; //禁止锁相环电路
SYNR = 3; //计算公式:PLLCLK=2*OSCCLK*((SYNR+1)/(REFDV+1))
REFDV = 1; //时钟为时钟64mhz 总线32mhz
PLLCTL = 0x60; //允许PLL电路
asm NOP;
asm NOP;
asm NOP;
while ((CRGFLG&0x08) == 0x00);//当CRGFLG的LOCK位为0后PLL稳定
CLKSEL =0X80; //选择PLL时钟BusClock=PLLCLK/2
}
/*********************************************************/
/************************************************************
通道1输出周期为20ms,占空比可调的方波
通道0,1级联组成16位寄存器
**************************************************************/
void InitPWM (void)
{
PWME=0X00; //禁止PWM输出
PWMCTL=0X30; //01,23通道级联
PWMPRCLK=0X04; //Clock B Prescaler=bus clockA Prescaler=2Mhz
PWMSCLB=0x05; // ClockSB=CLOCKB/(2*5)
PWMCLK=0x0C; //0,1通道是A,2,3通道时钟选择ClockSB
PWMPOL=0X0F; //起始电平为高
PWMCAE=0X00; //左对齐
//PWM0=PP0 // PWMx Period = Channel Clock Period * (2 * PWMPERx)
PWMPER01=39999; //ClockA/(39999+1)=50hz
//PWMDTY01=5110; //最右边 [(PWMDTYx+1)/(PWMPERx+1)]*100%
PWMDTY01=3600; // 中间位置
//PWMDTY01=2040; // 最左边
//PWM2=PP2;
// PWMPER2=99; //312.5Hz
//PWMDTY2=49; // %50
//输出到电调320hz,可调0.01%
PWMPER23=9999; //320Hz
PWMDTY23=4999; // %50
PWME = 0x0F; //使能PWM
}
void delay(uint k)
{
uint i,j;
for(;k>0;k--)
for(i=0;i<600;i++)
for(j=0;j<4000;j++);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -