代码设计具体看源工程
/* * "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 ********************************/ // 数码管显示字符对应的 16 进制数 alt_u8 segtab[16] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e}; // 0-F unsigned char led_buffer[8]={0}; unsigned char bittab[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf}; static unsigned char cnt=4; /******************************** Functions ********************************/ void init_timer(void); /* *================================functions================================ Name: main Description: *========================================================================= */ int main(void) { unsigned char i=0,k=0; unsigned char buf[20]; int j=0; init_timer(); while(1){ sprintf(buf,"%06u",j++); for(i=0;i<6;i++){ led_buffer[i] = buf[5-i]-'0'; } usleep(500000); } return 0; } void ISR_handle_timer(void *context) { IOWR_ALTERA_AVALON_PIO_DATA(SEG_WEI_BASE, 0xff); IOWR_ALTERA_AVALON_PIO_DATA(SEG_WEI_BASE, bittab[cnt]); IOWR_ALTERA_AVALON_PIO_DATA(SEG_DUAN_BASE, segtab[led_buffer[cnt]]);// cnt++; if(cnt==4) cnt=0; IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_BASE, 0x00);// 清除中断标志寄存器 } void init_timer(void) { IOWR_ALTERA_AVALON_TIMER_STATUS( TIMER_BASE, 0x00 ); // 清除中断标志寄存器 IOWR_ALTERA_AVALON_TIMER_PERIODL( TIMER_BASE, 200000 ); // 设置定时周期 1s IOWR_ALTERA_AVALON_TIMER_PERIODH( TIMER_BASE, 200000>>16 ); IOWR_ALTERA_AVALON_TIMER_CONTROL( TIMER_BASE, 0x07 ); // 使能中断 alt_ic_isr_register( TIMER_IRQ_INTERRUPT_CONTROLLER_ID, TIMER_IRQ, ISR_handle_timer, NULL, 0x0 ); // 注册中断 } |