cpp
#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 //最紧急情况
{//要保存数据,否则会丢失以前的数据
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);
//恢复数据
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);
//恢复数据
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)//延时0.5秒什么都不干的函数
{
for(sleep_i=0;sleep_i<a;sleep_i++)
{
int_t();
}
}
void main()
{
//设置定时器
TMOD=0X10 ;
TH1= (65536-50000)/256;
TL1= (65536-50000)%256;
TR1=1;
//设置中断
EA=1;
EX1=1;
EX0=1;
IT1=0;
IT0=0;
while(1)
{
rgb_light();
int_t();
}
}
这个代码需要稍微改一下才能运行,看得懂的不超过1分钟应该能够搞好。看不懂的无法运行