全部博文(92)
分类: 嵌入式
2010-03-24 17:18:36
INTERRUPTS AND KEY
S3C2440A具有32个中断源其中24个外部中断
分别连接至:
GPG0--GPG15:外部中断EINT8—EINT23
GPF0—GPF7:外部中断EINT0—EINT7
支持上升沿、下降沿、高电平、低电平、双沿触发
对于中断服务程序的写法,一般如下:
1. 设置触发方式:上升沿、下降沿、高电平、低电平、双沿触发
2. 清中断源挂起寄存器、中断服务寄存器(可选、防止原有中断干扰)
3. 设置中断模式(IRQ或FIQ可选,默认为IRQ)
4. 设置中断优先级(可选,一般默认即可)
5. 打开外部中断屏蔽
6. 设置中断服务入口程序
进入中断:
1、清中断源挂起寄存器
2、清中断服务寄存器
防止反复进入中断
3、执行中断服务程序
通过查资料可知key0~key3 -> EINT4~EINT7 -> GPF4~GPF7
Led0~led3-> EINT0~EINT3 -> GPF0~GPF3
我选用通过外部中断来响应按键(按键对应的LED灯会亮):
(IRQ)SUBSRCPND->SUBMASK->SRCPND->MASK->PRIORITY->INTPND->PROCEDURE
There are 32 interrupts, 15 sub_interrupts and 24 external interrupts
(INTPND->EINT4_7; INTMSK->EINT4_7
EINTPND->EINT4,EINT5,EINT6,EINT7;
EINTMASK->EINT4,EINT5,EINT6,EINT7)
1. Setting GPFCON register to select the corresponding pin function;
2. Setting GPFUP register to pull up the GPF4~GPF7, for later setting Falling Edge Trigger;
3. Setting GPFDATA register to set the initial value;
4. Setting EXTINT0 register to set the signaling method of EINT4~EINT7 into Falling edge trigger;
5. Setting EINTMASK register not to be masked(set 0) in order to let the corresponding bit of SRCPND or EINTPEND set;
6. Setting EINTPEND register to clear the EINT4,EINT5,EINT6,EINT7
rGPFCON = (rGPFCON|0xFFFF)&0xFFFFaa55; //GPF4-7设置为EINT4-7(中断响应),GPF0-3为输出
rGPFUP &= 0xFF00; //打开上拉功能()
rGPFDAT |= 0xF;
rEXTINT0 &= ~(7<<16 | 7<<20 | 7<<24 | 7<<28);
rEXTINT0 |= (2<<16 | 2<<20 | 2<<24 | 2<<28) ; //设置外部中断4_7下降沿触发
rEINTMASK &= ~(1<<4|1<<5|1<<6|1<<7); //enable eint 4
rEINTPEND |= (1<<4|1<<5|1<<6|1<<7); //clear eint
重点说明: SRCPND(Source Pending):中断源挂起寄存器
INTPND(Interrupt Pending):中断服务寄存器
当某个中断来临时,SRCPND对于该中断的相应位会被置1,SRCPND可以有多位被置1,但INTPND只能有一位被置1,说明中断只能一个一个被处理,值得关注的是当我们去清中断源挂起寄存器和清中断服务寄存器事操作时操作是这样的:通过对相应位置1 来清除相应位。如果你对相应位写0,则该位的数值保持不变。
void __irq EintHandler(void); //主函数外要申请中断函数
BIT_EINT4_7 EQU (0x1<<4)
中断寄存器要做如下设定:
ClearPending(BIT_EINT4_7);
__inline void ClearPending(int bit)
{
register i;
rSRCPND = bit;(对相应位置1,是进行清除操作,而不是设置为1)
rINTPND = bit; (对相应位置1,是进行清除操作,而不是设置为1)
i = rINTPND;
}
pISR_EINT4_7=(unsigned)EintHandler; //外部中断4_7中断服务子程序入口地址
EnableIrq(BIT_EINT4_7);
#define EnableIrq(bit) rINTMSK &= ~(bit)
中断服务程序(ISR)
void __irq EintHandler(void)
{
if(rINTPND==BIT_EINT4_7)
{
ClearPending(BIT_EINT4_7);
if(rEINTPEND&(1<<4))
{
Uart_Printf("eint 4\n");
rGPFDAT = LED1ON;
Delay(500);
rGPFDAT = LEDOFF;
rEINTPEND |= 1<< 4;
}
if(rEINTPEND&(1<<5))
{
Uart_Printf("eint 5\n");
rGPFDAT = LED2ON;
Delay(500);
rGPFDAT = LEDOFF;
rEINTPEND |= 1<< 5;
}
if(rEINTPEND&(1<<6))
{
Uart_Printf("eint 6\n");
rGPFDAT = LED3ON;
Delay(500);
rGPFDAT = LEDOFF;
rEINTPEND |= 1<< 6;
}
if(rEINTPEND&(1<<7))
{
Uart_Printf("eint 7\n");
rGPFDAT = LED4ON;
Delay(500);
rGPFDAT = LEDOFF;
rEINTPEND |= 1<< 7;
}
}
}
更详细的中断知识: