/******************************************************************************
此程序是依据吴坚鸿程序框架,在普中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
#define LED P2 //LED为P2口,低点亮
unsigned char code LED_Seg[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
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中断函数
{
if (LED_Timer_CNT>0)
{
LED_Timer_CNT--;
}
TL0=T1MS;
TH0=T1MS>>8;
}
void LED_Flicker()
{
static unsigned char LED_Status_Flag=0;
switch (LED_Status_Flag)
{
case 0:
if (0==LED_Timer_CNT)
{
LED_Timer_CNT=T1MS_CNT;
LED=LED_Seg[LED_Status_Flag];
LED_Status_Flag=1;
}
break;
case 1:
if (0==LED_Timer_CNT)
{
LED_Timer_CNT=T1MS_CNT;
LED=LED_Seg[LED_Status_Flag];
LED_Status_Flag=2;
}
break;
case 2:
if (0==LED_Timer_CNT)
{
LED_Timer_CNT=T1MS_CNT;
LED=LED_Seg[LED_Status_Flag];
LED_Status_Flag=3;
}
break;
case 3:
if (0==LED_Timer_CNT)
{
LED_Timer_CNT=T1MS_CNT;
LED=LED_Seg[LED_Status_Flag];
LED_Status_Flag=4;
}
break;
case 4:
if (0==LED_Timer_CNT)
{
LED_Timer_CNT=T1MS_CNT;
LED=LED_Seg[LED_Status_Flag];
LED_Status_Flag=5;
}
break;
case 5:
if (0==LED_Timer_CNT)
{
LED_Timer_CNT=T1MS_CNT;
LED=LED_Seg[LED_Status_Flag];
LED_Status_Flag=6;
}
break;
case 6:
if (0==LED_Timer_CNT)
{
LED_Timer_CNT=T1MS_CNT;
LED=LED_Seg[LED_Status_Flag];
LED_Status_Flag=7;
}
break;
case 7:
if (0==LED_Timer_CNT)
{
LED_Timer_CNT=T1MS_CNT;
LED=LED_Seg[LED_Status_Flag];
LED_Status_Flag=0;
}
break;
}
}
标签:Status,CNT,LED,单片机,T1MS,51,Timer,跑马灯,Flag
From: https://www.cnblogs.com/boliuXun/p/18169717