标签:
?
代码设计具体看源工程
?
/* * "Hello World" example. * * This example prints ‘Hello from Nios II‘ to the STDOUT stream. It runs on * the Nios II ‘standard‘, ‘full_featured‘, ‘fast‘, and ‘low_cost‘ example * designs. It runs with or without the MicroC/OS-II RTOS and requires a STDOUT * device in your system‘s hardware. * The memory footprint of this hosted application is ~69 kbytes by default * using the standard reference design. * * For a reduced footprint version of this template, and an explanation of how * to reduce the memory footprint for a given application, see the * "small_hello_world" template. * */ #include "system.h" #include "altera_avalon_pio_regs.h" #include "altera_avalon_timer_regs.h" #include "alt_types.h" #include "sys/alt_irq.h" #include <stdio.h> #include <unistd.h> #include <io.h> #include <string.h> /******************************** variables ********************************/ void init_timer(void); int i = 0, j = 0, flag; alt_u32 timer_prd[4] = {5000000, 10000000, 50000000, 100000000}; // 这四个是定时器的时钟数,定时时间=定时器的时钟数/定时器的时钟周期 // 该实验中系统时钟为 50MHz,则上述定时时间为{0.1s,0.2s,1s,2s} /* *================================functions================================ Name: main Description: *========================================================================= */ int main(void) { ????init_timer(); // 初始化定时器 ????while(1); ????return 0; } /* *================================functions================================ Name: ????????ISR_handle_time1 Description: *========================================================================= */ void ISR_handle_timer1(void *context) { ????// 控制 4 个流水灯闪烁 ????IOWR_ALTERA_AVALON_PIO_DATA( LED_PIO_BASE, 1<<i ); ????i++; ????if( 4 == i ) ????i = 0; ????// 清除中断标志寄存器 ????IOWR_ALTERA_AVALON_TIMER_STATUS( TIMER_0_BASE, 0x0 ); } /* *================================functions================================ Name: ????????????ISR_handle_timer2 Description: ????通过定时器 2 来改变定时器 1 的周期,改变后需要重启定时器 *========================================================================= */ void ISR_handle_timer2(void *context) { ????// 改变定时器 1 的周期 ????IOWR_ALTERA_AVALON_TIMER_PERIODL( TIMER_0_BASE, timer_prd[j] ); ????IOWR_ALTERA_AVALON_TIMER_PERIODH( TIMER_0_BASE, timer_prd[j]>>16 ); ????// 重启定时器 1 ????IOWR_ALTERA_AVALON_TIMER_CONTROL( TIMER_0_BASE, 0x07 ); ????// 闪烁频率先高后低然后又变高 ????if( 0 == j ) ????flag = 0; ????if( 3 == j ) ????flag = 1; ????if( 0 == flag ) ????j++; ????else ????j--; ????// 清除中断标志寄存器 ????IOWR_ALTERA_AVALON_TIMER_STATUS( TIMER_1_BASE, 0x0 ); } /* *================================functions================================ Name: ????????????init_timer Description: ????定时器初始化 *========================================================================= */ void init_timer(void) { ????// timer1 ????// 清除中断标志寄存器 ????IOWR_ALTERA_AVALON_TIMER_STATUS( TIMER_0_BASE, 0x00 ); ????// 设置定时周期 2s,输入的是时钟周期数 ????IOWR_ALTERA_AVALON_TIMER_PERIODL( TIMER_0_BASE, 10000000 ); ????IOWR_ALTERA_AVALON_TIMER_PERIODH( TIMER_0_BASE, 10000000>>16 ); ????IOWR_ALTERA_AVALON_TIMER_CONTROL( TIMER_0_BASE, 0x07 ); // 使能中断 ????alt_ic_isr_register( TIMER_0_IRQ_INTERRUPT_CONTROLLER_ID, ????TIMER_0_IRQ, ISR_handle_timer1, NULL, 0x0 ); // 注册中断 ????// timer2 ????// 清除中断标志寄存器 ????IOWR_ALTERA_AVALON_TIMER_STATUS( TIMER_1_BASE, 0x00 ); ????// 设置定时周期 10s,输入的是时钟周期数 ????IOWR_ALTERA_AVALON_TIMER_PERIODL( TIMER_1_BASE, 50000000 ); ????IOWR_ALTERA_AVALON_TIMER_PERIODH( TIMER_1_BASE, 50000000>>16 ); ????IOWR_ALTERA_AVALON_TIMER_CONTROL( TIMER_1_BASE, 0x07 ); // 使能中断 ????alt_ic_isr_register( TIMER_1_IRQ_INTERRUPT_CONTROLLER_ID, ????TIMER_1_IRQ, ISR_handle_timer2, NULL, 0x0 ); // 注册中断 } |
标签:
原文地址:http://www.cnblogs.com/logic3/p/5228900.html