
SOPC课程报告
——跑马灯实验
一、选题目标
1. 进一步了解简单设备及其编程
2. 熟悉相关I/O操作函数
3. 了解中断原理及中断函数编写方法
二、选题内容
使用2个按键作为输入,8位LED灯作为输出。
按下1号键LED灯自左到右依次灭;
按下2号键LED灯自右到左依次灭;
三、原理与步骤
1. 在SOPC中加入各元件并设置好各地址。
2. 加入引脚锁定文件,绘制顶层原理图。
#Setup.tcl
#Setup pin setting
set_global_assignment -name RESERVE_ALL_UNUSED_PINS "AS INPUT TRI-STATED"
set_global_assignment -name ENABLE_INIT_DONE_OUTPUT OFF
set_location_assignment PIN_B13 -to clk
set_location_assignment PIN_C5 -to reset_n
set_location_assignment PIN_AC10 -to out_port_from_the_led_pio[0]
set_location_assignment PIN_W11 -to out_port_from_the_led_pio[1]
set_location_assignment PIN_W12 -to out_port_from_the_led_pio[2]
set_location_assignment PIN_AE8 -to out_port_from_the_led_pio[3]
set_location_assignment PIN_AF8 -to out_port_from_the_led_pio[4]
set_location_assignment PIN_AE7 -to out_port_from_the_led_pio[5]
set_location_assignment PIN_AF7 -to out_port_from_the_led_pio[6]
set_location_assignment PIN_AA11 -to out_port_from_the_led_pio[7]
set_location_assignment PIN_Y11 -to in_port_to_the_key_pio[0]
set_location_assignment PIN_AA10 -to in_port_to_the_key_pio[1]
3. 在NIOS II IDE下,根据刚生成的硬件建立一个空工程文件。
4. 然后编写软件代码如下:
#include "alt_types.h"
#include #include "system.h" #include "sys/alt_irq.h" #include "altera_avalon_pio_regs.h" #include #include volatile int edge_capture; alt_u8 flag; static void handle_key_interrupt(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. */ /* 将edge_capture与*context对应起来 */ /* 设置为volatile 是为了避免下面的指令被编译器给优化掉 */ volatile int* edge_capture_ptr = (volatile int*)context; /* Store the value in the Button's edge capture register in *context. */ /* 读取按键中断边沿捕捉寄存器中的值 -> *context (这里就等于*edge_capture_ptr) */ *edge_capture_ptr = IORD_ALTERA_AVALON_PIO_EDGE_CAP(KEY_PIO_BASE); /* Reset the Button's edge capture register. */ /* 清0 边沿捕捉寄存器中的值 */ IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY_PIO_BASE, 0); flag = 1; //中断产生标志置1 } static void init_key_pio() { /* Recast the edge_capture pointer to match the alt_irq_register() function prototype. */ /* 准备将寄存器 edge_capture作为中断注册函数alt_irq_register的一个输入参数*/ void* edge_capture_ptr = (void*)&edge_capture; /* Enable all 2 button interrupts. */ /* 使能2个按键的中断 */ IOWR_ALTERA_AVALON_PIO_IRQ_MASK(KEY_PIO_BASE, 0xf); /* Reset the edge capture register. */ /* 清0 边沿捕捉寄存器中的值 */ IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY_PIO_BASE, 0x0); /* Register the interrupt handler. */ /* 对中断函数进行注册 */ alt_irq_register(KEY_PIO_IRQ,edge_capture_ptr, handle_key_interrupt); } int main (void) __attribute__ ((weak, alias ("alt_main"))); int alt_main(void) { alt_u8 led = 0x0; volatile int i; flag = 0; alt_u8 ledtype =1; alt_irq_init(ALT_IRQ_BASE); //使能中断 (很重要,相对于总的中断开关) init_key_pio(); while(1) { if(ledtype==1){ led = led << 1; if(led == 0x00) led = 0x01; } else if(ledtype==2){ led = led >> 1; if(led == 0x00) led = 0x80; } IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, led); i = 0; if(edge_capture!=0){ switch(edge_capture) { case 0x01: ledtype = 1; break; case 0x02: ledtype = 2; break; default: ledtype = 1; } } while (i<200000) i++; } } 四、结束语 利用SOPC技术,将整个DDS模块和处理器模块集成到一片FPGA内部,利用可编程逻辑器件的灵活性和NIOS的强大处理能力我们很方便的实现了此次课程设计,在设计过程中,碰到诸多问题,在此过程中,上网找了很多东西,也学到了很多东西。 感谢老师一个学期来的指点!
