http://www.ceet.hbnu.edu.cn/bbs/viewthread.php?tid=7281&extra=page%3D2
写在前面的话:最近两天在写这个东西,一直没写出来,最终在一个FPGA研究群里受人指点:
#define seg1 *(volatile unsigned char *) SEG_1_BASE
这种寄存器映射如果CPU等级选在F型的话,就不能映射,所以我改为S型的。
至于为什么,我也没弄清楚,一会把原因发给大家,我把C程序给大家看。
- #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 seg1 *(volatile unsigned char *) SEG_1_BASE
- #define seg2 *(volatile unsigned char *) SEG_2_BASE
- #define seg3 *(volatile unsigned char *) SEG_3_BASE
- #define seg4 *(volatile unsigned char *) SEG_4_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()
- {
- IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER1_BASE, 0); //清状态标志
- //1S的定时
- IOWR_ALTERA_AVALON_TIMER_PERIODH(TIMER1_BASE, 50000000>>16);
- IOWR_ALTERA_AVALON_TIMER_PERIODL(TIMER1_BASE, 50000000&0xffff);//修改定时时间1s
- IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER1_BASE, 0x07); //启动定时器允许中断,连续计数
- alt_irq_register(TIMER1_IRQ,0,Timer_ISR); //注册中断函数
- }
- //定时器中断服务函数
- 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(TIMER1_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;
- seg1 = duan[num[0]];
- seg2 = duan[num[1]];
- seg3 = duan[num[3]];
- seg4 = duan[num[4]];
- usleep(500);
- }
- //主函数
- int main(void)
- {
- KeyDown_Init(); //按键中断初始化
- Timer_Init(); //定时器初始化
- while(1)
- {
- display(); //显示
- }
- return 0;
- }
此程序在DE0上完美显示
按键可调