#include <reg52.h>
#include <intrins.h>
#define INT8U unsigned char
#define INT16U unsigned int
sbit RED_LED = P1^1; //RED_LED低电平点亮
INT8U TimeCnter_m = 0; //全局变量,50ms自增计时器
INT16U TimeCnter_x = 0; //全局变量,50ms自增计时器
INT16U TimeCnter_y = 0; //全局变量,1s自增计时器
INT16U TimeCnter_pj = 0;
INT16U TimeCnter_mj = 0;
//==========================================================================================================================
void DelayNs(INT16U Ns) //定时器延时Ns,测试中延时用
{
TimeCnter_y=0;
while(TimeCnter_y<Ns);//最长延时65535s(约18.2小时)
}
//==========================================================================================================================
void Init_Com_T0(void) //串行口和T0初始化程序
{ //T1:Gate C/T M1 M0 T0:Gate C/T M1 M0
TMOD = 0x21; // 0 0 1 0 0 0 0 1: T1方式2定时,8位初值重装,T0:定时器,方式1(16位)
TH0 = 0x4C; //T0定时50ms(50000us)的初值为0x4C00
TL0 = 0x00;
PCON = 0x00; //波特率不倍增
SCON = 0x50; //串口以方式1工作,8位波特率可变
TH1 = 0xFd; //波特率:9600 bps
TL1 = 0xFd;
EA = 1; //开总中断
ET0 = 1; //允许T0定时中断
ES = 1; //允许串口中断
TR0 = 1; //T0开始定时
TR1 = 1; //T1开始定时
}
//========================================================//
void T0_ISR() interrupt 1 //T0定时50ms的中断服务程序
{
TH0=0x4C;
TL0=0x00;
TimeCnter_m++; //每50ms到自增一次
TimeCnter_x++; //每50ms到自增一次
if(TimeCnter_m==20) //定时1s到,50ms * 20 = 1s
{
TimeCnter_m=0; //TimeCnter_m为50ms计数器
TimeCnter_y++; //TimeCnter_y为1s计数器
TimeCnter_pj=1;
}
}
//===============================================//
void main()
{
Init_Com_T0();//串口初始化
while(1)
{
RED_LED=0;
DelayNs(5);
RED_LED=1;
}
} |