51 single-chip computer serial communication

Source: Internet
Author: User

First, learn some basic concepts.

RS232 Level: Logic 1 (MARK) =-3v~-15v, Logic 0 (SPACE) =+3~+15v

TTL level: + + is equivalent to logic "1", 0V equivalent to logic "0"

TXD: Send data transmit the P31 of single chip microcomputer

RXD: Receiving data, P30 of MCU

Baud rate: The rate at which a single-chip or computer communicates at the serial port, defined as the number of bits transmitted in binary code per second, unit BTS

UART: Universal Asynchronous Transceiver. 51 MCU serial Port is a programmable full-duplex communication interface, with the full function of the UART, the data can be sent and received at the same time, it can also be used as a synchronous shift register.

SBUF: Serial Data buffer Register

Often use communication Mode 1 (10-bit data asynchronous communication, baud rate variable, timer 1), so must master ~ ~ ~

Timer 1 Common mode 2, when overflow, the single-chip opportunity to automatically install the initial value, mode 1 need to manually install the initial value

Attached: Common baud rate initial value table




/*		send character  @L2  2016/08/08 */
#include <reg51.h>
unsigned char word[19] = "nice to meet you!";
void Init ();
void delay10ms (unsigned int c);

void Main ()
{
	unsigned char i;
	Init ();
	while (1)
	{for
		(i=0; i<19; i++)
		{
			sbuf = word[i];   The data to be sent is put into the Send register while
			(! TI);		  Wait for send data to complete: When the serial send 8th bit of data ends, the internal hardware is 1.
			ti=0;			  Clear send completion flag bit: Software clear 0
		}
		delay10ms;		  Delay and then send
	}
}

void init ()				  //Set serial port
{
	scon=0x50;			Set to Working mode 1   SMO SM1 SM2 ren=0 1 0 1  
	tmod=0x20;			Set Timer 1 working mode 2
	pcon=0x80;			Baud rate doubled	smod=1
	th1=0xe6;		    The counter initial value is set, where the baud rate is 2400
	tl1=0xe6;	Es=1;						Open receive interrupt
//	ea=1;						Open total interrupt
	tr1=1;					    Open counter
}


void delay10ms (unsigned int c)   //Error 0us
{
    unsigned char a, b;
    for (; c>0;c--)
	{for
		(b=38;b>0;b--)
		{for
			(a=130;a>0;a--);       
}}}


			the/** will receive a send back to the computer. @L2  2016/08/08 		**/

#include <reg51.h>

void init ()			  //Set serial port
{
	scon=0x50;			Set to Working mode 1
	tmod=0x20;			Set the counter mode of Operation 2
	pcon=0x80;			Baud rate doubled
	th1=0xf3;		    The counter initial value is set, note that the baud rate is 4800
	tl1=0xf3;
	Es=1;						Open receive interrupt
	ea=1;						Open total interrupt
	tr1=1;					    Open counter
}

void Usart () Interrupt 4	 //serial port interrupt, serial ports complete a frame character send/Receive after
{
	unsigned char receivedata;

	receivedata=sbuf;//go out to receive the data
	RI = 0;           Clears the receive interrupt flag bit
	sbuf=receivedata;//puts the received data into the Send register while
	(! TI);		  Waiting to send data to complete
	ti=0;			  Clear send completion flag bit
}

void Main ()
{
	init ();
	while (1)
	{
	}
}






Related Article

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.