题目:通过甲单片机来控制乙机
要求:四个键盘中有一个按键按下时,甲乙二机中的对应的流水灯闪烁,如果有其他的键按下,则转向其他的对应的灯闪烁。 甲机代码:
#include #include
#define uchar unsigned char #define uint unsigned int void delay(uchari); voidkeyscan( );
voidchuankou(ucharrr); voidshanshuo(ucharww); void delay(uchari) { ucharj,k;
for(j=i;j>0;j--)
for(k=500;k>0;k--); }
void main() {
P0=0xff; SCON=0X40;
TMOD=0x20; PCON=0x00; TH1=0xfd; TL1=0xfd; TI=0; TR1=1; while(1) {
keyscan(); } }
voidkeyscan() { uchar temp; P1=0xff; temp=P1;
temp=temp&0xff; if(temp!=0xff) { delay(10);
temp=P1; if(temp!=0xff) { switch(temp)
{ case 0xfe:
chuankou('A'); shanshuo(temp); break; case 0xfd: chuankou('B');
shanshuo(temp); break;
case 0xfb: chuankou('C'); shanshuo(temp); break; case 0xf7: chuankou('D'); shanshuo(temp);
break; }
while(temp!=0xff)
}
}
{
temp=P1; temp=temp&0xff; } }
voidshanshuo(ucharaa) { ucharss; while(1) {
P0=aa; delay(300); P0=0xff; delay(300); ss=P1; if(ss!=0xff) {
delay(10); ss=P1; if(ss!=0xff) {
switch(ss) {
case 0xfe: chuankou('A'); shanshuo(ss); break; case 0xfd:
chuankou('B'); shanshuo(ss);
break; case 0xfb:
chuankou('C'); shanshuo(ss); break; case 0xf7: chuankou('D');
shanshuo(ss); break; }
}
} } }
voidchuankou(uchar F) {
SBUF=F; while(!TI); TI=0; }
乙机代码: #include #include #define uchar unsigned char #define uint unsigned int voidshanshuo(uintss); void delay(uchari); void delay(uchari) {
ucharj,k;
for(j=i;j>0;j--)
for(k=500;k>0;k--); }
void main() {
uchar a; SCON=0X50; TMOD=0x20; PCON=0x00; TH1=0xfd; TL1=0xfd; RI=0; TR1=1; P0=0xff; while(1) { if(RI) {
RI=0; switch(SBUF)
{ case 'A': a=0xfe;
break; case 'B':
a=0xfd; shanshuo(a); break; case 'C': a=0xfb; shanshuo(a);
break; case 'D': a=0xf7; shanshuo(a); break; }
}
} }
voidshanshuo(uintss) {
uchar a; a=ss; while(1) { P0=ss; delay(300); P0=0xff; delay(300); if(RI) {
RI=0; switch(SBUF)
{ case 'A':
a=0xfe; shanshuo(a);
break; case 'B':
a=0xfd;
shanshuo(a); break;
case 'C':
a=0xfb; shanshuo(a);
}
case 'D':
a=0xf7; shanshuo(a); break; }
} }
题目:通过甲单片机来控制乙机
要求:四个键盘中有一个按键按下时,甲乙二机中的对应的流水灯闪烁,如果有其他的键按下,则转向其他的对应的灯闪烁。 甲机代码:
#include #include
#define uchar unsigned char #define uint unsigned int void delay(uchari); voidkeyscan( );
voidchuankou(ucharrr); voidshanshuo(ucharww); void delay(uchari) { ucharj,k;
for(j=i;j>0;j--)
for(k=500;k>0;k--); }
void main() {
P0=0xff; SCON=0X40;
TMOD=0x20; PCON=0x00; TH1=0xfd; TL1=0xfd; TI=0; TR1=1; while(1) {
keyscan(); } }
voidkeyscan() { uchar temp; P1=0xff; temp=P1;
temp=temp&0xff; if(temp!=0xff) { delay(10);
temp=P1; if(temp!=0xff) { switch(temp)
{ case 0xfe:
chuankou('A'); shanshuo(temp); break; case 0xfd: chuankou('B');
shanshuo(temp); break;
case 0xfb: chuankou('C'); shanshuo(temp); break; case 0xf7: chuankou('D'); shanshuo(temp);
break; }
while(temp!=0xff)
}
}
{
temp=P1; temp=temp&0xff; } }
voidshanshuo(ucharaa) { ucharss; while(1) {
P0=aa; delay(300); P0=0xff; delay(300); ss=P1; if(ss!=0xff) {
delay(10); ss=P1; if(ss!=0xff) {
switch(ss) {
case 0xfe: chuankou('A'); shanshuo(ss); break; case 0xfd:
chuankou('B'); shanshuo(ss);
break; case 0xfb:
chuankou('C'); shanshuo(ss); break; case 0xf7: chuankou('D');
shanshuo(ss); break; }
}
} } }
voidchuankou(uchar F) {
SBUF=F; while(!TI); TI=0; }
乙机代码: #include #include #define uchar unsigned char #define uint unsigned int voidshanshuo(uintss); void delay(uchari); void delay(uchari) {
ucharj,k;
for(j=i;j>0;j--)
for(k=500;k>0;k--); }
void main() {
uchar a; SCON=0X50; TMOD=0x20; PCON=0x00; TH1=0xfd; TL1=0xfd; RI=0; TR1=1; P0=0xff; while(1) { if(RI) {
RI=0; switch(SBUF)
{ case 'A': a=0xfe;
break; case 'B':
a=0xfd; shanshuo(a); break; case 'C': a=0xfb; shanshuo(a);
break; case 'D': a=0xf7; shanshuo(a); break; }
}
} }
voidshanshuo(uintss) {
uchar a; a=ss; while(1) { P0=ss; delay(300); P0=0xff; delay(300); if(RI) {
RI=0; switch(SBUF)
{ case 'A':
a=0xfe; shanshuo(a);
break; case 'B':
a=0xfd;
shanshuo(a); break;
case 'C':
a=0xfb; shanshuo(a);
}
case 'D':
a=0xf7; shanshuo(a); break; }
} }