注册会员
- 积分
- 115
- 威望
- 175
- 贡献
- 30
- 兑换币
- 0
- 注册时间
- 2010-11-12
- 在线时间
- 0 小时
|
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#include <MC9S12XS128.h>
//unsigned char uchar;
//uchar table[]={0xff,2897,42200,2943,41976,2988,41899,2996,41311,0x00,0xb5,0xaa};
uchar table[]={1,2,3};
uint count;
//-------------------------------------------------------
void sysclock_init(void)
{
CLKSEL=0x00;
PLLCTL_PLLON=1; //turn on PLL
SYNR =0xc0 | 0x05; //48M
REFDV=0x80 | 0x01;
POSTDIV=0x00; //pllclock=2*16*(1+SYNR)/(1+REFDV)=xxMHz;
while(CRGFLG_LOCK!=1);
CLKSEL_PLLSEL=0x01;
}
//-------------------------------------------------------
#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt 20 SCI0_ISR(void)
{
SCI0CR2_RIE=0;
//此处为串口中断需要处理的事情
PORTA_PA0=~PORTA_PA0;
SCI0CR2_RIE = 1;
}
#pragma CODE_SEG DEFAULT
//---------------------------------------------------
static void SCI_Init(void) //初始化SCI0
{
SCI0CR1 =0x00; //正常8位模式,无奇偶校验
SCI0CR2 =0x2c; //enable Receive Full Interrupt,RX enable,Tx enable
SCI0BDH =0x00;
SCI0BD =0x68; //SCI0BD=busclk/(16*SCI0BDL)
//busclk 8MHz, 9600bps,SCI0BD=0x34
//busclk 16MHz, 9600bps,SCI0BD=0x68
//busclk 24MHz, 9600bps,SCI0BD=0x9C
} //busclk 32MHz, 9600bps,SCI0BD=0xD0
//busclk 40MHz, 9600bps,SCI0BD=0x106
//-----------------------------------------------------
/*void SCI_sendchar(char *cs) { //单字符发送函数
while(SCI0SR1_TDRE==0);
SCI0DRL=*cs;
}
void SCI_sendstring(char *ss){ //字符串发送函数
while(*ss){
SCI_sendchar(ss);
++ss;
}
}*/
//-------------------------------------------------
void SCI_putchar(unsigned char *ch) //发送子程序
{
while(!(SCI0SR1&0x80));
SCI0DRL= *ch; //将待发数据写入寄存器
}
void SCI_sendchar(char *s) {
while(*s){
SCI_putchar(s);
++s;
}
}
//------------------------------------------
//------------------------------------------------------------------------
void PWM_Init(void)
{
PWME_PWME1=0; //禁止
PWMPRCLK=0x22 ; //0010 0010 B=48M/4=12M A=48M/4=12M 先B分频 后A分频
PWMSCLA=3; //SA=A/2/3=2M SA对A再分频 SA=A/2/PWMSCLA SA为2M
PWMSCLB=3; //SB=B/2/3=2M SB对B再分频 SB=B/2/PWMSCLB SB为2M
PWMCLK_PCLK1=1; //通道1选SA为时钟源
PWMPOL_PPOL1=1; //通道1先输出高电平
PWMCAE_CAE1=0X00; //左对齐
PWMCTL_CON01=1; //通道0,1级联
PWMPER01=42194; //周期Frenquency=SB/42194=2M/42194=47.4Hz
PWMDTY01=2996; //占空比为7.1%
PWME_PWME1=1; //允许
PWME_PWME3=0; //通道3
PWMCLK_PCLK3=1; //通道3选SB为时钟源
PWMPOL_PPOL3=1; //通道3先输出高电平
PWMCAE_CAE3=0X00; //左对齐
PWMCTL_CON23=1; //通道2,3级联
PWMPER23=42194; //周期Frenquency=SB/42194=2M/42194=47.4Hz
PWMDTY23=2996; //占空比为7.1%
PWME_PWME3=1;
PWME_PWME5=0; //通道5
PWMCLK_PCLK5=1; //通道5选SA为时钟源
PWMPOL_PPOL5=1; //通道5先输出高电平
PWMCAE_CAE5=0X00; //左对齐
PWMCTL_CON45=1; //通道4,5级联
PWMPER45=42194; //周期Frenquency=SB/42194=2M/42194=47.4Hz
PWMDTY45=2996; //占空比为7.1%
PWME_PWME5=1;
PWME_PWME7=0; //Disable PWM //舵机
PWMCLK_PCLK7=1; //PWM7----SB
PWMPOL_PPOL7=1; // 起始为高电平
PWMCAE_CAE7=0; //左对齐
PWMCTL_CON67=1; //通道6,7级联
PWMPER67=42194; //Frenquency=SB/42194=2M/42194=47.4Hz 周期寄存器设置
PWMDTY67=2996; //占空比为7.1%
PWME_PWME7=1; //Enable 舵机PWM
}
//----------------------------------------------------------------------------
//------------------------------------------------------------
void PIT_init(void)//定时中断初始化函数 5MS定时中断设置
{
PITCFLMT_PITE=0;//总定时器关闭
PITCE_PCE0=1;//定时器通道0使能
//PITCE_PCE1=1;//定时器通道1使能
//PITCE_PCE2=1;//定时器通道2使能
//PITCE_PCE3=1;//定时器通道3使能
PITMUX_PMUX0=0;//定时器0以微定时器0为时基
//PITMUX_PMUX1=0;//定时器1以微定时器0为时基
//PITMUX_PMUX2=0;//定时器2以微定时器0为时基
//PITMUX_PMUX3=0;//定时器3以微定时器0为时基
PITMTLD0=240-1;//8位定时器初值设定,240分频,在48MHzBusClock下,为0.2MHz。即5us
//PITMTLD1=240-1;//8位定时器初值设定
//PITMTLD2=240-1;//8位定时器初值设定
//PITMTLD3=240-1;//8位定时器初值设定
PITLD0=2000-1;//16位定时器初值设定。PITTIME*0.005MS =2000*0.005ms=10ms
//PITLD1=PITTIME-1;//16位定时器初值设定
//PITLD2=PITTIME-1;//16位定时器初值设定
//PITLD3=PITTIME-1;//16位定时器初值设定
PITINTE_PINTE0=1;//定时器中断通道0中断使能
//PITINTE_PINTE1=1;//定时器中断通道1中断使能
//PITINTE_PINTE2=1;//定时器中断通道2中断使能
//PITINTE_PINTE3=1;//定时器中断通道3中断使能
PITCFLMT_PITE=1;//总定时器开启
}
//---------------------------------------------------------------
//--------------------------------------
void Dly_ms(int ms)
{
int ii,jj;
if (ms<1) ms=1;
for(ii=0;ii<ms;ii++)
for(jj=0;jj<2670;jj++); //busclk:16MHz--1ms
}
//---------------------------------------
//--------------------------------------------------------------------
void main(void)
{
sysclock_init();
PWM_Init();
PIT_init();
SCI_Init();
DDRA=0XFF ;
PUCR_PUPBE=1;
EnableInterrupts;
for(;;)
{
PWMDTY67=2100;
PORTB=0xff;
_FEED_COP();
PORTA_PA0=~PORTA_PA0;
/* feeds the dog */
SCI_sendchar("Hello World!");
Dly_ms(1000);
} /* loop forever */
/* please make sure that you never leave main */
} |
|