智能车制作

标题: MC9S12XS128系列例程--RTI [打印本页]

作者: chiusir    时间: 2009-3-3 16:50
标题: MC9S12XS128系列例程--RTI
**************************************************************************************
龙丘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
作者: cys179    时间: 2009-3-4 15:00


补充内容 (2015-4-23 09:36):
真人游戏|足球篮球|时时彩| 六合投注| 网络赚钱去SO娱乐城:顶级信用,提现百分百即时到账SO.CC



作者: liuxing4585    时间: 2009-3-14 09:15

作者: dalefang    时间: 2009-3-23 18:36

作者: zhaodong584    时间: 2009-4-12 17:58
THANK U!!!
作者: yangyong1011    时间: 2009-4-17 17:54
非常感谢楼主。
作者: 13673123373    时间: 2009-4-18 13:03
3Q
作者: 星星点火    时间: 2009-4-18 20:54
谢楼主
作者: tanfanqiong    时间: 2009-5-24 21:02
1# chiusir
RTI 定时是怎么计算的呢?、?/、/??
作者: starjiajia    时间: 2009-10-27 11:24
谢谢了
作者: wangguoxiao    时间: 2009-11-22 10:51
谢谢了。
作者: chiusir    时间: 2009-12-3 23:49
举手之劳,不言谢!
作者: ruiaijun    时间: 2010-3-14 14:51
保存下来看看~
作者: 大水杯    时间: 2010-3-26 19:17

作者: 大水杯    时间: 2010-3-26 19:18

作者: zhuzhu0    时间: 2010-3-28 18:10
非常感谢楼主。
作者: tanglin2007    时间: 2010-4-11 10:40
谢谢楼主
作者: hcx8911    时间: 2010-4-12 19:36
xianyongyongkan!
作者: hui_太狼    时间: 2010-5-10 19:35
看看
作者: 张海博    时间: 2010-5-17 20:23
谢谢
作者: 张海博    时间: 2010-5-17 20:24

作者: zhang54188    时间: 2010-11-13 00:38

作者: zhang54188    时间: 2010-11-13 00:39

作者: white1sky    时间: 2010-11-21 08:20
谢谢楼主分享
作者: chiusir    时间: 2011-5-17 10:15
chiusir  
RTI 定时是怎么计算的呢?、?/、/??
tanfanqiong 发表于 2009-5-24 21:02



    结合晶振的频率和RTICTL寄存器就可以计算出定时时间了:
   RTICTL=0x77;   //8x2^16 =>32,75ms,30.5175Hz
作者: qaabq    时间: 2011-8-21 22:31
千千
作者: 吴铭鸿    时间: 2011-9-24 10:50
不太理解
作者: bxsjc    时间: 2011-11-18 13:00
看看新手学习
作者: guichuqing    时间: 2011-12-29 22:16
谢了
作者: 乐逍遥a    时间: 2012-1-15 11:45





欢迎光临 智能车制作 (http://dns.znczz.com/) Powered by Discuz! X3.2