按键控制流水灯的运行与暂停
项目要求:
【1】程序开始时,4个LED灯全亮一会,然后,再全灭一会,开始进入跑马灯。
【2】跑马灯过程为:D4灯亮,其余熄灭,延时,D3灯亮,其余熄灭,延时,D6灯亮,
其余熄灭,延时,D5灯亮,其余熄灭,延时…如此反复。
【3】按下SW1按键松开后,跑马灯暂停保留当前状态;再一次SW1按键松开后,从当前状态保留处继续运行跑马灯,在按下SW1按键时,不能打断流水灯的运行。
#include"ioCC2530.h" //引入头文件之后才能使用其中定义的一些寄存器的名称
#define D3 P1_0
#define D4 P1_1
#define D5 P1_3
#define D6 P1_4
#define SW1 P1_2
unsigned int count =0;
unsigned char flag=0;
void delay(unsigned int t)
{
while(t--);
}
void Init_Port() //端口初始化函数。
{ //配置LED引脚
P1SEL&=~0X1B;//0001 1011。将其设为通用I/O端口
P1DIR|=0X1B; //0001 1011。设置端口的方向为输出
//配置SW1按键引脚
P1SEL&=~0X04; //0000 0100
P1DIR&=~0X04;//按键的信息输入单片机,所以单片机此时为输入模式,将其置0。
P1INP&=~0X04; //置零操作将其设置为上拉/下拉模式
P2INP&=~0X40;//0100 0000 将P1口置0设置为上拉。
}
void Led_Check()
{
P1|=0X1B ;//0000 1111
delay(60000);
P1&=~0X1B;
delay(60000);
}
void Led_Runing()
{
delay(60);
if(flag==1)
{
count++;
}
if(count<1000)
{
D4=1;
D3=0;
D5=0;
D6=0;
}
else if(count<2000)
{
D4=0;
D3=1;
D5=0;
D6=0;
}
else if(count<3000)
{
D4=0;
D3=0;
D5=1;
D6=0;
}
else if(count<4000)
{
D4=0;
D3=0;
D5=0;
D6=1;
}
else
{
count=0;
}
}
void Scan_Key()
{
if(SW1==0)
{
delay(200);
if(SW1==0)
{
while(SW1==0)
{
Led_Runing();//等待松手时仍然执行流水灯程序。
}
if(flag==0)
{
flag=1;
}
else if(flag==1)
{
flag=0;
}
}
}
}
void main ()
{
Init_Port();
Led_Check();
flag=1;
while(1)
{
Led_Runing();
Scan_Key();
}
}