📄 d0aaba53458f001c180b8a3c09c33bce
字号:
#include <stdio.h>
#include "system.h"
#include "altera_avalon_pio_regs.h"
#include "alt_types.h"
#include "sys/alt_irq.h"
#include "priv/alt_busy_sleep.h"
#define LEDCON 0xff //
#define KEYCON 0xff //
alt_u32 done = 0; // 信号量:通知外部中断事件发生
/********************************************************************
* 功 能:键按下事件中断服务子程序,当键按下时,通过done标志
* 告知外界
********************************************************************/
static void KeyDown_interrupts(void* context, alt_u32 id)
{
/* 清中断捕获寄存器 */
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE, 0);
/* 通知外部有中断事件发生 */
done++;
}
/********************************************************************
* 功 能:初始化LED_PIO为输出,KEY为输入,开中断,清边沿捕获寄存器
********************************************************************/
void InitPIO(void)
{
/* 初始化LED_PIO为输出,KEY为输入 */
IOWR_ALTERA_AVALON_PIO_DIRECTION(LED_PIO_BASE, LEDCON);
IOWR_ALTERA_AVALON_PIO_DIRECTION(BUTTON_PIO_BASE, 0x00);
/* 开KEY的中断 */
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(BUTTON_PIO_BASE, KEYCON);
/* 清边沿捕获寄存器 */
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE, 0x00);
/* 注册中断服务子程序 */
alt_irq_register(BUTTON_PIO_IRQ, NULL, KeyDown_interrupts);
}
/********************************************************************
* 功 能:等待按键中断,并输出控制相应的LED。
********************************************************************/
int main(void)
{
volatile alt_u32 key_state,old_state,new_state;
old_state = 0xff;
alt_u8 led = 0x2;
alt_u8 dir = 0;
volatile int i;
IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, old_state);//初始化LED全灭
InitPIO();
while(1)
{
if (led & 0x81)
{
dir = (dir ^ 0x1);
}
if (dir)
{
led = led >> 1;
}
else
{
led = led << 1;
}
IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, led);
/*
* The delay element in this design has been written as a while loop
* to avoid confusing the software debugger. A tight, one line software
* delay loop such as:
* for(i=0; i<200000; i++);
* can cause problems when it is stepped through using a software debugger.
* The while loop below produces the same behavior as the for loop shown
* above, but without causing potential debugger problems.
*/
i = 0;
while (i<200000)
i++;
if(0 != done)
{
/* 中断事件数量减1 */
done--;
alt_busy_sleep(5000); //延时5ms
key_state = IORD_ALTERA_AVALON_PIO_DATA(BUTTON_PIO_BASE)&KEYCON;
if(key_state == 0xff) //如果是由短暂脉冲引起的中断则忽略
continue;
new_state = ~(old_state ^ key_state); // 按键按下时对应的LED取反
old_state = new_state; // 保存LED的状态
IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, new_state);
usleep(200000);
}
}
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -