本帖最后由 lidaji0 于 2013-11-8 22:36 编辑
功能就是:LED0、1和LED2、3分别为左右两组,模式就是左闪闪右闪闪,同闪闪,快慢
上菜,大家给看看怎么整能合理点
/*-----------------------------------------------
------------------------------------------------*/
#include<reg52.h>
sbit LED0=P1^0;
sbit LED1=P1^1;
sbit LED2=P1^2;
sbit LED3=P1^3;
sbit LED4=P1^4;
sbit LED5=P1^5;
sbit LED6=P1^6;
sbit LED7=P1^7;
sbit KEY1=P0^0;
sbit KEY2=P0^1;
sbit KEY3=P0^2;
sbit KEY4=P0^3;
sbit KEY5=P0^4;
sbit KEY6=P0^5;
sbit KEY7=P0^6;
sbit KEY8=P0^7;
unsigned char i;
unsigned char j;
unsigned char mode;
void Delay(unsigned int t);
void DelayUs2x(unsigned char t);
void DelayMs(unsigned char t);
unsigned char KeyScan(void);
void Init_LED(void);
void Init_Timer0(void);
/*------------------------------------------------
------------------------------------------------*/
void main (void)
{
unsigned char keyPress=0;
mode=0;
Init_LED();
Init_Timer0();
while (1)
{
P0=0xFF;
keyPress=KeyScan();
if(keyPress!=0){
switch(keyPress){
case 1:
mode=0;
break;
case 2:
mode=1;
break;
case 3:
mode=2;
break;
case 4:
mode=3;
break;
case 5:
mode=4;
break;
case 6:
mode=5;
break;
case 7:
mode=6;
break;
case 8:
if(mode<6){
mode+=1;
}else{mode=0;}
break;
default :
break;
}
}
}
}
/*------------------------------------------------
------------------------------------------------*/
void DelayUs2x(unsigned char t)
{
while(--t);
}
void DelayMs(unsigned char t)
{
while(t--)
{
DelayUs2x(245);
DelayUs2x(245);
}
}
/*------------------------------------------------
------------------------------------------------*/
void Init_Timer0(void)
{
TMOD |= 0x01;
TH0=(65536-20000)/256;
TL0=(65536-20000)%256;
EA=1;
ET0=1;
TR0=1;
}
/*------------------------------------------------
------------------------------------------------*/
void Timer0_isr(void) interrupt 1 using 1
{
switch(mode){
case 0:
TH0=(65536-20000)/256;
TL0=(65536-20000)%256;
if(j<2){
j++;
}else{j=0;}
if(j==0){
if(i<11){i++;}else{i=0;}
if(i<6){
LED2=1;
LED3=1;
LED0=!LED0;
LED1=!LED1;
}else{
LED0=1;
LED1=1;
LED2=!LED2;
LED3=!LED3;
}
}
break;
case 1:
TH0=(65536-20000)/256;
TL0=(65536-20000)%256;
if(j<4){
j++;
}else{j=0;}
if(j==0){
if(i<11){i++;}else{i=0;}
if(i<6){
LED2=1;
LED3=1;
LED0=!LED0;
LED1=!LED1;
}else{
LED0=1;
LED1=1;
LED2=!LED2;
LED3=!LED3;
}
}
break;
case 2:
TH0=(65536-20000)/256;
TL0=(65536-20000)%256;
if(j<2){
j++;
}else{j=0;}
if(j==0){
if(i<11){i++;}else{i=0;}
if(i<6){
LED0=!LED0;
LED1=!LED1;
LED2=!LED2;
LED3=!LED3;
}else{
LED0=1;
LED1=1;
LED2=1;
LED3=1;
}
}
break;
case 3:
TH0=(65536-20000)/256;
TL0=(65536-20000)%256;
if(j<2){
j++;
}else{j=0;}
if(j==0){
if(i<24){i++;}else{i=0;}
if(i<6){
LED2=1;
LED3=1;
LED0=!LED0;
LED1=!LED1;
}else if(i<12){
LED0=1;
LED1=1;
LED2=1;
LED3=1;
}else if(i<18){
LED0=1;
LED1=1;
LED2=!LED2;
LED3=!LED3;
}else{
LED0=1;
LED1=1;
LED2=1;
LED3=1;
}
}
break;
case 4:
TH0=(65536-20000)/256;
TL0=(65536-20000)%256;
if(j<4){
j++;
}else{j=0;}
if(j==0){
if(i<24){i++;}else{i=0;}
if(i<6){
LED2=1;
LED3=1;
LED0=!LED0;
LED1=!LED1;
}else if(i<12){
LED0=1;
LED1=1;
LED2=1;
LED3=1;
}else if(i<18){
LED0=1;
LED1=1;
LED2=!LED2;
LED3=!LED3;
}else{
LED0=1;
LED1=1;
LED2=1;
LED3=1;
}
}
break;
case 5:
TH0=(65536-20000)/256;
TL0=(65536-20000)%256;
if(j<4){
j++;
}else{j=0;}
if(j==0){
if(i<11){i++;}else{i=0;}
if(i<6){
LED0=!LED0;
LED1=!LED1;
LED2=!LED2;
LED3=!LED3;
}else{
LED0=1;
LED1=1;
LED2=1;
LED3=1;
}
}
break;
case 6:
break;
}
}
void Init_LED(void)
{
P1=0xFF;
}
unsigned char KeyScan(void)
{
/********************************************************/
if(!KEY1)
{
DelayMs(10);
if(!KEY1)
{
while(!KEY1);
return 1;
}
}else if(!KEY2)
{
DelayMs(10);
if(!KEY2)
{
while(!KEY2);
return 2;
}
}else if(!KEY3)
{
DelayMs(10);
if(!KEY3)
{
while(!KEY3);
return 3;
}
}else if(!KEY4)
{
DelayMs(10);
if(!KEY4)
{
while(!KEY4);
return 4;
}
}else if(!KEY5)
{
DelayMs(10);
if(!KEY5)
{
while(!KEY5);
return 5;
}
}else if(!KEY6)
{
DelayMs(10);
if(!KEY6)
{
while(!KEY6);
return 6;
}
}else if(!KEY7)
{
DelayMs(10);
if(!KEY7)
{
while(!KEY7);
return 7;
}
}else if(!KEY8)
{
DelayMs(10);
if(!KEY8)
{
while(!KEY8);
return 8;
}
}else{return 0;}
}
|