金牌会员
- 积分
- 1382
- 威望
- 406
- 贡献
- 768
- 兑换币
- 10
- 注册时间
- 2009-9-4
- 在线时间
- 104 小时
|
今天一下午测试XS128的定时器模块,包括IOC和PIT,感觉飞思这个系列的定时器做得用起来有些不方便,Reference Manuel里说得也不是太清楚,尤其是输出比较的用法,在论坛里逛了一大圈也没明白……后来调通了这里贴出我的代码供初学者参考。
#include <hidef.h> /* common defines and macros */
#include <MC9S12XS128.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"
#pragma CODE_SEG __NEAR_SEG NON_BANKED
unsigned int counter;
void interrupt 8 Compare_ISR(void)
{
if( counter == 50000 ) //Although enormous,PORTB flashes very fast at 16MHz
{
PORTB = ~PORTB; // Toggle PORTB Every 50000 times
counter = 0;
}
counter += 1;
TFLG1 = 0x01; // Clear T0 interrupt flag
}
void SetBusCLK(void) /* 16MHz */
{
CLKSEL = 0x00; /* disengage PLL to system */
PLLCTL_PLLON = 1; /* turn on PLL */
SYNR = 0x01;
REFDV = 0x81;
POSTDIV = 0x00;
_asm(nop); /* BUS CLOCK=80M */
_asm(nop);
while( !(CRGFLG_LOCK == 1) ); /* when pll is steady ,then use it */
CLKSEL_PLLSEL =1; /* engage PLL to system */
}
void OutputCompare_Init(void)
{
TIE = 0x00; /* Disable all channel interrupt */
TIOS = 0x01; /* channel 0 set to Output Compare */
TSCR1 = 0x90; /* Enable Timer; */
/* Timer interrupt flag can be cleared(channel 0) */
TSCR2 = 0x07; /* Prescale Factor = 128 */
TC0 = 0xFF00;
TIE = 0x01; /* Enable channel 0 interrupt */
}
void Port_Init(void)
{
DDRB = 0xFF;
PORTB = 0xFF;
}
void main(void)
{
/* put your own code here */
DisableInterrupts;
SetBusCLK();
Port_Init();
OutputCompare_Init();
counter = 0;
EnableInterrupts;
for(;;)
{
_FEED_COP();
}
/* wait forever */
/* please make sure that you never leave this function */
} |
|