Altera公司nios II 處理器的HAL系統里 初始化中斷
看下面的例子:
#include <stdio.h>
#include <sys/unistd.h>
#include <io.h>
#include <string.h>
#include "system.h"
#include "altera_avalon_pio_regs.h"
#include "altera_avalon_timer_regs.h"
#include "alt_types.h"
#include "sys/alt_irq.h"
#define TRUE 1
#define FALSE 0
/***********************************************/
static void Timer_ISR_Init(void); //初始化中斷
/***********************************************/
int main(void)
{
printf("\n Exp4 - Keyboard & 7Seg LED!\n");
//設定Timer中斷
Timer_ISR_Init();
while(1)
{
usleep(10000);
}
return 0;
}
/***********************************************/
//Timer
alt_u8 DispSegTab[]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40}; //0~F
alt_u8 PrintDispTab[]={ '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; //0~F
alt_u8 KeyTab[]={ 0x4e,0x67,0x47,0x27,
0x6b,0x4b,0x2b,0x6d,
0x4d,0x2d,0x07,0x0b,
0x0d,0x0e,0x6e,0x2e };
alt_u8 KB_Scan_Tab[8] = {0x0e, 0xff, 0x0d, 0xff, 0x0b, 0xff, 0x07, 0xff};
alt_u8 DispBuff[8] = {16,16,16,16,16,16,16,16};
alt_u8 KeyValue;
alt_u8 DisplayNum, pressedKeyNum = 0, KeyPressedFlag;
static void Timer_Irq_Handler(void *context, alt_u32 id)
{
alt_u8 i,j;
DisplayNum = (DisplayNum + 1) % 8;
//輸出顯示掃描信號
IOWR_ALTERA_AVALON_PIO_DATA(SEG_SEL_BASE, DisplayNum);
IOWR_ALTERA_AVALON_PIO_DATA(SEG_A_DP_BASE, DispSegTab[DispBuff[DisplayNum]]);
//判斷有無按鍵按下
IOWR_ALTERA_AVALON_PIO_DATA(KB_COL_BASE, KB_Scan_Tab[DisplayNum]);
KeyValue = IORD_ALTERA_AVALON_PIO_DATA(KB_ROW_BASE) & 0x0f;
if(KeyValue != 0x0f && !KeyPressedFlag)
{
KeyPressedFlag = TRUE;
PressedKeyNum = DisplayNum;
//轉換鍵值
KeyValue |= DisplayNum << 4;
for(i=0; i<16; i++)
{
if(KeyValue == KeyTab[i])
break;
}
if(i < 16)
{
for(j=0; j<7; j++)
DispBuff[j] = DispBuff[j+1];
DispBuff[j] = i;
printf("You pressed '%c' key!\n", PrintDispTab[i]);
}
}
else if(PressedKeyNum == DisplayNum && KeyValue == 0x0f && KeyPressedFlag)
KeyPressedFlag = FALSE;
//清除Timer中斷標誌暫存器
IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_BASE, 0);
}
/************************************************/
static void Timer_ISR_Init(void) //初始化中斷
{
//清除Timer中斷標誌暫存器
IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_BASE, 0);
//註冊Timer中斷
alt_irq_register(TIMER_IRQ, NULL, Timer_Irq_Handler);
//允許Timer中斷
IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER_BASE, 0x01);
}