/////////////////////////////////////////////////////////////////////////////////////////////////
date.h
/////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef DATE_H_
#define DATE_H_
#include "io.h"
#include "GUI.h"
#include "system.h"
#include "alt_types.h"
#include "sys/alt_irq.h"
#include "altera_avalon_pio_regs.h" //包含基本的I/O口信息
#include "altera_avalon_timer_regs.h" //定义内核寄存器的映射,提供对底层硬件的符号化访问
#define key IORD_ALTERA_AVALON_PIO_DATA(KEY_BASE)
#define sw IORD_ALTERA_AVALON_PIO_DATA(SWITCH_BASE)
#define SECOND 0
#define MIN 0
#define HOUR 12
#define DAY 1
#define MONTH 1
#define YEAR 2011
#define WEEKDAY 0
#define Date_Disp() display()
void init_value(void);
void Timer_Init();
void Timer_ISR(void* context, alt_u32 id);
void display();
void KeyDown_ISR(void* context, alt_u32 id);
void KeyDown_Init();
void Date_Init(void);
#endif /*DATE_H_*/
///////////////////////////////////////////////////////////////////////////////////////////////
date.c
///////////////////////////////////////////////////////////////////////////////////////////////
#include"date.h"
alt_u8 second,min,hour,day,month,weekday;
alt_u16 year;
char *week[]={"SUN","MON","TUE","WED","THU","FRI","SAT"}; //星期
void init_value(void)
{
second=SECOND;
min=MIN;
hour=HOUR;
day=DAY;
month=MONTH;
year=YEAR;
weekday=WEEKDAY;
}
//按键中断服务函数
void KeyDown_ISR(void* context, alt_u32 id)
{
if(sw==0x01)
{
if(key == 0x0e) { second = 0; while(key == 0x0e){display();} } //秒调设置
if(key == 0x0d) { min++; if(min==60) min = 0; while(key == 0x0d){display();} } //分调设置
if(key == 0x0b) { hour++; if(hour == 24) hour = 0; while(key == 0x0b){display();} }//小时设置
}
else if(sw==0x02)
{
if(key == 0x0e) { weekday++; if(weekday == 7) weekday = 0; while(key == 0x0e){display();} }//星期设置
if(key == 0x0d) //日期设置
{
day++;
if( ( month<=7 && month%2 ) || ( month>=8 && (!(month%2)) ) ) //大月31天
{
if(day==32) day=1;
}
else //小月30
{
if(month==2) //2月 是28天还是29天
{
if( (!(year%4) && year%100) || (!(year%400) ) )// 闰年 29天
{ if(day==30) day=1; }
else if(day==29) day=1; //平年 28天
}
else if(day==31) day=1;
}
while(key == 0x0d) display();
}
if(key == 0x0b) { month++; if(month == 13) month = 1; while(key == 0x0b){display();} }//月份设置
}
else if(sw==0x03)
{
if(key == 0x0e) { year--; if(year == 1900) year = 2010; while(key == 0x0e){display();} }//年份设置,递减
if(key == 0x0d) { year++; if(year == 2100) year = 2010; while(key == 0x0d){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) { day++;weekday++;hour = 0;}
if(weekday == 7) weekday = 0;
if( ( month<=7 && month%2 ) || ( month>=8 && (!(month%2)) ) )
{
if(day==32)
{
day=1;
month++;
}
}
else
{
if(month==2)
{
if( (!(year%4) && year%100) || (!(year%400) ) )
{
if(day==30)
{
day=1;
month++;
}
}
else if(day==29)
{
day=1;
month++;
}
}
else if(day==31)
{
day=1;
month++;
}
}
if(month==13) { month = 1; year ++; }
if(year==2100) year = 2010;
IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_BASE, 0); //清状态寄存器
}
//TFT LCD 显示函数
void display()
{
GUI_DispDecAt(year,50,10,4);
GUI_DispDecAt(month,90,10,2);
GUI_DispDecAt(day,114,10,2);
GUI_DispDecAt(hour,50,26,2);
GUI_DispDecAt(min,74,26,2);
GUI_DispDecAt(second,98,26,2);
GUI_DispStringAt(" ",114,26);
GUI_DispStringAt(week[weekday],122,26);
}
void Date_Init(void)
{
// GUI_SetTextMode(2);
GUI_DispStringAt("Date:",10,10);
GUI_DispStringAt("-",82,10);
GUI_DispStringAt("-",106,10);
GUI_DispStringAt("Time:",10,26);
GUI_DispStringAt("-",66,26);
GUI_DispStringAt("-",90,26);
// GUI_SetTextMode(0);
KeyDown_Init(); //按键中断初始化
Timer_Init(); //定时器初始化
init_value();
}