转载--MCF52JM128完整键盘中断工程,KBI演示程序
0赞
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
/*********************变量和函数定义****************************/
/***********************I/O初始化********************************/
void IO_INIT(void)
{
PTGDD &= 0xF0; /*set PTG0-3 to input*/
PTGPE |= 0x0F; /*enable PTG0-3 pull-up resistor*/
PTFDD =0XFF; // LED
}
/************************KB初始化********************************/
void KB_INIT(void)
{
KBI1PE =0XC3; /* Enable KBI10,1,6,7*/
KBI1SC_KBIE = 1; /* Enable Keyboard Interrupts */
//KBI1SC_KBMOD = 1;
KBI1PE =0XC3; /* Enable Pullup for Keyboard pin */
KBI1SC_KBACK = 1; /* Clear Pending Keyboard Interrupts */
}
/********************************键盘中断**************************/
interrupt 87 void KBI1_ISR(void)
{
//KBI1SC_KBACK = 1; //清除键盘中断标志
//delayms(1);
if(PTGD_PTGD0==0){ //查询是由哪一个口引起的中断
PTFD_PTFD0 = ~PTFD_PTFD0;
}
else if(PTGD_PTGD1==0){
PTFD_PTFD1 = ~PTFD_PTFD1;
}
else if(PTGD_PTGD2==0){
PTFD_PTFD2 = ~PTFD_PTFD2;
}
else if(PTGD_PTGD3==0){
PTFD_PTFD3 = ~PTFD_PTFD3;
}
KBI1SC_KBACK = 1; //清除键盘中断标志
}
/***************************************************
延时1毫秒函数
****************************************************/
void delayms(int ms)
{
int ii,jj;
if (ms<1) ms=1;
for(ii=0;ii<ms;ii++)
{
__RESET_WATCHDOG(); /* feeds the dog */
for(jj=0;jj<2000;jj++); // busclk 24MHz--1ms
}
}
/***************************************************
总线时钟配置函数
****************************************************/
void MCUInit(void)
{
SOPT1 = 0x13; // disable COP
MCGC2 = 0x36;
while(!(MCGSC & 0x02)); // wait for the OSC stable
MCGC1 = 0x98;
while((MCGSC & 0x1C ) != 0x08); // external clock is selected
MCGC3 = 0x48;
while ((MCGSC & 0x48) != 0x48); // wait for the PLL is locked
MCGC1 = 0x18;
while((MCGSC & 0x6C) != 0x6C);
}
/*********************主函数************************************/
void main(void)
{
MCUInit();
IO_INIT();
KB_INIT();
PTFD=0XFF;
EnableInterrupts; /* enable interrupts */
for(;;) {
//__RESET_WATCHDOG(); /* feeds the dog */
delayms(500);
PTFD_PTFD6 = ~PTFD_PTFD6;
PTFD_PTFD7 = ~PTFD_PTFD7;
} /* loop forever */
/* please make sure that you never leave main */
}
