Timer_ISR_Init()

#in #in #in

Timer_ISR_Init()
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&#91;&#93;={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40}; //0~F
alt_u8 PrintDispTab&#91;&#93;={ &#039;0&#039;, &#039;1&#039;, &#039;2&#039;, &#039;3&#039;, &#039;4&#039;, &#039;5&#039;, &#039;6&#039;, &#039;7&#039;,
&#039;8&#039;, &#039;9&#039;, &#039;A&#039;, &#039;B&#039;, &#039;C&#039;, &#039;D&#039;, &#039;E&#039;, &#039;F&#039;}; //0~F
alt_u8 KeyTab&#91;&#93;={ 0x4e,0x67,0x47,0x27,
0x6b,0x4b,0x2b,0x6d,
0x4d,0x2d,0x07,0x0b,
0x0d,0x0e,0x6e,0x2e };
alt_u8 KB_Scan_Tab&#91;8&#93; = {0x0e, 0xff, 0x0d, 0xff, 0x0b, 0xff, 0x07, 0xff};
alt_u8 DispBuff&#91;8&#93; = {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&#91;DispBuff&#91;DisplayNum&#93;&#93;);
//判斷有無按鍵按下
IOWR_ALTERA_AVALON_PIO_DATA(KB_COL_BASE, KB_Scan_Tab&#91;DisplayNum&#93;);
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&#91;i&#93;)
break;
}
if(i < 16)
{
for(j=0; j<7; j++)
DispBuff&#91;j&#93; = DispBuff&#91;j+1&#93;;
DispBuff&#91;j&#93; = i;
printf("You pressed &#039;%c&#039; key!\n", PrintDispTab&#91;i&#93;);
}
}
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);
}

相關詞條

熱門詞條

聯絡我們