文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-729210.html
#include "reg51.h"
#include "myheader.h"
#define uchar unsigned char
uchar sleep_i=0;
uchar sleep_i_b=0;
uchar i=0;
uchar i_b=0;
uchar p2_b=0;
uchar th1_b=0;
uchar tl1_b=0;
void rgb_light()
{
while(1)
{
P20=P21=0;P22=1;P23=1;P24=P25=0;
sleep(2*10);
for(i=0;i<3;i++)
{
P20=0;P22=P23=1;
sleep(1);
P20=0;P23=1;;
sleep(1);
}
P2=0;P21=P23=1;
sleep(5*2);
P2=0;P20= P25=1;
sleep(2*50);
for(i=0;i<3;i++)
{
P2=0;P20=P24=1;
sleep(1);
P2=0;P20=1;
sleep(1);
}
P2=0;P20=P24=1;
sleep(5*2);
}
}
void int_t0() interrupt 0 //最緊急情況
{//要保存數(shù)據(jù),否則會(huì)丟失以前的數(shù)據(jù)
i_b=i;
p2_b=P2;
sleep_i_b=sleep_i;
th1_b=TH1;
tl1_b=TL1;
EA=0;
P2=0;P20=P23=1;
sleep(2*10);
//恢復(fù)數(shù)據(jù)
i=i_b;
P2=p2_b;
TH1=th1_b;
TL1=tl1_b;
sleep_i=sleep_i_b;
EA=1;
}
void int_t1() interrupt 2
{
EA=0;
i_b=i;
p2_b=P2;
sleep_i_b=sleep_i;
th1_b=TH1;
tl1_b=TL1;
EA=1;
P2=0;P22=P23=1;
sleep(2*5);
//恢復(fù)數(shù)據(jù)
EA=0;
i=i_b;
P2=p2_b;
TH1=th1_b;
TL1=tl1_b;
sleep_i=sleep_i_b;
EA=1;
}
void int_t()
{
static uchar i=0;
for(i=0;i<10;i++)
{
TH1= (65536-50000)/256;
TL1= (65536-50000)%256;
TR1=1;
while(TF1==0)
{
;
}
TF1=0;
}
}
void sleep(long int a)//延時(shí)0.5秒什么都不干的函數(shù)
{
for(sleep_i=0;sleep_i<a;sleep_i++)
{
int_t();
}
}
void main()
{
//設(shè)置定時(shí)器
TMOD=0X10 ;
TH1= (65536-50000)/256;
TL1= (65536-50000)%256;
TR1=1;
//設(shè)置中斷
EA=1;
EX1=1;
EX0=1;
IT1=0;
IT0=0;
while(1)
{
rgb_light();
int_t();
}
}
這個(gè)代碼需要稍微改一下才能運(yùn)行,看得懂的不超過(guò)1分鐘應(yīng)該能夠搞好??床欢臒o(wú)法運(yùn)行文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-729210.html
到了這里,關(guān)于AT89C51單片機(jī)紅綠燈控制源代碼(C語(yǔ)言版)附帶Proteus電路圖的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!