注册会员
- 积分
- 132
- 威望
- 194
- 贡献
- 28
- 兑换币
- 0
- 注册时间
- 2010-5-22
- 在线时间
- 0 小时
|
#include <hidef.h> /* common defines and macros */
#include <MC9S12XS128.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"
#define ReSendStatusR SCI0SR1 //SCI状态寄存器
#define ReTestBit 5 //接收缓冲区满标志位
#define SendTestBit 7 //发送缓冲区空标志位
#define ReSendDataR SCI0DRL //数据寄存器
#define ENABLE_INTERRUPTS asm("cli") //开中断
#define DISABLE_INTERRUPTS asm("sei") //关中断
typedef unsigned char INT8U; //无符号8位数
typedef signed char INT8S; //有符号8位数
typedef unsigned int INT16U; //无符号16位数
typedef signed int INT16S; //有符号16位数
typedef unsigned long INT32U; //无符号32位数
typedef signed long INT32S; //有符号32位数
typedef float FP32; //单精度浮点数
typedef double FP64; //双精度浮点数
int data;
void MCUInit(void)
{
//(1)禁止总中断
DISABLE_INTERRUPTS;
//(2)CLKSEL的第7位置0,选择系统时钟源为OSCCLK
//在PLL程序执行前,内部总线频率=OSCCLK/2
//clksel.7(PLL选择位)决定内部总线时钟来源
//=O,BusClock=OSCCLK/2; =1,BusClock=PLLCLK/2
CLKSEL =0x00; //此时:BusClock=OSCCLK/2
//(3)禁止PLL
PLLCTL_PLLON=0;//PLLCTL.6(pllon)设为0;先关闭PLL
//(4)根据需要的时钟频率设置SYNR和REFDV寄存器
//计算公式LLCLK=2*OSCCLK*((SYNR+1)/(REFDV+1))
SYNR = 0x01; //对PLLCLK增频的因子
REFDV = 0x00; //对PLLCLK分频的因子
//(5)打开PLL
PLLCTL |= (1<<6); //PLLCTL.6(pllon)设为1;开PLL
//(6)通过判断CRGFLG寄存器的LOCK位,确定PLL是否稳定
_asm(nop);
_asm(nop);
POSTDIV = 0x00;
while ((CRGFLG&0x08) == 0x00);
//(7)时钟频率稳定后,允许锁相环时钟源作为系统时钟源;
CLKSEL |= (1<<7); //本句执行后:BusClock=PLLCLK/2
//(8)设置是否允许IRQ中断、是否允许看门狗
//INTCR &= 0xbf; //IRQCR.6(IRQEN) =0禁止IRQ中断(默认开)
COPCTL = 0x00; //COPCTL.2-0(cr2:cr0) =000禁止看门狗
}
void SCIInit(void)
{
INT8U t;
//定义波特率Bt=9600,SCI0BD=fBUS/(16*Bt)
SCI0BDL = 0xD0; //须先给低8位赋值
SCI0BDH = 0x00; //再给高8位赋值
SCI0CR1 = 0x00; //设置允许SCI,正常码输出,8位数据,无校验
t = SCI0DRL; //读数据寄存器(清0)
t = SCI0SR1; //读状态寄存器(清0)
SCI0CR2 = 0x0C; //允许SCI0接收和发送 查询方式
}
void main(void) {
/* put your own code here */
EnableInterrupts;
MCUInit() ;
SCIInit();
for(;;) {
if ((SCI0SR1 & (1<<ReTestBit)) != 0) {
data=SCI0DRL;
if ((SCI0SR1 & (1<<SendTestBit)) != 0){
SCI0DRL=data;
}
}
} /* wait forever */
/* please make sure that you never leave this function */
} |
|