基于stc15f2k60s2芯片单片机编程(多种间隔时间流水灯)

1.按键S7切换流水灯模式(4种模式循环切换),模式1:点亮L1,并以100ms的间隔实现从左向右流水;模式2:点亮L8,并以100ms的间隔实现右左向左流水;模式3:点亮L1和L8,并以100ms的间隔从两边向中间流水;模式4:点亮L4和L5,并以100ms为间隔实现从中间到两边流水。
2.按键S6切换流水间隔(100ms–300ms–600ms–1000ms–100ms)

#include 
#include 


sbit S7=P3^0;
sbit S6=P3^1;

unsigned int a;
unsigned char work,time;
unsigned char i;
unsigned char moshiyi[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};						
unsigned char moshier[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
unsigned char moshisan[]={0x7e,0xbd,0xdb,0xe7,0x7e,0xbd,0xdb,0xe7};
unsigned char moshisi[]={0xe7,0xdb,0xbd,0x7e,0xe7,0xdb,0xbd,0x7e};

void anjian();
void Timer0Init();
void Delay10ms();



void main()
{
	P2=0XA0;P0=0X00;P2=0X1F;
	Timer0Init();EA=1;ET0=1;
	
	while(1)
	{
	  anjian();
		if(i>=8)i=0;
		switch (work)
		{
			case 0:P2=0X80;P0=moshiyi[i];P2=0X1F;break;
			case 1:P2=0X80;P0=moshier[i];P2=0X1F;break;
			case 2:P2=0X80;P0=moshisan[i];P2=0X1F;break;
			case 3:P2=0X80;P0=moshisi[i];P2=0X1F;break;
		}
	}
}
void anjian()
{
  if(S7==0)
	{
	  Delay10ms();
		if(S7==0)
		{
		  work++;
			if(work>3)
				work=0;
		}
		while(!S7);
	}
	
	
	if(S6==0)
	{
	  Delay10ms();
		if(S6==0)
		{
		  time++;
			if(time>3)
				time=0;
		}
		while(!S6);
	}
}


void Timer0Init(void)		//1毫秒@11.0592MHz
{
	AUXR |= 0x80;		//定时器时钟1T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0xCD;		//设置定时初值
	TH0 = 0xD4;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
}


void Timer0() interrupt 1
{

	a++;
 if(time==0)
 {
	 if(a>=100)
	 {
		 i++;
		 a=0;
	 }
 }
 if(time==1)
 {
	 if(a>=300)
	 {
		 i++;
	   a=0;
	 }
 }
 if(time==2)
 {
	 if(a>=600)
	 {
		 i++;
		 a=0;
	 }
 }
 if(time==3)
 {
   if(a>=1000)
	 {
		 i++;
		 a=0;
	 }
 }
}
void Delay10ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 108;
	j = 145;
	do
	{
		while (--j);
	} while (--i);
}

你可能感兴趣的:(基于stc15f2k60s2芯片单片机编程(多种间隔时间流水灯))