Chinaunix首页 | 论坛 | 博客
  • 博客访问: 188198
  • 博文数量: 20
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 405
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-19 13:00
文章分类
文章存档

2011年(1)

2009年(18)

2008年(1)

我的朋友

分类:

2009-03-03 22:36:07

 
 

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

    volatile int* edge_capture_ptr = (volatile int*) context;
    /* Store the value in the Button's edge capture register in *context. */
    *edge_capture_ptr = IORD_ALTERA_AVALON_PIO_EDGE_CAP(PIO_BUTTON_BASE);
    /* Reset the Button's edge capture register. */
    IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PIO_BUTTON_BASE,0x00);
}
/* 按键IO初始化 */
static void init_button_pio()
{
    /* Recast the edge_capture pointer to match the alt_irq_register() function
     * prototype. */

    void* edge_capture_ptr = (void*) &edge_capture;
    /* Enable all 4 button interrupts. */
    IOWR_ALTERA_AVALON_PIO_IRQ_MASK(PIO_BUTTON_BASE, 0xff);
    /* Reset the edge capture register. */
    IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PIO_BUTTON_BASE, 0x00);
    /* Register the interrupt handler. */
    alt_irq_register(PIO_BUTTON_IRQ, edge_capture_ptr,
                      handle_button_interrupts );
}
#endif
void delay(int i)
{
  int j;
  for(j=0;j<i;j++);
}
int main (void) __attribute__ ((weak, alias ("alt_main")));
int alt_main(void)
{
 
    alt_irq_init(ALT_IRQ_BASE); //使能中断

    init_button_pio();
    while(1)
    {
        switch(edge_capture)
        {
        case 0x01:
            {
              led=led^0x01;
              IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);
              break;
            }
        case 0x02:
            {
              led=led~0x02;
              IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);
              break;
            }
        case 0x04:
            {
              led=led^0x04;
              IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);
              break;
            }
        case 0x08:
            {
              led=led^0x08;
              IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);
              break;
            }
        case 0x10:
            {
              led=led^0x10;
              IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);
              break;
            }
        case 0x20:
            {
              led=led^0x20;
              IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);
              break;
            }
        case 0x40:
            {
              led=led^0x40;
              IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);
              break;
            }
        case 0x80:
            {
              led=led^0x80;
              IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,led);
              break;
            }
        default:
            {
              IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE,0x00);
              edge_capture=0;
              break;
              
            }
        }
    }
    return 0;
}

   注意,在添加按键PIO时一定要使能中断!研究过一段时间的Nios II,感觉还是很有收获,深刻体会到了SOPC技术的灵活性,但目前速度方面是其最大的缺点,相信Nios II会慢慢流行起来....

阅读(3641) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-04-27 10:02:18

按键IO初始化 ?那在SOPC配置按键IO的时候不是已经设置了嘛?