【连载】【FPGA黑金开发板】NIOSII那些事儿定时器应用实例(十八)

声明:本文为原创作品,版权归本博文作者所有,如需转载,请注明出处http://www.cnblogs.com/kingst/

  7

      最近写了个小程序,动态数码管扫描,算是定时器的应用吧,单片机上经常看得到,我移植过来了,充实一下内容吧。

      首先在SOPC中添加一个定时器,两个8位的PIO,一个用于数据线,一个是作为开关,作为开关的可以根据数码管个数决定PIO的数量。

clip_image002

      编译完以后就可以写程序了,程序如下所示,利用的是动态余辉效果

/*
 * ==============================================================
 *
 *       Filename:  main.c
 *
 *    Description:  
 *
 *        Version:  1.0.0
 *        Created:  2010.6.17
 *       Revision:  none
 *       Compiler:  Nios II 9.0 IDE
 *
 *         Author:  马瑞 (AVIC)
 *          Email:  avic633@gmail.com  
 *
 * ==============================================================
 */

#include <stdio.h>
#include <sys/unistd.h>
#include <io.h>
#include <string.h>

#include "system.h"
#include "altera_avalon_pio_regs.h"
#include "altera_avalon_timer_regs.h"
#include "alt_types.h"
#include "sys/alt_irq.h"


unsigned char segtab[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};  //0~9
unsigned char led_buffer[8]={0};
unsigned char bittab[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
static unsigned char cnt=0;  


static void timer_init(void);   

/* 
 * ===  FUNCTION  ===================================================
 *         Name:  main
 *  Description:  
 * ====================================================================
 */
int main(void)
{ 
    unsigned char i=0,j=0;
    unsigned char buf[20];

    timer_init();   

    while(1){
        sprintf(buf,"%08u",j++);

        for(i=0;i<8;i++){
            led_buffer[i] = buf[7-i]-'0';
        }
        usleep(500000);
    }

    return 0;
}
/* 
 * ===  FUNCTION  ===================================================
 *         Name:  timer_irq
 *  Description:  定时器中断函数 
 * =================================================================
 */
static void timer_irq(void *context, alt_u32 id)
{
    IOWR_ALTERA_AVALON_PIO_DATA(SEG_SEL_BASE, 0xff);
    IOWR_ALTERA_AVALON_PIO_DATA(SEG_SEL_BASE, bittab[cnt]);

    IOWR_ALTERA_AVALON_PIO_DATA(SEG_DAT_BASE, segtab[led_buffer[cnt]]); 

    cnt++;
    
    if(cnt==8)
        cnt=0;   
    
    IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_BASE, 0);
}

/* 
 * ===  FUNCTION  ===================================================
 *         Name:  timer_init
 *  Description:  定时器初始化 
 * =================================================================
 */
static void timer_init(void)   
{
    IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_BASE, 0);

    IOWR_ALTERA_AVALON_TIMER_PERIODL(TIMER_BASE,200000);
    IOWR_ALTERA_AVALON_TIMER_PERIODH(TIMER_BASE,200000 >> 16);
  
    IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER_BASE, 0x07);

 
    alt_irq_register(TIMER_IRQ, NULL, timer_irq); 
}

内容很简单,也不详细叙述了,说多了大家也嫌烦了,呵呵!

原文地址:https://www.cnblogs.com/kingst/p/1759719.html