main.c

来自「飞思卡尔HCS12里的定时和计时的方案,列出例子,以供参考.这个不额外占资源,以」· C语言 代码 · 共 58 行

C
58
字号
///////////////////////////////////////////////////////////////////
//精确定时10ms
//author:whut_wj

#include <hidef.h>      /* common defines and macros */
#include <mc9s12dg128.h>     /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"

unsigned int m;
void Start_PLL(void)   //busCLK=20M
{
  REFDV=0x03;          // PLLCLK =2*OSCCLK*(SYNR + 1)/(REFDV + 1)
  SYNR=0x04;	         //        =2*16M*3/2=40M
  asm{
    BRCLR CRGFLG,#$08,*
    BSET CLKSEL,#$80
    }													  
}

void Timerch0Init(void)
{
   TSCR2_PR   =0x04;  //prescale factor is 16, bus clock/16=20Mhz/16=1250000
   TIOS       =0x01;  //0 channel output compare
   TC0        =0x30D4;//channel 0 output compare holding register 0x30D4*(1/1250000)=10ms
   TCTL2      =0x02;	 //output  low level
   TSCR1_TFFCA=1;  	 // if set, TFLG1 can be cleared auto, manual clear is useless
   TIE=0x01;  //Timer0 Interrupt enable 
   TSCR1_TEN  = 1;      //Timer enable

}

void main(void)
 {

   DisableInterrupts;
   Start_PLL();
   Timerch0Init();
   DDRB=0xff;
   PORTB=0;
     

   for(;;) 
      {
        EnableInterrupts;
      } /* wait forever */
    /* please make sure that you never leave this function */
  
}

#pragma CODE_SEG __NEAR_SEG NON_BANKED
interrupt 8 void timerch0(void){

  DisableInterrupts;
  m=TCNT;
  TC0=m+0x30D4;
  PORTB ^=0xff ;

}

⌨️ 快捷键说明

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