Nios II中简单的按键中断程序

1#include <stdlib.h>
  2#include <io.h>
  3#include "sys/alt_irq.h"
  4#include "altera_avalon_pio_regs.h"
  5#include "system.h"
  6#ifdef PIO_BUTTON_BASE
  7/* 按键中断服务程序*/
  8static void handle_button_interrupts(void* context, alt_u32 id)
  9{
 10    /* Cast context to edge_capture's type. It is important that this be
 11     * declared volatile to avoid unwanted compiler optimization.
 12     */

 13    volatile int* edge_capture_ptr = (volatile int*) context;
 14    /* Store the value in the Button's edge capture register in *context. */
 15    *edge_capture_ptr = IORD_ALTERA_AVALON_PIO_EDGE_CAP(PIO_BUTTON_BASE);
 16    /* Reset the Button's edge capture register. */
 17    IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PIO_BUTTON_BASE,0x00);
 18}

 19/* 按键IO初始化 */
 20static void init_button_pio()
 21{
 22    /* Recast the edge_capture pointer to match the alt_irq_register() function
 23     * prototype. */

 24    void* edge_capture_ptr = (void*&edge_capture;
 25    /* Enable all 4 button interrupts. */
 26    IOWR_ALTERA_AVALON_PIO_IRQ_MASK(PIO_BUTTON_BASE, 0xff);
 27    /* Reset the edge capture register. */
 28    IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PIO_BUTTON_BASE, 0x00);
 29    /* Register the interrupt handler. */
 30    alt_irq_register(PIO_BUTTON_IRQ, edge_capture_ptr,
 31                      handle_button_interrupts );
 32}

 33#endif
 34void delay(int i)
 35{
 36  int j;
 37  for(j=0;j<i;j++);
 38}
 
 39int main (void) __attribute__ ((weak, alias ("alt_main")));
 40int alt_main(void)
 41{
 42 
 43    alt_irq_init(ALT_IRQ_BASE); //使能中断
 44
 45    init_button_pio();
 46    while(1)
 47    {
 48        switch(edge_capture)
 49        {
 50        case 0x01:
 51            {
 52              led=led^0x01;
 53              IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);
 54              break;
 55            }

 56        case 0x02:
 57            {
 58              led=led~0x02;
 59              IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);
 60              break;
 61            }

 62        case 0x04:
 63            {
 64              led=led^0x04;
 65              IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);
 66              break;
 67            }

 68        case 0x08:
 69            {
 70              led=led^0x08;
 71              IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);
 72              break;
 73            }

 74        case 0x10:
 75            {
 76              led=led^0x10;
 77              IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);
 78              break;
 79            }

 80        case 0x20:
 81            {
 82              led=led^0x20;
 83              IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);
 84              break;
 85            }

 86        case 0x40:
 87            {
 88              led=led^0x40;
 89              IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);
 90              break;
 91            }

 92        case 0x80:
 93            {
 94              led=led^0x80;
 95              IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);
 96              break;
 97            }

 98        default:
 99            {
100              IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,0x00);
101              edge_capture=0;
102              break;
103              
104            }

105        }

106    }

107    return 0;
108}

 

注意,在添加按键PIO时一定要使能中断!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值