跨届大侠
北京龙邱智能科技有限公司
- 积分
- 10332
- 威望
- 2905
- 贡献
- 6151
- 兑换币
- 4515
- 注册时间
- 2008-6-7
- 在线时间
- 638 小时
|
MC9S12DG128系列例程--RTI
/**************************************************************************************
龙丘MC9S12(DG128)多功能开发板V4.0
Designed by Chiu Sir
E-mail:chiusir@163.com
软件版本:V1.1
最后更新:2008年12月01日
相关信息参考下列地址:
博客: http://longqiu.21ic.org
淘宝店:http://shop36265907.taobao.com
------------------------------------
Code Warrior 4.6
Target : MC9S12DG128B
Crystal: 16.000Mhz
busclock:16.000MHz
pllclock:32.000MHz
目录路径:F:\IVTech\
============================================
演示程序使用说明:
1.实时时钟的演示:
2.毫秒级计时输出;
3.串口输出速率:9600bps.
4.中断的使用。
*****************************************************************************************/
#i nclude <hidef.h> /* common defines and macros */
#i nclude <mc9s12dg128.h> /* derivative information */
#i nclude <stdio.h>
#i nclude "LQprintp.h"
#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"
typedef unsigned char U8;
typedef unsigned int U16;
typedef unsigned long U32;
typedef signed char S8;
typedef signed int S16;
volatile U32 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_BIT2=~PORTB_BIT2;
CRGFLG|=0X80;//Write 1 to clear RTIF bit
}
/*******************************************************************************
*** 函 数 名: void DLY_ms(U16 x)
*** 功能描述: 毫秒级延时;
*** 全局变量: NO !
*** 输 入: U16 x !
*** 输 出: NO !
*** 修 改 人: 日期:2007-11-28
*** 函数说明:
******************************************************************************/
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
//for(jj=0;jj<4006;jj++); //48MHz--1ms
//for(jj=0;jj<5341;jj++); //64MHz--1ms
}
//-----------------------------------------------------
static void Port_Init(void)
{
DDRA = 0xff;
PORTA= 0x00;
DDRB = 0xff; //LED PTB0--7,
PORTB= 0xff; //LEDs on
}
//-----------------------------------------------------
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 8MHz, 9600bps,SCI0BDL=0x34
//busclk 16MHz, 9600bps,SCI0BDL=0x68
//busclk 24MHz, 9600bps,SCI0BDL=0x9C
} //busclk 32MHz, 9600bps,SCI0BDL=0xD0
// 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)
{
S8 txtbuf[66]="";
U8 u8_mintes=0;
U16 u16_sec=0,u16_ms=0;
Init_Dev();
PORTB=0x00;
putstr("\nLongQiu s.&t.\n");
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,"LONGQIU--time elapsed: %d:%02ds-%03dms ",u8_mintes,u16_sec,u16_ms);
printp("\n\t%s",txtbuf);
if(u16_sec>58)
{
u8_mintes++;
u32_time_cnt=0;
}
//--------------------------------------------------------
PORTB_BIT0=~PORTB_BIT0;
}//for(;;)
} //main
下面是完整工程,需要的请点击下载:demoRTI.rar
看帖不回是不道德的!
|
|
|