⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 9s12fortime.c

📁 9s12定时器的应用实例
💻 C
字号:
#include <hidef.h>      /* common defines and macros */
#include <mc9s12dg128.h>     /* derivative information */

/*======================================
宏定义
=========================================*/
#define GETCHARFLAG    0X40
#define PUTCHARFLAG     0X01

#define UNITTSCR1		0x80
#define UNITTSCR2		0x80

#define X10US  1592


#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"

unsigned  int cnt;
unsigned char zkbpre,zkb;


 /*********************************************************/
/* 函数名:  */
/* 功能:初始化 */
/*********************************************************/
 void timeout_10us(void)
{

  TIOS_IOS4 = 1;   // timer 4 output compare
  TIE_C4I = 1;     // interrupt 4 enable
  
  TSCR2_PR = 1;    // prescale 24Mhz/2 = 12 000 000 Hz
  TC4 = 120 ;    // 120 counter: 12000000Hz/120 = 100 000 Hz (10us)
  
  TSCR1_TSFRZ = 1;
  TSCR1_TEN = 1;   // abilitazione timer 
}



//////////////////////////////////////////////////////////////////////
void SCI_ini(void) {
  SCI0CR2=0x0c;
  SCI0BDH=0x00;
  SCI0BDL=0x9C;
 }
////////////////////////////////////////////////////////////////////////// 
 void SET_PLL() //通过设置五个寄存器
{CLKSEL=0x00;
 PLLCTL=0xe1; //pll control reg
 SYNR=2;     //时钟合成寄存器 00-63
 REFDV=1;    //时钟分频寄存器 00-15     PLLCLK=2*OSCCLK*(SYNR+1)/(REFDV+1)
              //=48MHZ     BUS CLOCK=0.5*CORE CLOCK=24MHZ 
 PLLCTL=0x60;
 asm NOP;
 asm NOP;
 asm NOP;
 while(0==(CRGFLG&0x08)); //JUDGE THE LOCK BIT, =1 CLOCK STABLE
 CLKSEL=0x80; //SELECT PLL clock
}

 //////////////////////////////////////////////////
void IOInit(void){

    DDRB = 0xff;
   // PORTB=0xff;
    DDRA = 0xff;

}
//////////////////////////////////////////////////



/*********************************************************/
/* 函数名:用户功能函数 */
/* check ok */
/*********************************************************/
 //////////////////////////////////////////////////////
 unsigned char get_char(void){
  
  while((SCI0SR1&0x20)==0){
  }
  return(SCI0DRL);
}

 ////////////////////////////////////////////////////
void put_char(unsigned char c){

     while((SCI0SR1&0x80) == 0){}
     SCI0DRL = c;
}



 ////////////////////////////////////////////////////////
 /////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////
 
  
void main(void)
{ zkb=152;
  SET_PLL();
  SCI_ini();
  IOInit();
  timeout_10us();

 
  EnableInterrupts;
  while(1)
  {		zkbpre=zkb;
      zkb=get_char();
      if(zkbpre==zkb) 
         continue;
      else 
        put_char(zkb);
  }
  
}


 /*********************************************************/
/* 函数名:定时中断函数 */
/* check ok */
/*********************************************************/

#pragma CODE_SEG NON_BANKED

//interrupt void _Timerovf(void) 
void interrupt 12 timer4_isr_handler(void)

{		TFLG1_C4F = 1; //clear the interrupt flag
     cnt++;
     if(cnt>=zkb)
         PORTA=0X00;
     
     else PORTA=0XFF;
    
     
     if(cnt>X10US)
      {
      cnt=0;
      PORTA=0Xff;
      }
     
}
  
#pragma CODE_SEG DEFAULT

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -