第九届彩灯控制器,题目要求稍微改了一下,换成不同按键控制,这样可以稍微简单一点,但是发现在进入设置界面时,mode的自加只加到一,time显示不正确,请诸位看看怎么回事?
ps:目前还是小白,感觉不怎么进门
#include "STC15F2K60S2.h"
#include "intrins.h"
#define a0 unsigned char
sbit buzzer=P0^6;
sbit relay=P0^4;
a0 code duan[]={0x3f,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00}; //0-9
a0 code wei[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
#define KEY P3
#define NO_KEY 0xff
#define KEY_STATE0 0
#define KEY_STATE1 1
#define KEY_STATE2 2
unsigned char Key_Scan()
{
static unsigned char key_state=KEY_STATE0;
a0 key_value=0,key_temp;
a0 key1,key2;
P30=0;P31=0;P32=0;P33=0;P34=1;P35=1;P42=1;P44=1;
if(P44==0) key1=0x70;
if(P42==0) key1=0xb0;
if(P35==0) key1=0xd0;
if(P34==0) key1=0xe0;
if((P34==1)&&(P35==1)&&(P42==1)&&(P44==1)) key1=0xf0;
P30=1;P31=1;P32=1;P33=1;P34=0;P35=0;P42=0;P44=0;
if(P30==0) key2=0x0e;
if(P31==0) key2=0x0d;
if(P32==0) key2=0x0b;
if(P33==0) key2=0x07;
if((P30==1)&&(P31==1)&&(P32==1)&&(P33==1)) key2=0x0f;
key_temp=key1|key2;
switch(key_state)
{
case KEY_STATE0:
if(key_temp!=NO_KEY) key_state=KEY_STATE1;
break;
case KEY_STATE1:
if(key_temp==NO_KEY) key_state=KEY_STATE0;
else
{
switch(key_temp)
{
case 0x77: key_value=4;break;
case 0x7b: key_value=5;break;
case 0x7d: key_value=6;break;
case 0x7e: key_value=7;break;
case 0xb7: key_value=8;break;
case 0xbb: key_value=9;break;
case 0xbd: key_value=10;break;
case 0xbe: key_value=11;break;
case 0xd7: key_value=12;break;
case 0xdb: key_value=13;break;
case 0xdd: key_value=14;break;
case 0xde: key_value=15;break;
case 0xe7: key_value=16;break;
case 0xeb: key_value=17;break;
case 0xed: key_value=18;break;
case 0xee: key_value=19;break;
}
key_state=KEY_STATE2;
}
break;
case KEY_STATE2:
if(key_temp==NO_KEY)
{
key_state=KEY_STATE0;
}
break;
}
return key_value;
}
void Timer0Init(void) //1ms@11.0592MHz
{
AUXR &= 0x7F;
TMOD &= 0xF0;
TL0 = 0x66;
TH0 = 0xFC;
TF0 = 0;
TR0 = 1;
ET0=1;
EA=1;
}
void Delay500ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 22;
j = 3;
k = 227;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
bit key_flag;
a0 set_display[8];
a0 anjian_index;
a0 mode=0;
a0 time=400;
a0 s7_count=0;
a0 shuzu[8];
a0 modee=0;
void main()
{
a0 key_val=NO_KEY;
P2=0xa0;P0=0x00;P2=0x00;
P2=0x80;P0=0xff;P2=0x00;
Timer0Init();
while(1)
{
set_display[0]=0x40;
set_display[1]=duan[mode];
set_display[2]=0x40;;
set_display[3]=0x00;
set_display[4]=0x00;;
set_display[5]=duan[time/100];
set_display[6]=duan[time/10];
set_display[7]=duan[time%10];
shuzu[0]=0x40;
shuzu[1]=duan[modee];
shuzu[2]=0x40;
shuzu[3]=0x40;
shuzu[4]=0x40;
shuzu[5]=0x40;
shuzu[6]=0x40;
shuzu[7]=0x40;
if(mode==1)
{
unsigned char i;
while(1)
{
P2=0x80;
P0=~(0x01<<i);//?è×óò????ùè?·'??×?oóê?óòò?
P2=0X00;
i++;
if(i==8) i=0;
Delay500ms();
}
}
if(mode==2)
{
unsigned char i;
while(1)
{
P2=0x80;
P0=~(0x80>>i);//?èóòò????ùè?·'??×?oóê?×óò?
P2=0X00;
i++;
if(i==8) i=0;
Delay500ms();
}
}
if(mode==3)
{
P2=0x80; P0=0x7e;P2=0x00;//1 8
Delay500ms();
P2=0x80; P0=0xbd;P2=0x00;//2 7
Delay500ms();
P2=0x80; P0=0xdb;P2=0x00;//3 6
Delay500ms();
P2=0x80; P0=0xe7;P2=0x00;//4 5
Delay500ms();
P2=0x80; P0=0x7e;P2=0x00;//1 8
Delay500ms();
}
if(mode==4)
{
P2=0x80; P0=0xe7;P2=0x00;//4 5
Delay500ms();
P2=0x80; P0=0xdb;P2=0x00;//3 6
Delay500ms();
P2=0x80; P0=0xbd;P2=0x00;// 2 7
Delay500ms();
P2=0x80; P0=0x7e;P2=0x00;//1 8
Delay500ms();
P2=0x80; P0=0xe7;P2=0x00;// 4 5
Delay500ms();
}
if(key_flag)
{
key_flag=0;
key_val=Key_Scan();
switch(key_val)
{
case 4: mode++;
if(mode==5)
{
mode=1;
}
break;
case 5: mode--;
if(mode==0)
{
mode=4;
}
break;
case 6: anjian_index++;
if(anjian_index==2)
{
anjian_index=0;
}
break;
case 7: s7_count++;
if(s7_count==2)
{
s7_count=0;
P2=0x80;P0=0xff;P2=0x00;
}
break;
case 8: time=time+100;
if(time>1200)
{
time=1200;
}
break;
case 9: time=time-100;
if(time<400)
{
time=400;
}
break;
case 10:break;
case 11:break;
case 12:break;
case 13:break;
case 14:break;
case 15:break;
case 16:break;
case 17:break;
case 18:break;
case 19:break;
}
}
}
}
void timer0() interrupt 1 using 1
{
static int key_count=0,smg_count=0,i=0,time_count=0,blink_count=0;
key_count++;smg_count++;time_count++;blink_count++;
if(key_count==1)
{
key_count=0;
key_flag=1;
}
if(smg_count==3)
{
smg_count=0;
P2=0xc0;P0=0X00;P2=0X00;
if(anjian_index)
{
P2=0xe0;P0=~set_display[i];P2=0x00;
}
else
{
P2=0xe0;P0=~shuzu[i];P2=0x00;
}
P2=0xc0;P0=wei[i];P2=0X00;
i++;
if(i==8) i=0;
}
} |