Serial Communication Control

Source: Internet
Author: User

Http://hi.baidu.com/%D7%F6%B6%F8%C2%DB%B5%C0/blog/item/725053edc5cec62d27979143.html

About the serial communication between two single-chip microcomputer.

The requirements are as follows:
1. If the p1.0 port of the lower computer enters a low level, 0x01 is sent, and the upper computer outputs a high level in its p1.0 port;
2. If the p1.1 port of the lower computer enters a low level, 0x03 will be sent, and the upper computer will output a high level on its p1.1 port;
3. Only when both ports of the lower computer change to a high level can 0x07 be sent. The upper computer outputs a low level in both the p1.0 and p1.1 ports.
Reward score: 10-resolution time:
//-----------------------------------------------------------
Best Answer:
As required, the Proteus simulation circuit diagram is as follows:


Picture connection: http://hi.baidu.com/%D7%F6%B6%F8%C2%DB%B5%C0/album/item/d9581a5108d990171238c244.html#IMG=d9581a5108d990171238c244

In the figure, the upper computer completes serial reception and outputs high and low levels based on the received data. The program used is as follows:
// ================================================ ============
# Include <reg51.h>

# Define xtal 11059200 // CPU crystal oscillator frequency
# Define baudrate 9600 // communication baud rate
# Define uchar unsigned char
# Define uint unsigned int

Sbit k1 = p1 ^ 0; // define the port
Sbit k2 = p1 ^ 1;

Void Init (void)
{
Scon = 0x50; // sets the working mode of the serial port.
Pcon & = 0x00; // The baud rate is not doubled.
Tmod = 0x20; // timer 1 works in 8-bit auto-Reload mode to generate a baud rate
Th1 = (unsigned char) (256-(xtal/(32l * 12l * baudrate )));
TL1 = (unsigned char) (256-(xtal/(32l * 12l * baudrate); // T1 assigns an initial value
Tr1 = 1; // start timer 1
Ie = 0x00; // disable any interruptions
// Ea = 1; // The total number of interruptions allowed
}
//-----------------------------------------------------------
Void main (void)
{
Init ();
While (1 ){
While (Ri = 0 );
Ri = 0;
If (sbuf = 0x01) k1 = 1;
If (sbuf = 0x03) k2 = 1;
If (sbuf = 0x07 ){
K1 = 0;
K2 = 0;
}}}
// ================================================ ============

In the figure, the lower computer completes input detection and sends different serial data. The program used is as follows:
// ================================================ ============
# Include <reg51.h>

# Define xtal 11059200 // CPU crystal oscillator frequency
# Define baudrate 9600 // communication baud rate
# Define uchar unsigned char
# Define uint unsigned int

Sbit k1 = p1 ^ 0; // define the port
Sbit k2 = p1 ^ 1;
//-----------------------------------------------------------
Void Init (void)
{
Scon = 0x50; // sets the working mode of the serial port.
Pcon & = 0x00; // The baud rate is not doubled.
Tmod = 0x20; // timer 1 works in 8-bit auto-Reload mode to generate a baud rate
Th1 = (unsigned char) (256-(xtal/(32l * 12l * baudrate )));
TL1 = (unsigned char) (256-(xtal/(32l * 12l * baudrate); // T1 assigns an initial value
Tr1 = 1; // start timer 1
Ie = 0x00; // disable any interruptions
// Ea = 1; // The total number of interruptions allowed
}
//-----------------------------------------------------------
Void main (void)
{
Bit t_1 = 0, T_2 = 0, T_3 = 0;
Init ();
//--------------------------------------------------
While (1 ){
If (k1 = 0) & (t_1 = 0 )){
T_1 = 1;
T_3 = 0;
Sbuf = 0x01;
While (Ti = 0 );
Ti = 0;
}
//-------------------------------------
If (k2 = 0) & (T_2 = 0 )){
T_2 = 1;
T_3 = 0;
Sbuf = 0x03;
While (Ti = 0 );
Ti = 0;
}
//-------------------------------------
If (k1 = 1) & (k2 = 1) & (T_3 = 0 )){
T_1 = 0;
T_2 = 0;
T_3 = 1;
Sbuf = 0x07;
While (Ti = 0 );
Ti = 0;
}}}
// ================================================ ============
The program running screen is displayed in the illustration.
Original question Web site: http://zhidao.baidu.com/question/198788419.html? Fr = im00008
The questioner's comment on the answer: Thank you for your help!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.