《Nios II按键中断调试程序及经验.docx》由会员分享,可在线阅读,更多相关《Nios II按键中断调试程序及经验.docx(3页珍藏版)》请在三一办公上搜索。
1、Nios II按键中断调试程序及经验软件:Quatus II 6.1,Nios II 6.1 硬件:EP1C3T144 FPGA开发板 实验目的:熟悉nios II开发,掌握中断程序调试 系统配置:CPU+OnChipRam(4K)+LED_PIO+BUTTON_PIO 功能描述:按键实现中断,使led亮或灭 led配置成开发板上的led0,led1,led2,led3 按键采用简单按钮B17,B18,19,B20 不给过多注释了(其实注释够多了) #include #include #include sys/alt_irq.h #include altera_avalon_pio_regs.
2、h #include system.h /* A variable to hold the value of the button pio edge capture register. */ volatile int edge_capture; #ifdef BUTTON_PIO_BASE /* 按键中断服务程序*/ static void handle_button_interrupts(void* context, alt_u32 id) /* Cast context to edge_captures type. It is important that this be * declar
3、ed volatile to avoid unwanted compiler optimization. */ volatile int* edge_capture_ptr = (volatile int*) context; /* Store the value in the Buttons edge capture register in *context. */ *edge_capture_ptr = IORD_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE); /* Reset the Buttons edge capture register.
4、*/ IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE, 0); /* Initialize the button_pio. */ 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. */从硬件角度,允许
5、硬件中断 IOWR_ALTERA_AVALON_PIO_IRQ_MASK(BUTTON_PIO_BASE, 0xf); /* Reset the edge capture register. */ IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE, 0x0); /* Register the interrupt handler. */ 注册后开中断 alt_irq_register( BUTTON_PIO_IRQ, edge_capture_ptr, handle_button_interrupts ); #endif int main (void
6、) _attribute_ (weak, alias (alt_main); int alt_main(void) alt_irq_init(ALT_IRQ_BASE); /necessary,enable interrupts alt_sys_init; /unnecessary alt_io_redirect(ALT_STDOUT,ALT_STDIN,ALT_STDERR);/unnecessary init_button_pio; while(1) switch(edge_capture) case 0x01: IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BA
7、SE,0xFF); break; case 0x02: IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,0x00); break; case 0x04: IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,0xFF); break; case 0x08: IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,0x00); break; default: IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,0x0A); break; return 0; 1 因为使用的是片上RAM,容量有限,故得重载alt_main, 否则空间不够 2 系统默认的alt_main是干了很多活的.比如系统中断,IO设备驱动等,改写的时候得注意,哪些用户程序用到了.比如本例用到中断,故 alt_irq_init(ALT_IRQ_BASE);函数是必须的.开始没有调试成功就是因为没有开总中断 中断的调试过程可以依次看以下: status:最低位PIE,为禁止,为允许中断 ienable:32位,每位对应一个外部中断,为则对应使能 ipending:表示处理器正在处理的中断 全局变量:alt_irq.若注册成功的话就会向向量表写入和Context.注册完后返回则成功,为负则失败
链接地址:https://www.31ppt.com/p-3061919.html