您的位置:>>网站首页>学习园地>定时器中断进入方法

定时器中断进入方法1:(以MC68HC908JK8_DIP20的timer1溢出中断为例)

/*********************************************************/
/* 功能描述: 定时器1溢出中断,溢出时间1ms, */
/* 编译环境:CodeWarrior3.1 */
/* IC 型号 :mc68hc908jk8_dip20 */
/* 晶振频率:4.9152MHz */
/* 编辑时间:2005_11_29 */
/* 编者:laomu */
/*********************************************************/
#include <hidef.h> /* for EnableInterrupts macro */
#include <MC68HC908JK8.h> /* include peripheral declarations */

uint time_ms;
struct TIME {
uchar sec;
uchar min;
uchar hour;
} time,time1;

void init_sys(void);

/*********************************************************/
/* 函数:__interrupt void Cpu_Interrupt(void) */
/* 功能:中断程序,执行空操作 */
/*********************************************************/
__interrupt void Cpu_Interrupt(void){
}

/*********************************************************/
/* 函数:__interrupt void timer1_overflow(void) */
/* 功能:定时器1中断溢出程序,溢出时间为1ms,实现计时 */
/*********************************************************/
__interrupt 5 void timer1_overflow(void){

asm LDA T1SC
T1SC_TOF = 0;
time_ms++;

if(999<time_ms){
time.sec++;
time_ms = 0;
if(59<time.sec){
time.sec = 0;
time.min++;
if(59<time.min){
time.min = 0;
time.hour++;
if(23<time.hour) time.hour = 0;
}
}

}

EnableInterrupts;
}

/*********************************************************/
/* 中断向量的设置: */
/*********************************************************/
void (* const _vect[])() @0xFFDE = { // Interrupt table
Cpu_Interrupt, //ADC IF15 :FFDE-FFDF
Cpu_Interrupt, //KBI IF14 :FFE0-FFD1
Cpu_Interrupt, //SCI_T IF13 :FFE2-FFE3
Cpu_Interrupt, //SCI_R IF12 :FFE4-FFE5
Cpu_Interrupt, //SCI_E IF11 :FFE6-FFE7
Cpu_Interrupt, //NC IF10 :FFE8-FFE9
Cpu_Interrupt, //NC IF9 :FFEA-FFEB
Cpu_Interrupt, //T2_O IF8 :FFEC-FFED
Cpu_Interrupt, //T2_CH1 IF7 :FFEE-FFEF
Cpu_Interrupt, //T2_CH0 IF6 :FFF0-FFF1

timer1_overflow, //T1_O IF5 :FFF2-FFF3

Cpu_Interrupt, //T1_CH1 IF4 :FFF4-FFF5
Cpu_Interrupt, //T1_CH0 IF3 :FFF6-FFF7
Cpu_Interrupt, //NC IF2 :FFF8-FFF9
Cpu_Interrupt, //IRQ IF1 :FFFA-FFFB
Cpu_Interrupt, //SWI IF0 :FFFC-FFFD
};

/*********************************************************/
/* 函数:void main(void) */
/*********************************************************/
void main(void) {

DisableInterrupts; /* disable interrupts */
/* include your code here */
init_sys();
EnableInterrupts; /* enable interrupts */
T1SC_TSTOP = 0; // 开始记数

for(;;) {

} /* loop forever */
/* please make sure that you never leave this function */
}

/*********************************************************/
/* 函数名:void init_sys(void) */
/* 功能:系统初始化 */
/* 1ms:Fbus = 4/4.9152=0.8138us; */
/* X*0.8138=1000us; */
/* X=1230=0x04CE */
/*********************************************************/
void init_sys(void){

CONFIG1 = 0x03; // close watchdog
CONFIG2 = 0x80;

//时间变量初始化
time.sec = 0;
time.min = 0;
time.hour = 0;

//初始化定时器1为溢出模式,溢出时间1毫秒
asm LDA T1SC
T1SC = 0x70;

T1MODH=0x04;
T1MODL=0xCE; // 1ms overflow

}

定时器中断进入方法2:(以MC68HC908JK8_DIP20的timer1溢出中断为例)

/*********************************************************/
/* 功能描述: 定时器1溢出中断,溢出时间1ms, */
/* 编译环境:CodeWarrior3.1 */
/* IC 型号 :mc68hc908jk8_dip20 */
/* 晶振频率:4.9152MHz */
/* 编辑时间:2005_11_29 */
/* 编者:laomu */
/*********************************************************/
#include <hidef.h> /* for EnableInterrupts macro */
#include <MC68HC908JK8.h> /* include peripheral declarations */

uint time_ms;
struct TIME {
uchar sec;
uchar min;
uchar hour;
} time,time1;

void init_sys(void);

/*********************************************************/
/* 函数:__interrupt void timer1_overflow(void) */
/* 功能:定时器1中断溢出程序,溢出时间为1ms,实现计时 */
/*********************************************************/
interrupt void timer1_overflow(void){

asm LDA T1SC
T1SC_TOF = 0;
time_ms++;

if(999<time_ms){
time.sec++;
time_ms = 0;
if(59<time.sec){
time.sec = 0;
time.min++;
if(59<time.min){
time.min = 0;
time.hour++;
if(23<time.hour) time.hour = 0;
}
}

}

EnableInterrupts;
}

 

/*********************************************************/
/* 函数:void main(void) */
/*********************************************************/
void main(void) {
DisableInterrupts; /* disable interrupts */
/* include your code here */
init_sys();
EnableInterrupts; /* enable interrupts */
T1SC_TSTOP = 0; // 开始记数

for(;;) {

} /* loop forever */
/* please make sure that you never leave this function */
}

 

/*********************************************************/
/* 函数名:void init_sys(void) */
/* 功能:系统初始化 */
/* 1ms:Fbus = 4/4.9152=0.8138us; */
/* X*0.8138=1000us; */
/* X=1230=0x04CE */
/*********************************************************/
void init_sys(void){

CONFIG1 = 0x03; // close watchdog
CONFIG2 = 0x80;


//时间变量初始化
time.sec = 0;
time.min = 0;
time.hour = 0;

//初始化定时器1为溢出模式,溢出时间1毫秒
asm LDA T1SC
T1SC = 0x70;

T1MODH=0x04;
T1MODL=0xCE; // 1ms overflow


}

在PRM文件设置中断向量地址,编译即可

以上两个程序均得到验证无误,希望对初学者有所帮助;

版权所有 Copyright 2005-2006 老木屋  电子邮件: laomu-room@163.com  QQ:54170265
电话:0755-21130335  手机:13715060135