Generally, the dual-host communication of single-chip microcomputer supports the following four methods:
TTL level communication (dual-host serial port direct interconnection), RS-232C communication, RS-422A communication, RS-485 communication.
For TTL-level communication, the txd of mcu a is connected to the rxd of mcu B, and the rxd of mcu B is connected to the txd of mcu B. However, the ground wires of the two single-chip microcomputer must be co-located, that is, the power wires of their systems must be connected together.
Host buttons
# Include <reg52.h>
# Define uchar unsigned char
# Define uint unsigned int
Void delayms (uint XMS)
{
Uint I, J;
For (I = XMS; I> 0; I --)
For (j = 110; j> 0; j --);
}
Void send (uchar key_num)
{
Sbuf = key_num;
While (! Ti );
Ti = 0;
}
Void matrixkeyscan ()
{
Uchar temp, key;
P3 = 0xfe;
Temp = P3;
Temp = temp & 0xf0;
If (temp! = 0xf0)
{
Delayms (10 );
Temp = P3;
Temp = temp & 0xf0;
If (temp! = 0xf0)
{
Temp = P3;
Switch (temp)
{
Case 0xEE:
Key = 0;
Break;
Case 0xde:
Key = 1;
Break;
Case 0xbe:
Key = 2;
Break;
Case 0x7e:
Key = 3;
Break;
}
While (temp! = 0xf0)
{
Temp = P3;
Temp = temp & 0xf0;
}
Send (key );
}
}
P3 = 0xfd;
Temp = P3;
Temp = temp & 0xf0;
If (temp! = 0xf0)
{
Delayms (10 );
Temp = P3;
Temp = temp & 0xf0;
If (temp! = 0xf0)
{
Temp = P3;
Switch (temp)
{
Case 0xed:
Key = 4;
Break;
Case 0xdd:
Key = 5;
Break;
Case 0xbd:
Key = 6;
Break;
Case 0x7d:
Key = 7;
Break;
}
While (temp! = 0xf0)
{
Temp = P3;
Temp = temp & 0xf0;
}
Send (key );
}
}
P3 = 0xfb;
Temp = P3;
Temp = temp & 0xf0;
If (temp! = 0xf0)
{
Delayms (10 );
Temp = P3;
Temp = temp & 0xf0;
If (temp! = 0xf0)
{
Temp = P3;
Switch (temp)
{
Case 0xeb:
Key = 8;
Break;
Case 0xdb:
Key = 9;
Break;
Case 0xbb:
Key = 10;
Break;
Case 0x7b:
Key = 11;
Break;
}
While (temp! = 0xf0)
{
Temp = P3;
Temp = temp & 0xf0;
}
Send (key );
}
}
P3 = 0xf7;
Temp = P3;
Temp = temp & 0xf0;
If (temp! = 0xf0)
{
Delayms (10 );
Temp = P3;
Temp = temp & 0xf0;
If (temp! = 0xf0)
{
Temp = P3;
Switch (temp)
{
Case 0xe7:
Key = 12;
Break;
Case 0xd7:
Key = 13;
Break;
Case 0xb7:
Key = 14;
Break;
Case 0x77:
Key = 15;
Break;
}
While (temp! = 0xf0)
{
Temp = P3;
Temp = temp & 0xf0;
}
Send (key );
}
}
}
Void main ()
{
Tmod = 0x02;
Th1 = 0xfd;
TL1 = 0xfd;
Tr1 = 1;
Sm0 = 0;
SM1 = 1;
Ea = 1;
Es = 1;
While (1)
{
Matrixkeyscan (); // keeps calling the keyboard Scanner
}
}
Slave display
# Include <reg52.h>
# Define uchar unsigned char
# Define uint unsigned int
Sbit Dula = P2 ^ 6;
Sbit wela = P2 ^ 7;
Uchar code table [] = {
0x3f, 0x06, 0x5b, 0x4f,
0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c,
0x39, 0x5e, 0x79,0x71
};
Void display (uchar num)
{
P0 = table [num];
Dula = 1;
Dula = 0;
}
Void main ()
{
Tmod = 0x20;
Th1 = 0xfd;
TL1 = 0xfd;
Tr1 = 1;
Ren = 1;
Sm0 = 0;
SM1 = 1;
Ea = 1;
Es = 1;
P0 = 0xc0;
Wela = 1;
Wela = 0;
While (1 );
}
Void Ser () interrupt 4
{
Uchar;
Ri = 0;
A = sbuf;
Display ();
}
Single-Chip dual-machine communication