/**************************************************************************************
龙丘MC9S12XS128 多功能开发板
Designed by Chiu Sir
E-mail:chiusir@163.com
软件版本:V1.1
最后更新:2009年2月28日
相关信息参考下列地址:
博客: http://longqiu.21ic.org
淘宝店:http://shop36265907.taobao.com
------------------------------------
Code Warrior 4.7
Target : MC9S12XS128
Crystal: 16.000Mhz
busclock:16.000MHz
pllclock:32.000MHz
演示程序使用说明:
1.实时时钟的演示:
2.毫秒级计时输出;
3.串口输出速率:9600bps.
4.中断的使用。
*****************************************************************************************/
#include <hidef.h>
#include <MC9S12XS128.h>
#include <stdio.h>
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"
volatile dword u32_time_cnt=0;
//====================中断函数==================================
#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt 7 RTI_ISR(void) { // 32.75ms timer
overflow
++u32_time_cnt;
PORTB_PB2=~PORTB_PB2;
CRGFLG|=0X80;//Write 1 to clear RTIF bit
}
// 函 数 名: void DLY_ms(word x)
// 功能描述: 毫秒级延时
void DLY_ms(int ms) //x取值1~255;
{
int ii,jj;
if (ms<1) ms=1;
for(ii=0;ii<ms;ii++)
for(jj=0;jj<2770;jj++); //32MHz--1ms
}
//-----------------------------------------------------
static void Port_Init(void)
{
DDRA = 0xff;
PORTA=
0x00;
DDRB = 0xff; //LED PTB0--7,
PORTB= 0xff; //LEDs off
}
//-----------------------------------------------------
static void SCI_Init(void)
{
SCI0CR2=0x2c; //enable Receive Full Interrupt,RX
enable,Tx enable
SCI0BDH=0x00; //busclk
8MHz,19200bps,SCI0BDL=0x1a
SCI0BDL=0x68;
//SCI0BDL=busclk/(16*SCI0BDL)
//busclk 16MHz,
9600bps,SCI0BDL=0x68
}
void uart_putchar(unsigned char ch)
{
if (ch == '\n')
{
while(!(SCI0SR1&0x80))
;
SCI0DRL=
0x0d;
//output'CR'
return;
}
while(!(SCI0SR1&0x80)) ;
//keep waiting when not empty
SCI0DRL=ch;
}
void putstr(char ch[])
{
unsigned char ptr=0;
while(ch[ptr]){
uart_putchar((unsigned
char)ch[ptr++]);
}
}
// setup of the RTI interrupt frequency
static void RTI_Init(void)
{
RTICTL=0x77; //8x2^16
=>32,75ms,30.5175Hz
//CRGINT=0X80; //enable RTI
Interrupt
CRGINT=0X80; //enable RTI Interrupt
}
// PLL初始化子程序 BUS Clock=16M
void setbusclock(void)
{
CLKSEL=0X00; //disengage
PLL to system
PLLCTL_PLLON=1; //turn on PLL
SYNR=1;
REFDV=1;
//pllclock=2*osc*(1+SYNR)/(1+REFDV)=32MHz;
_asm(nop);
//BUS CLOCK=16M
_asm(nop);
while(!(CRGFLG_LOCK==1)); //when pll
is steady ,then use it;
CLKSEL_PLLSEL
=1; //engage
PLL to system;
}
//-----------------------------------------------------
#pragma CODE_SEG DEFAULT
void Init_Dev(void)
{
setbusclock();
Port_Init();
SCI_Init();
RTI_Init();
}
//====================main()==================================
#pragma CODE_SEG DEFAULT
void main(void)
{
char txtbuf[66]="";
byte u8_mintes=0;
word u16_sec=0,u16_ms=0;
Init_Dev();
PORTB=0x00;
putstr("\nLongQiu s.&t. Co. Ltd.");
putstr("\nhttp://shop36265907.taobao.com");
DLY_ms(1000);
PORTB=0xFF;
EnableInterrupts;
for(;;)
{
//----------时钟计时器---------------------------------
u16_sec=u32_time_cnt*131/4;//理论值为32.75ms,时间计算部分
u16_ms=u16_sec%1000;
u16_sec=u16_sec/1000;
sprintf(txtbuf,"\n%d:%02ds-%03dms
",u8_mintes,u16_sec,u16_ms);
putstr(txtbuf);
if(u16_sec>58)
{
u8_mintes++;
u32_time_cnt=0;
}
//--------------------------------------------------------
PORTB_PB0=~PORTB_PB0;
DLY_ms(50);
}//for(;;)
} //main