/*
此程序是依据吴坚鸿程序框架,在普中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 LED_Flag=0; //LED_Flag=0,LED灭,LED_Flag=1,LED亮;
unsigned int LED_Timer_CNT=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;
LED_Timer_CNT++;
if(LED_Timer_CNT>T1MS_CNT)
{
if(1==LED_Flag)
{
LED_Flag=0;
}
else
{
LED_Flag=1;
}
LED_Timer_CNT=0;
}
}
void LED_Flicker()
{
switch (LED_Flag)
{
case 0:
LED=0;
break;
case 1:
LED=1;
break;
}
}