📄 main.c
字号:
///////////////////////////////////////////////////////////////////
//精确定时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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -