/*
题目: watchdog
日期: 2013-07-10
操作要求: F区PC2接地,PD2、3跳线接上(启用外部中断) 本例外部中断0开启定时器,外部中断1关闭定时器
晶振: 11.0592Mhz
作者: 任聚才
*/
#include<reg52.h>
#include<intrins.h>
sfr WDTRST = 0xa6;
sfr WDTRPG = 0xa7 ;
unsigned char led[9] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff};
static volatile unsigned char i,time_count;
/*
WDTPGR |= 0x00(2^14-1) 机器周期,16.3ms @Fosc = 12MHz
WDTPGR |= 0x01(2^15-1) 机器周期,32.7ms @Fosc = 12MHz
WDTPGR |= 0x02(2^16-1) 机器周期,65.5ms @Fosc = 12MHz
WDTPGR |= 0x03(2^17-1) 机器周期,131ms @Fosc = 12MHz
WDTPGR |= 0x04(2^18-1) 机器周期,262ms @Fosc = 12MHz
WDTPGR |= 0x05(2^19-1) 机器周期,542ms @Fosc = 12MHz
WDTPGR |= 0x06(2^20-1) 机器周期,1.05ms @Fosc = 12MHz
WDTPGR |= 0x07(2^21-1) 机器周期,2.09ms @Fosc = 12MHz
*/
//流水灯花样定义
unsigned char code Type_PA[] = {
0xFC,0xF9,0xF3,0xE7,0xCF,0x9F,0x3F,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xE7,0xDB,0xBD,0x7E,0xBD,0xDB,0xE7,0xFF,0xE7,0xC3,0x81,0x00,0x81,0xC3,0xE7,0xFF,
0xAA,0x55,0x18,0xFF,0xF0,0x0F,0x00,0xFF,0xF8,0xF1,0xE3,0xC7,0x8F,0x1F,0x3F,0x7F,
0x7F,0x3F,0x1F,0x8F,0xC7,0xE3,0xF1,0xF8,0xFF,0x00,0x00,0xFF,0xFF,0x0F,0xF0,0xFF,
0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE,
0xFE,0xFC,0xF8,0xF0,0xE0,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0xE0,0xF0,0xF8,0xFC,0xFE,
0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF
};
void init_time_ExternalInterrupt_watchdog(void)
{
EX0 = 1; //外部中断0开
EX1 = 1; //外部中断1开
IT0 = 0; //电平触发
IT1=0; //边沿触发,IT1=0表示电平触发
TMOD |= 0x01; //使用模式1,使用"|"符号可以在使用多个定时器时不受影响
TH0=0x00; //给定初值,这里使用定时器最大值从0开始计数一直到65535溢出
TL0=0x00;
ET0=1; //定时器中断打开
TR0=1; //定时器开关打开
EA = 1; //全局中断开
P0 = 0;
}
/*------------------------------------------------
延时子程序
------------------------------------------------*/
void _delay_xus(unsigned int cnt)
{
while(--cnt);
}
int main(void)
{
init_time_ExternalInterrupt_watchdog();
for(i=0;i<sizeof(Type_PA/sizeof(Type_PA[0]));i++) //若重启,狗会从花样流水灯执行。即此处是重启标志。
{
P0 = Type_PA[i];
_delay_xus(9535);
}
// WDTPRG = 0x07;
WDTRST = 0x1e; //按手册顺序写0x1e和0xe1,激活看门狗,16.3ms
WDTRST = 0xe1;
while(1);
return 0;
}
/*------------------------------------------------
外部中断程序 要是把关门狗注释掉,再编辑烧进程序,按外部中断,会发现灯初始化后不闪烁了。因为外部中断里面把定时器关掉了
------------------------------------------------*/
void ISR_Key0(void) interrupt 0 using 1
{
TR0 = 0;
}
void ISR_Key1(void) interrupt 2 using 0
{
TR0 = 0;
// WDTRST = 0x1e; //按手册顺序写0x1e和0xe1,激活看门狗
// WDTRST = 0xe1;
}
/*------------------------------------------------
定时器中断子程序
------------------------------------------------*/
void Timer0_isr(void) interrupt 1 using 1
{
TH0=(65535-10000)/256; //重新赋值,50ms
TL0=(65535-10000)%0xff;
time_count++;
if(time_count==100)
{
P0 = ~P0;
time_count = 0;
}
WDTRST = 0x1e; //按手册顺序写0x1e和0xe1,激活看门狗
WDTRST = 0xe1;
}