#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>
//数码管显示字符对应的16进制数
alt_u8 segtab[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
static alt_u8 data[8]={0,0,'-',0,0,'-',0,0};
static alt_u16 bit_sel_data=0;
alt_u16 hour=0;
alt_u16 minute=0;
alt_u16 second=0;
void ISR_handle_timer1(void *context)
{
while(1)
{
second++;
if(second>=60)
{
second=0;
minute++;
}
if(minute>=60)
{
minute=0;
hour++;
}
if(hour>=24)
{
hour=0;
}
data[0]=(alt_u8)(hour/10);
data[1]=(alt_u8)(hour%10);
data[2]='-';
data[3]=(alt_u8)(minute/10);
data[4]=(alt_u8)(minute%10);
data[5]='-';
data[6]=(alt_u8)(second/10);
data[7]=(alt_u8)second%10);
int i;
for(i=0;i<8;i++)
{
IOWR_ALTERA_AVALON_PIO_DATA(SEG01_DAT_BASE,segtab[data[i]]);
//IOWR_ALTERA_AVALON_PIO_DATA(SEG01_DAT_BASE,(bit_sel_data<<13)|(segtab[data[bit_sel_data]]<<5));
usleep(2000);
}
}
//清除中断标志寄存器
IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER1_BASE,0x00);
}
void ISR_handle_timer2(void *context)
{
while(1)
{
int i;
for(i=0;i<8;i++)
{
bit_sel_data=(bit_sel_data+1)&7;
IOWR_ALTERA_AVALON_PIO_DATA(SEG_DAT_BASE,bit_sel_data);
usleep(5000);
}
}
//清除中断标志寄存器
IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER2_BASE,0x00);
}
void init_timer(void)
{
//timer1
////清除中断标志寄存器
IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER1_BASE,0x00);//设置定时器1s,输入的是时钟周期数
IOWR_ALTERA_AVALON_TIMER_PERIODL(TIMER1_BASE,50000000);
IOWR_ALTERA_AVALON_TIMER_PERIODH(TIMER1_BASE,50000000>>16);
IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER1_BASE,0x07);//使能中断
alt_ic_isr_register(TIMER1_IRQ_INTERRUPT_CONTROLLER_ID, TIMER1_IRQ,ISR_handle_timer1,NULL,0x0);//注册中断
//timer2
////清除中断标志寄存器
IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER2_BASE,0x00);//设置定时器0.1s,输入的是时钟周期数
IOWR_ALTERA_AVALON_TIMER_PERIODL(TIMER2_BASE,5000000);
IOWR_ALTERA_AVALON_TIMER_PERIODH(TIMER2_BASE,5000000>>16);
IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER2_BASE,0x07);//使能中断
alt_ic_isr_register(TIMER2_IRQ_INTERRUPT_CONTROLLER_ID, TIMER2_IRQ,ISR_handle_timer2,NULL,0x0);//注册中断
}
int main(void)
{
init_timer(); //初始化定时器
while(1);
return 0;
}