【FPGA黑金开发板】NIOSII那些事儿--定时器应用实例(十八)声明:本文转载于http://www.cnblogs.com/kingst,版权归黑金动力社区(http://www.heijin.org)所有。
最近写了个小程序,动态数码管扫描,算是定时器的应用吧,单片机上经常看得到,我移植过来了,充实一下内容吧。
首先在SOPC中添加一个定时器,两个8位的PIO,一个用于数据线,一个是作为开关,作为开关的可以根据数码管个数决定PIO的数量。
编译完以后就可以写程序了,程序如下所示,利用的是动态余辉效果
002 | * ============================================================== |
011 | * Compiler: Nios II 9.0 IDE |
014 | * Email: avic633@gmail.com |
016 | * ============================================================== |
025 | #include "altera_avalon_pio_regs.h" |
026 | #include "altera_avalon_timer_regs.h" |
027 | #include "alt_types.h" |
028 | #include "sys/alt_irq.h" |
031 | unsigned char segtab[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //0~9 |
032 | unsigned char led_buffer[8]={0}; |
033 | unsigned char bittab[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; |
034 | static unsigned char cnt=0; |
037 | static void timer_init(void); |
040 | * === FUNCTION =================================================== |
043 | * ==================================================================== |
047 | unsigned char i=0,j=0; |
048 | unsigned char buf[20]; |
053 | sprintf(buf,"%08u",j++); |
056 | led_buffer[i] = buf[7-i]-'0'; |
064 | * === FUNCTION =================================================== |
066 | * Description: 定时器中断函数 |
067 | * ================================================================= |
069 | static void timer_irq(void *context, alt_u32 id) |
071 | IOWR_ALTERA_AVALON_PIO_DATA(SEG_SEL_BASE, 0xff); |
072 | IOWR_ALTERA_AVALON_PIO_DATA(SEG_SEL_BASE, bittab[cnt]); |
074 | IOWR_ALTERA_AVALON_PIO_DATA(SEG_DAT_BASE, segtab[led_buffer[cnt]]); |
081 | IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_BASE, 0); |
085 | * === FUNCTION =================================================== |
087 | * Description: 定时器初始化 |
088 | * ================================================================= |
090 | static void timer_init(void) |
092 | IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_BASE, 0); |
094 | IOWR_ALTERA_AVALON_TIMER_PERIODL(TIMER_BASE,200000); |
095 | IOWR_ALTERA_AVALON_TIMER_PERIODH(TIMER_BASE,200000 >> 16); |
097 | IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER_BASE, 0x07); |
100 | alt_irq_register(TIMER_IRQ, NULL, timer_irq); |
内容很简单,也不详细叙述了,说多了大家也嫌烦了,呵呵!
阅读(816) | 评论(0) | 转发(0) |