// NAME: main.c
// DESC: 内部定时器4LED灯延时
//=========================================
/* 头文件包含 */
#define U32 unsigned int
/* 中断向量表 */
#define _ISR_STARTADDRESS 0x33ffff00
#define pISR_TIMER4 (*(unsigned *)(_ISR_STARTADDRESS+0x58))
/* 中断控制 寄存器地址 */
#define rSRCPND (*(volatile unsigned *)0x4a000000) //Interrupt request status 源挂起寄存器
#define rINTMSK (*(volatile unsigned *)0x4a000008) //Interrupt mask control 中断屏蔽寄存器
#define rINTPND (*(volatile unsigned *)0x4a000010) //Interrupt request status 中断挂起寄存器
/* 定时器 寄存器地址 */
#define rTCFG0 (*(volatile unsigned *)0x51000000) //Timer 0 configuration
#define rTCFG1 (*(volatile unsigned *)0x51000004) //Timer 1 configuration
#define rTCON (*(volatile unsigned *)0x51000008) //Timer control
#define rTCNTB4 (*(volatile unsigned *)0x5100003c) //Timer count buffer 4
/* LED灯寄存器地址 */
#define rGPBCON (*(volatile unsigned *)0x56000010) //Port B control
#define rGPBDAT (*(volatile unsigned *)0x56000014) //Port B data
#define rGPBUP (*(volatile unsigned *)0x56000018) //Pull-up control B
void led_init(void)
{
//板载LED为GPB[5:8]
rGPBCON = (rGPBCON & ~(0xff<<10)) | (0x55<<10); //rGPBCON为01 配置为输出
rGPBUP = rGPBUP | (0xf<<5); //rGPBUP为1 禁止上拉
rGPBDAT = rGPBDAT | (0xf<<5); //LED灯全关
}
void led_display(unsigned char data)
{
//0x0全灭 0xf全亮 0x01 0x02 0x04 0x80 各自灯亮
rGPBDAT = (rGPBDAT & ~(0xf<<5)) | ((~data) <<5);
}
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)
{
led_init();
timer4_init();
/* 中断函数入口地址 */
pISR_TIMER4 = (U32)timer4_ISR;
while(1);
}