Serial communication of 51 single-chip microcomputer (III.)

Source: Internet
Author: User

Serial communication of 51 single-chip microcomputer (III.)

has been read 47 times 2015-12-29 00:21 |  Personal Category: 51 single-chip microcomputer | single chip microcomputer, communication

Use the serial port to send and receive simultaneously operation: The computer displays the result source program:

/*
Project name: Serial send and receive experiment
Project content: Serial sending and receiving at the same time,
The computer sends data to the microcontroller to control the P0 port, thus controlling
LED light off, single-chip computer to send data to computers, computers
Show up
Date: December 23, 2015
Project leader: YUAN
*/
#include <reg52.h>
typedef Unsigned CharUChar8;
typedef Unsigned IntUInt16;

void Delayms(UInt16Valms);
void Uartinit(void);Serial initialization
void Uartreceivedata(void);Receive data
void Uartsenddata(void);The serial port sends the data, namely the monolithic computer sends the data
voidMain(void)
{
/* Initialization: Includes timer interrupt settings,
Serial port register settings, baud rate setting */
Uartinit();
While(1)
{
/* Receive the data sent by the computer and send it to the LED control port */
Uartreceivedata();
Uartsenddata();
Delayms(500);
}
}
void Delayms(UInt16Valms)
{
UInt16 Uival,Ujval;
For(Uival=0;Uival<Valms;Uival++)
For(Ujval=0;Ujval<113;Ujval++);
}
void Uartinit(void)
{
Tmod&= 0x0f; Just change what you want to change.
Tmod|= 0x20;Set timer 0 to working mode 2
TL1= 0xfd;
TH1= 0xfd;Set baud rate to 9600bps
TR1= 1;Allow T1 to start counting

SCON&= 0x5f;
SCON|= 0x50;Set the serial port to work in mode 1, allowing data to be received
}
void Uartreceivedata(void)
{
If the receive interrupt is 1, then there is a send
If(RI)
{
RI= 0;
P0=Sbuf;//send data to P0 control led
}
}
void uartsenddata ( void)
{
static uChar8 num = 0 //send data and accumulate
sbuf = Num num++;
ti = 0
}
/span>

Collection sharing invitation to use props| edit | Delete |

Serial communication of 51 single-chip microcomputer (III.)

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.