#include<reg52.h>#define uchar unsigned char#define uint unsigned intsbit s1=P3^0;sbit s2=P3^1;
void delay(uint z) //延迟函数{uint x,y;for(x=100;x>0;x--)for(y=z;y>0;y--);}uchar keyscan() //按键{uchar key_value,temp;P3=0xff;temp=P3;if(temp!=0xff){delay(5);if(temp!=0xff){temp=P3;switch(temp){case 0xfe:key_value=P0++;//按s1键LED右移break;case 0xfd:key_value=P0--;//按s2键LED左移break;}while(P3!=0xff);delay(5);while(P3!=0xff);}}return key_value;}void main(){P2=((P2&0x1f)|0x80);P0=0xfe;keyscan();P2&=0x1f;}
void delay(uint z) //延迟函数{uint x,y;for(x=100;x>0;x--)for(y=z;y>0;y--);}uchar keyscan() //按键{uchar key_value,temp;P3=0xff;temp=P3;if(temp!=0xff){delay(5);if(temp!=0xff){temp=P3;switch(temp){case 0xfe:key_value=P0++;//按s1键LED右移break;case 0xfd:key_value=P0--;//按s2键LED左移break;}while(P3!=0xff);delay(5);while(P3!=0xff);}}return key_value;}void main(){P2=((P2&0x1f)|0x80);P0=0xfe;keyscan();P2&=0x1f;}