//=========================================
// NAME: main.c
// DESC: 内部定时器4LED灯延时
//=========================================
/* 头文件包含 */
#include "2440addr.h"
#include "def.h"
#include "mmu.h"
void led_init(void)
{
//板载LED为GPF[4:7]
rGPFCON = 0x5500; //rGPFCON为01 配置为输出
rGPFUP = rGPFUP | (0xf<<4); //rGPBUP为1 禁止上拉
rGPFDAT = rGPFDAT | (0xf<<4); //LED灯全关
}
void led_display(unsigned char data)
{
//0x0全灭 0xf全亮 0x01 0x02 0x04 0x80 各自灯亮
rGPFDAT = (rGPFDAT & ~(0xf<<4)) | ((~data) <<4);
}
void timer4_init(void)
{
rSRCPND = rSRCPND | (0x1<<14); //清空定时器4源请求
rINTPND = rINTPND | (0x1<<14); //清空定时器4中断请求
rINTMSK = rINTMSK & ~(0x1<<14);//打开定时器4中断
//定时器配制寄存器0
//定时器输入时钟频率 = PCLK / {预分频值+1} / {分频值}
//{预分频值} = 0~255 {分频值} = 2, 4, 8, 16
//25KHz:50MHz/(250*8)=50MHz/(2000)
rTCFG0 = (rTCFG0 & ~(0xff<<8)) | (249<<8); // prescaler1:249
rTCFG1 = (rTCFG1 & ~(0xf<<16)) | (0x2<<16); //divider:8,0b0010
rTCNTB4 = 25000; //让定时器4每隔1秒中断一次 25000=1*25000
rTCON = (rTCON & ~(0x7<<20)) | (0x7<<20); //自动重载、手动更新、启动定时器4
rTCON = (rTCON & ~(0x2<<20)); //关闭手动更新
}
void __irq timer4_ISR(void)
{
static int count;
rSRCPND = rSRCPND | (0x1<<14);
rINTPND = rINTPND | (0x1<<14);
//每隔0.5秒LED灯亮一次
if (count == 0)
{
led_display(0xf); //LED亮
count = 1;
}
else if (count == 1)
{
led_display(0x0); //LED灭
count = 0;
}
}
void Main(void)
{
MMU_Init();
led_init();
timer4_init();
/* 中断函数入口地址 */
pISR_TIMER4 = (U32)timer4_ISR;
while(1);
}