#include "system.h" //包含基本的硬件描述信息
#include "altera_avalon_timer_regs.h" //定义内核寄存器的映射,提供对底层硬件的符号化访问
#include "altera_avalon_pio_regs.h" //包含基本的I/O口信息
#include "alt_types.h" //Altera定义的数据类型
#include "sys/alt_irq.h"
//#include "sys/alt_alarm.h" //系统时钟服务头文件
#include "unistd.h" //延时函数usleep
//定义端口
#define seg *(volatile unsigned char *) SM_SEG_BASE
#define bit *(volatile unsigned char *) SM_BIT_BASE
#define key *(volatile unsigned char *) KEY_BASE
//函数声明
void Timer_Init();
void Timer_ISR(void* context, alt_u32 id);
void display();
void KeyDown_ISR(void* context, alt_u32 id);
void KeyDown_Init();
//变量定义
alt_u8 duan[11]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xBF};
alt_u8 num[8] = {0,0,0,0,0,0,0,0};
alt_u8 second,min,hour;
//按键中断服务函数
void KeyDown_ISR(void* context, alt_u32 id)
{
if(key == 0x06) { second = 0; while(key == 0x06){display();} } //秒调设置
if(key == 0x05) { min++; if(min==60) min = 0; while(key == 0x05){display();} } //分调设置
if(key == 0x03) { hour++; if(hour == 24) hour = 0; while(key == 0x03){display();} }//小时设置
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY_BASE, 0x00); //清中断捕获寄存器
}
//按键中断初始化
void KeyDown_Init()
{
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(KEY_BASE, 0x07); //开KEY中断
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY_BASE, 0x00); //清中断捕获寄存器
alt_irq_register(KEY_IRQ,0,KeyDown_ISR); //注册中断函数
}
//定时器初始化
void Timer_Init()
{
alt_irq_register(TIMER_IRQ,0,Timer_ISR); //注册中断函数
IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_BASE, 0); //清状态标志
IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER_BASE, 7); //启动定时器允许中断,连续计数
}
//定时器中断服务函数
void Timer_ISR(void* context, alt_u32 id)
{
second++;
if(second == 60) { min++; second = 0;}
if(min == 60) { hour++; min = 0;}
if(hour == 24) { hour = 0;}
if(second % 2) duan[10] = 0xff;
else duan[10] = 0xbf; //秒闪烁
IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_BASE, 0); //清状态寄存器
}
//数码管显示函数
void display()
{
alt_u8 j;
num[0] = second % 10;
num[1] = second / 10;
num[2] = 10;
num[3] = min % 10;
num[4] = min / 10;
num[5] = 10;
num[6] = hour % 10;
num[7] = hour / 10;
for(j=0; j<8; j++)
{
bit = ~(0x01 << j); //位选低位有效
seg = duan[num[j]];
usleep(500);
}
}
//主函数
int main(void)
{
KeyDown_Init(); //按键中断初始化
Timer_Init(); //定时器初始化
while(1)
{
display(); //显示
}
return 0;
}
用户188034 2009-9-13 22:02