实验现象:
定时器实现流水灯;
四个模式;S6调模式;S7暂停
yunzuo_state1:按照L1、L2...L8的顺序,从左到右单循环点亮。
yunzuo_state2:按照L8、L7...L1的顺序,从右到左单循环点亮。
yunzuo_state3 L1、L8往回聚集
yunzuo_state4 L4、L5往出扩散
代码展示:
/****************************************
定时器实现流水灯;流转间隔可调。
四个模式;S6调模式;S7暂停
yunzuo_state1:按照L1、L2...L8的顺序,从左到右单循环点亮。
yunzuo_state2:按照L8、L7...L1的顺序,从右到左单循环点亮。
yunzuo_state3 L1、L8往回聚集
yunzuo_state4 L4、L5往出扩散
流转间隔设置:
*******************************************/
#include "reg52.h"
#include "intrins.h"
sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sfr AUXR = 0x8e;
unsigned int time = 0;
unsigned char count = 0;
unsigned char yunzuo_state =1;
int ledCode[8]= //led段码(单个显示)
{0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
int ledCode2[8]= //led段码(两两显示)
{0x7e,0xbd,0xdb,0xe7,0x7e,0xbd,0xdb,0xe7};
unsigned int liuzhuan_val[5]=
{0,400,400,500,900};//0不管,1、2、3、4分别为四个流转间隔的值
unsigned char light_val = 0;
unsigned char light_level = 2;
void Delay100ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 5;
j = 52;
k = 195;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void Delay10ms() //@11.0592MHz
{
unsigned char i, j;
i = 108;
j = 145;
do
{
while (--j);
} while (--i);
}
void Timer0Init(void) //10毫秒@11.0592MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x00; //设置定时初值
TH0 = 0xDC; //设置定时初值
TF0 = 0; //清除TF0标志
ET0 = 1;
EA = 1;
TR0 = 1; //定时器0开始计时
}
void Delay50ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 3;
j = 26;
k = 223;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void Lightlevel_Proc()
{
switch(light_level)
{
case 1:
Delay10ms();
break;
case 2:
Delay50ms();
break;
case 3:
Delay100ms();
Delay50ms();
break;
case 4:
Delay100ms();
Delay100ms();
Delay100ms();
break;
}
}
void display1() //1按照L1、L2...L8的顺序,从左到右单循环点亮。
{
if(count>7) count=0;
P2 =(P2 & 0x1f)|0x80;
P0 = ledCode[count];
Lightlevel_Proc();
P0 = 0xff;
}
void display2() //2按照L8、L7...L1的顺序,从右到左单循环点亮。
{
if(count>7) count=0;
P2 =(P2 & 0x1f)|0x80;
P0 = ledCode[7-count];
Lightlevel_Proc();
P0 = 0xff;
}
void display3() //3 L1、L8往回聚集
{
if(count>7) count=0;
P2 =(P2 & 0x1f)|0x80;
P0 = ledCode2[count];
Lightlevel_Proc();
P0 = 0xff;
}
void display4() //4 L4、L5往出扩散
{
if(count>7) count=0;
P2 =(P2 & 0x1f)|0x80;
P0 = ledCode2[7-count];
}
void Timer0_Service() interrupt 1
{
time++;//流转间隔控制
// light_val++;//亮度等级控制
if(time %(liuzhuan_val[1]/10) == 0) //400,1000,500,900ms
{
if(yunzuo_state == 1){display1(); count++;}
}
if(time %(liuzhuan_val[2]/10) == 0)
{
if(yunzuo_state == 2){display2(); count++;}
}
if(time %(liuzhuan_val[3]/10) == 0)
{
if(yunzuo_state == 3){display3(); count++;}
}
if(time %(liuzhuan_val[4]/10) == 0)
{
if(yunzuo_state == 4){display4(); count++;}
}
}
void main()
{
Timer0Init();
while(1)
{
if(S7 == 0)
{
Delay10ms();
if(S7 == 0)
{
while(S7 == 0);
TR0 = !TR0;
}
}
if(S6 == 0)
{
Delay10ms();
if(S6 == 0)
{
while(S6 == 0);
yunzuo_state ++;
if(yunzuo_state>4)yunzuo_state=1;
}
}
if(S5 == 0)
{
Delay10ms();
if(S5 == 0)
{
while(S5 == 0);
light_level ++;
if(light_level>4)light_level=1;
}
}
}
}