- UID
- 856476
|
本文转自:http://blog.sina.com.cn/s/blog_62b616340100f7vf.html
#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(n) n 微妙延时
//定义端口
#define led *(volatile unsigned char *)LED_BASE
//函数声明
void Timer_Init();
void Timer_interrupts(void* context, alt_u32 id);
//定时器初始化
void Timer_Init()
{
alt_irq_register(TIMER_IRQ,0,Timer_interrupts); //注册中断函数
IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_BASE,0); //清状态标志
IOWR_ALTERA_AVALON_TIMER_PERIODH(TIMER_BASE,50000000>>16);
IOWR_ALTERA_AVALON_TIMER_PERIODL(TIMER_BASE,50000000&0xffff);//修改定时时间1s
IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER_BASE,7); //启动定时器允许中断,连续计数
}
//定时器中断服务函数
void Timer_interrupts(void* context, alt_u32 id)
{
led = ~led;
IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_BASE,0); //清状态寄存器
//IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER_BASE, 0); //停止计数
// IOWR_ALTERA_AVALON_TIMER_PERIODH(TIMER_BASE,50000000>>16);
//IOWR_ALTERA_AVALON_TIMER_PERIODL(TIMER_BASE,50000000&0xffff);//修改定时时间1s
// IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER_BASE, 5); //启动定时器允许中断
}
int main(void)
{
led = 1;
Timer_Init(); //定时器初始化
while(1); //等待
return 0;
}
|
|