/******************************************************************************
此程序是依据吴坚鸿程序框架,在普中51 A2单片机开发板上的程序练习
程序目标:LED以1HZ的频率闪烁
*******************************************************************************/
#include<REG51.H>
#define Main_Fosc 12000000L //默认系统时钟12Mhz
#define T1MS (65536-Main_Fosc/12/1000) //12分频下1ms定时器的装载值,n=t/T=t/(12/f)=0.001*f/12=f/12/1000
#define T1MS_CNT 1000 //1000个1ms累加产生1S
sbit LED=P2^0; //LED为P21口,低点亮
unsigned int LED_Timer_CNT=0;
unsigned char Time_Lock_Flag=0;
void LED_Flicker(); //LED闪烁函数
void Sys_Init(); //系统初始化
void Delay_Long(); //长延时,等待系统稳定
void Perpherial_Init(); //端口初始化
void main()
{
Sys_Init();
Delay_Long();
Perpherial_Init();
while (1)
{
LED_Flicker();
}
}
void Sys_Init()
{
TMOD=0X01; //定时器0模式1
TL0=T1MS;
TH0=T1MS>>8;
}
void Delay_Long()
{
unsigned char i,j;
for(i=0;i++;i<220)
{
for(j=0;j<220;j++)
;
}
}
void Perpherial_Init()
{
ET0=1;
TR0=1;
EA=1;
}
void Timer0_ISR() interrupt 1 //定时器0中断函数
{
TL0=T1MS;
TH0=T1MS>>8;
if ((1==Time_Lock_Flag)&&(LED_Timer_CNT>0))
{
LED_Timer_CNT--;
}
}
void LED_Flicker()
{
static unsigned char LED_Status_Flag=0;
switch (LED_Status_Flag)
{
case 0:
if (0==LED_Timer_CNT)
{
LED=0;
Time_Lock_Flag=0;
LED_Timer_CNT=T1MS_CNT;
Time_Lock_Flag=1;
LED_Status_Flag=1;
}
break;
case 1:
if (0==LED_Timer_CNT)
{
LED=1;
Time_Lock_Flag=0;
LED_Timer_CNT=T1MS_CNT;
Time_Lock_Flag=1;
LED_Status_Flag=0;
}
break;
}
}
标签:CNT,LED,void,51,Timer,单片机,Flag,Time
From: https://www.cnblogs.com/boliuXun/p/18169644