刚注册发现论坛要激活,想做个stm32的东西,可快递都放假了,没办法,手头没东西,就随便发一个吧,这个是去年这个时候帮别人写的,大概就是四个按键控制单片机,按一下按键点亮一个对应led,再按一下灯灭,其中一个按亮灯后,再按其他按键灯灭,然后长按其他按键单片机重启,各位可能觉得没什么难度,当时刚学,写的时候里面的逻辑挺绕的,所以就发这个吧
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long
typedef struct key
{
uchar key1;
uchar key2;
uchar key3;
ulong flag;
uchar count;
} keycon;
keycon keyarray[8];
ulong n,at,m;
void inittimer0(void);
void delay(uint);
void init(keycon * );
uchar scankeys(void);
void openled(keycon * ,uchar);
void doled(keycon * );
void ledtoled(keycon * ,uchar);
//void closetimer1(void);
//void inittimer1(void);
void main()
{
uchar i,j;
init(keyarray);
inittimer0();
P3=0xff;
j=9;
while(P3!=0xff);
while(1)
{
j=scankeys();
if(j<8)
{
openled(keyarray,j);
}
while(P3!=0xff);
while(P3==0xff)
{
for(i=0;i<8;i++)
{
if((keyarray[i].flag)<n)
{
ledtoled(keyarray,i);
}
at&=keyarray[i].flag;
}
if(at==0xffff)
n=0;
}
}
}
uchar scankeys()
{
uchar keys;
if(P3!=0xff)
{
delay(5);
if(P3!=0xff)
{
keys=P3;
switch(keys)
{
case 0xfe : return 0 ; break ;
case 0xfd : return 1 ; break ;
case 0xfb : return 2 ; break ;
case 0xf7 : return 3 ; break ;
case 0xef : return 4 ; break ;
case 0xdf : return 5 ; break ;
case 0xbf : return 6 ; break ;
case 0x7f : return 7 ;
}
}
}
else
return 9;
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void inittimer0()
{
TMOD=0x01;
TH0=(65535-60000)/256;
TL0=(65535-60000)%256;
EA=1;
ET0=1;
TR0=1;
}
/*
void inittimer1()
{
TMOD=0x01;
TH1=(65535-50000)/256;
TL1=(65535-50000)%256;
EA=1;
ET1=1;
TR1=1;
m=0;
}
*/
void timer0() interrupt 1
{
TH0=5535/256;
TL0=5535%256;
n++;
}
/*
void closetimer1()
{
TR1=0;
}
void timer1() interrupt 3
{
TH1=(65535-50000)/256;
TL1=(65535-50000)%256;
m++;
}
*/
void init(keycon * keyarray)
{
uchar pio1,pio2,poi;
uchar i;
pio1=0xff;
pio2=0xff;
poi=0xfe;
for(i=0;i<8;i++)
{
keyarray[i].key1=pio1;
keyarray[i].key2=pio2;
keyarray[i].key3=poi;
keyarray[i].count=0;
keyarray[i].flag=0xffff;
poi=(poi<<1)|1;
}
doled(keyarray);
}
void openled(keycon * keyarray,uchar i)
{
keyarray[i].flag=1000+n;
keyarray[i].count++;
if(keyarray[i].count==2)
{
/* if((P3!=0xff)&&(keyarray[i].flag>n))
{
inittimer1();
}
while(P3!=0xff);
closetimer1();
if(m>=20) */
{
// (*(void(*)()) 0x0000)();
(*(void (*)())0x000)();
}
}
else
{
keyarray[i].key1=keyarray[i].key3;
keyarray[i].key2=0xff;
}
doled(keyarray);
}
void doled(keycon * keyarray)
{
uchar temp1,temp2;
uchar i;
temp1=0xff;
temp2=0xff;
for(i=0;i<8;i++)
{
temp1=temp1&keyarray[i].key1;
temp2=temp2&keyarray[i].key2;
}
P1=temp1;
P2=temp2;
}
void ledtoled(keycon * keyarray,uchar i)
{
// if(keyarray[i].flag<n)
{
keyarray[i].flag=0xffff;
keyarray[i].count=1;
keyarray[i].key1=0xff;
keyarray[i].key2=keyarray[i].key3;
}
doled(keyarray);
}
200字以内,仅用于支线交流,主线讨论请采用回复功能。