是根据
http://bbs.elecfans.com/jishu_184829_1_1.html这里面大神的程序弄的,我是新手,请大神看看。
#include<reg51.h>
#define LED P0
#define LED_ON() LED = 0x00 //所有LED亮
#define LED_OFF() LED = 0xff //所有LED熄灭
unsigned int g_u16Led
timeCount = 0 ; //LED计数器
unsigned char g_u8LedState = 0 ; //LED状态标志, 0表示亮,1表示熄灭
unsigned char g_bSystemTime1Ms = 0 ;
void LedProcess(void)
{
if(0 == g_u8LedState) //如果LED的状态为亮,则点亮LED
{
LED_ON() ;
}
else //否则熄灭LED
{
LED_OFF() ;
}
}
void LedStateChange(void)
{
if(g_bSystemTime1Ms) //系统1MS时标到
{
g_bSystemTime1Ms = 0 ;
g_u16LedTimeCount++ ; //LED计数器加一
if(g_u16LedTimeCount >= 500) //计数达到500,即500MS到了,改变LED的状态。
{
g_u16LedTimeCount = 0 ;
g_u8LedState = ! g_u8LedState ;
}
}
}
void main(void)
{
while(1)
{
LedProcess() ;
LedStateChange() ;
}
}
#include<reg52.h>
sbit LED_SEG = P1^4; //数码管段选
sbit LED_DIG = P1^5; //数码管位选
sbit LED_CS11 = P1^6; //led控制位
sbit ir=P1^7;
#define LED P0 //定义LED接口
bit g_bSystemTime1Ms = 0 ; // 1MS系统时标
unsigned int g_u16LedTimeCount = 0 ; //LED计数器
unsigned char g_u8LedState = 0 ; //LED状态标志, 0表示亮,1表示熄灭
#define LED_ON() LED = 0x00 ; //所有LED亮
#define LED_OFF() LED = 0xff ; //所有LED熄灭
void Timer0Init(void)
{
TMOD &= 0xf0 ;
TMOD |= 0x01 ; //定时器0工作方式1
TH0 = 0xfc ; //定时器初始值
TL0 = 0x66 ;
TR0 = 1 ;
ET0 = 1 ;
}
void LedProcess(void)
{
if(0 == g_u8LedState) //如果LED的状态为亮,则点亮LED
{
LED_ON() ;
}
else //否则熄灭LED
{
LED_OFF() ;
}
}
void LedStateChange(void)
{
if(g_bSystemTime1Ms) //系统1MS时标到
{
g_bSystemTime1Ms = 0 ;
g_u16LedTimeCount++ ; //LED计数器加一
if(g_u16LedTimeCount >= 500) //计数达到500,即500MS到了,改变LED的状态。
{
g_u16LedTimeCount = 0 ;
g_u8LedState = ! g_u8LedState ;
}
}
}
void main(void)
{
Timer0Init() ;
EA = 1 ;
LED_CS11 = 1 ; //74HC595输出允许
LED_SEG = 0 ; //数码管段选和位选禁止(因为它们和LED共用P0口)
LED_DIG = 0 ;
while(1)
{
LedProcess() ;
LedStateChange() ;
}
}
void Time0Isr(void) interrupt 1
{
TH0 = 0xfc ; //定时器重新赋初值
TL0 = 0x66 ;
g_bSystemTime1Ms = 1 ; //1MS时标标志位置位
}
一周热门 更多>