基于51单片机的led灯控制,
单片机源程序如下:
- /*
- * @file: led灯控制器.c
- * @function: 控制led
- * @date: 2019.5.22
- * @version: v1.0
- * @author: xiaohuli
- */
- #include<reg51.h>
- sbit key2=P3^0; //定义独立按键端口
- sbit key3=P3^2;
- sbit key1=P3^1;
- sbit key4=P3^3;
- unsigned char led2[]={
- 0x7f,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf}; //定义led向右移动
- unsigned char led1[]={
- 0xfe,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd}; //定义led向左移动
- unsigned char led3[]={
- 0xf7,0xe7,0xe3,0xc3,0xc1,0x81,0x80,0x00}; // 模式2led
- unsigned char led4[]={
- 0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; // 模式3led
- unsigned char led5[]={
- 0xfe,0xdf,0xfb,0x7f,0xef,0xfd,0xbf,0xf7}; // 模式3led
- /*
- * @file: delay(unsigned int z)
- * @function: 延时
- * @date: 2019.5.22
- * @version: v1.0
- * @author: xiaohuli
- */
- void delay(unsigned int z)
- {
- unsigned int y;
- for(;z>0;z--)
- for(y=10;y>0;y--);
- }
- /*
- * @file: Timer0Init()
- * @function: 定时器初始化
- * @date: 2019.5.22
- * @version: v1.0
- * @author: xiaohuli
- */
- void Timer0Init()
- {
- TMOD|=0X01;//选择为定时器0模式,工作方式1,仅用TR0打开启动。
- TH0=0XFC; //给定时器赋初值,定时1ms
- TL0=0X18;
- ET0=1;//打开定时器0中断允许
- EA=1;//打开总中断
- TR0=1;//打开定时器
- }
- /*
- * @file: interface()
- * @function: 初始界面
- * @date: 2019.5.22
- * @version: v1.0
- * @author: xiaohuli
- */
- void interface()
- {
- int i,j;
- for(i=0;i<2;i++)
- {
- for(j=0;j<8;j++)
- {
- P2=led1[j];
- delay(1000);
- }
- }
- i=0; //将ij置0,方便下一次循环
- j=0;
- for(i=0;i<2;i++)
- {
- for(j=0;j<8;j++)
- {
- P2=led2[j];
- delay(1000);
- }
- }
- i=0;
- j=0;
- for(i=0;i<5;i++)
- {
- P2=0xaa;
- delay(2000);
- P2=0xff;
- delay(2000);
- }
- for(j=0;j<5;j++)
- {
- P2=0x55;
- delay(2000);
- P2=0xff;
- delay(2000);
- }
- }
- /*
- * @file: pattern1()
- * @function: 模式1
- * @date: 2019.5.22
- * @version: v1.0
- * @author: xiaohuli
- */
- void pattern1()
- {
- P2=0xaa;
- delay(5000);
- P2=0x55;
- delay(5000);
- }
- /*
- * @file: pattern2()
- * @function: 模式2
- * @date: 2019.5.22
- * @version: v1.0
- * @author: xiaohuli
- */
- void pattern2()
- {
- int i,j;
- for(j=0;j<3;j++)
- {
- for(i=0;i<8;i++)
- {
- P2=led3[i];
- delay(5000);
- }
- if(i==8)
- {
- P2=0xff;
- delay(5000);
- }
- }
- }
- /*
- * @file: pattern3()
- * @function: 模式3
- * @date: 2019.5.22
- * @version: v1.0
- * @author: xiaohuli
- */
- void pattern3()
- {
- int i,j;
- int flag; // 定义向左向右的标志变量
- int keyflag; // 定义处理按键按下的标志变量
- int count;
- if(key3==0)
- {
- delay(10); //延时消抖
- if(key3==0)
- {
- keyflag=1;
- TR0=1; //开启定时器
- while(!key3);
- TR0=0; //关闭定时器
- }
- }
- if(key4==0)
- {
- delay(10);
- if(key4==0)
- {
- keyflag=2;
- TR0=1;
- while(!key4);
- TR0=0;
- }
- }
- if(keyflag==1) //处理按键
- {
- if(count>=1500) //长按处理程序
- {
- i++;
- if(i>=4)
- i=3;
- }
- else //短按处理程序
- {
- i++;
- if(i>=4)
- i=3;
- }
- keyflag=0; //将标志变量置0;
- count=0;
- }
- if(keyflag==2)
- {
- if(count>=1500)
- {
- flag++;
- if(flag==2)
- flag=0;
- }
- else
- {
- flag++;
- if(flag==2)
- flag=0;
- }
- keyflag=0;
- count=0;
- }
- switch(i) //对i,进行分类处理,判断是左移或右移几位
- {
- case 0:
- if(flag==0)
- {
- for(j=0;j<8;j++)
- {
- P2=led4[j];
- delay(5000);
- }
- j=0;
- }
- if(flag==1)
- {
- for(j=0;j<8;j++)
- {
- P2=led1[j];
- delay(5000);
- }
- j=0;
- }
- break;
- case 1:
- if(flag==0)
- {
- for(j=0;j<8;j=j+2)
- {
- P2=led4[j];
- delay(5000);
- }
- j=0;
- }
- if(flag==1)
- {
- for(j=0;j<8;j=j+2)
- {
- P2=led1[j];
- delay(5000);
- }
- j=0;
- }
- break;
- case 2: //这个算法可以直接定义一个一维数组代替
- if(flag==0)
- {
- for(j=0;j<22;j=j+3)
- {
- if(j==9)
- {
- P2=0xfd;
- delay(5000);
- }
- else
- {
- if(j==12)
- {
- P2=0xef;
- delay(5000);
- }
- else
- {
- if(j==15)
- {
- P2=0x7f;
- delay(5000);
- }
- else
- {
- if(j==18)
- {
- P2=0xfb;
- delay(5000);
- }
- else
- {
- if(j==21)
- {
- P2=0xdf;
- delay(5000);
- }
- else
- {
- P2=led4[j];
- delay(5000);
- }
- }
- }
- }
- }
- }
- j=0;
- }
- if(flag==1)
- {
- for(j=0;j<8;j++)
- {
- P2=led5[j];
- delay(5000);
- }
- j=0;
- }
- break;
- case 3:
- if(flag==0)
- {
- for(j=0;j<8;j=j+4)
- {
- P2=led4[j];
- delay(5000);
- }
- j=0;
- }
- if(flag==1)
- {
- for(j=0;j<8;j=j+4)
- {
- P2=led1[j];
- delay(5000);
- }
- j=0;
- }
- break;
- }
- }
- /*
- * @file: 主函数
- * @function: 主函数
- * @date: 2019.5.22
- * @version: v1.0
- * @author: xiaohuli
- */
- void main()
- {
- Timer0Init();
- interface();
- while(1)
- {
- int keyflag2;
- int k;
- int count;
- if(key1==0)
- {
- delay(10);
- if(key1==0)
- {
- keyflag2=1;
- TR0=1;
- while(!key1);
- TR0=0;
- }
- }
- if(key2==0)
- {
- delay(10);
- if(key2==0)
- {
- keyflag2=2;
- TR0=1;
- while(!key2);
- TR0=0;
- }
- }
- if(keyflag2==1)
- {
- if(count>=1500)
- {
- pattern1();
- }
- else
- {
- pattern1();
- }
- //keyflag2=0; 注意,这个标志变量不能置0,否则·不会·无限循环
- count=0;
- }
- if(keyflag2==2)
- {
- if(count>=1500)
- {
- for(k=0;k<1;k++)
- {
- pattern2();
- }
- pattern3();
- }
- else
- {
- for(k=0;k<1;k++)
- {
- pattern2();
- }
- for(k=0;k<100;k++)
- pattern3();
- }
- keyflag2=0;
- count=0;
- }
- }
- }
- /*
- * @file: Timer0() interrupt 1
- * @function: 定时器0的1模式中断
- * @date: 2019.5.22
- * @version: v1.0
- * @author: xiaohuli
- */
- void Timer0() interrupt 1
- {
- static int i;
- TH0=0XFC; //给定时器赋初值,定时1ms
- TL0=0X18;
- i++;
- }
复制代码
所有资料51hei提供下载:
led灯控制器.zip
(27.01 KB, 下载次数: 9)
|