51 Single chip microcomputer learning notes "Six"--serial communication experiment

Source: Internet
Author: User

I. Serial communication Fundamentals 1. Serial communication
    • Serial traffic divides the data bytes into one-bit form and transmits them one at a transmission line. Single-chip microcomputer adopts serial communication. Features are: Less transmission lines, low long-distance transmission costs, but data transmission control complex.
    • Transmission direction of serial communication: simplex (data transmission can only be carried in one direction), run duplex (transmit data and accept data distribution), full duplex (transmission and accept synchronous).
    • MCU uses asynchronous communication, sending and receiving devices using their own clock frequency, easy to achieve, but the transmission efficiency is not high, data format
2.max232
    • The experiment can convert the serial port level ( -12V~+12V) of the computer to single chip and TTL level by MAX232 level conversion chip. Circuit schematic diagram is as follows
3.80c51 Serial Port


Write data to the send buffer sbuf, two physically independent receive, send buffer sbuf, they occupy the same address 99H, the receiver is a double buffer structure, send buffer, because the CPU is active when sending, does not produce overlapping errors.

4. Working Mode Register Scon
bit D7 D6 D5 D4 D3 D2 D1 D0
Function SM0 SM1 SM2 REN TB8 RB8 T1 R1
    • Introduced
    • RI: Receive interrupt flag bit
      At the end of the reception, 1 of the hardware is placed, and the interrupt request is made to the CPU. (To be reset by software)
    • TI: Send interrupt flag bit
      At the end of the send, the hardware is set to 1 and an interrupt request is made to the CPU. To be reset by software)
    • TB8: Used to store the 9th bit of the send.
    • RB8: Used to store the received 9th bit.
    • REN: is a serial receive allow bit
      0 o'clock: Allow serial reception
      1 o'clock: Serial reception is not allowed
5. Working Mode Register Pcon
    • The Pcon is not bit addressable, that is, it cannot directly manipulate the Smod to operate the Pcon register directly.
    • Smod: Is the baud rate double the selection bit.
      0 o'clock: Baud rate does not double.
      1 o'clock: Double the baud rate.
6. Serial port operation step: First step: Set baud rate, set timer T1 to work Mode 2 (set Tmod Register), give counter initial value (mode 2 will reload automatically) Second step: Set the serial port working mode, set the Scon (if allowed). The third step: if the interrupt mode is used, then the corresponding interrupts and total interrupts are turned on. Fourth step: Turn on the timer T1 and start generating the baud rate. Tcon Set TRX. Two. Experimental routine 1. Description: This experiment uses the serial debugging assistant to send data to the computer, and returns, the experiment does not need the wiring. 2. The program source code is as follows:
/************************************** > File Name: Serial communication experiment > AUTHOR:PENGSHP > Mail: [email protected] & Gt date:2015 July 22 ***************************************/#include <reg51.h>#define UCHAR unsigned char#define UINT unsigned intUchar Flag,i;/ * Serial initialization function * /voidInit_uart () {scon=0x50;//Set to working mode 1Tmod=0x20;//Set how the counter works 2pcon=0x80;//baud rate doubledth1=0xf3;//Counter initial value, baud rate is 4800tl1=0xf3; Ea=1;//Open Total Interruptes=1;//Open Accept interrupttr1=1;//Turn on the counter}voidMain () {Init_uart (); while(1)    {if(flag==1) {es=0;//Shut down serial interruptflag=0; Sbuf=i;//Received data written to Sbuf             while(! TI);//Decide whether to send outTi=0; es=1;//Open serial interrupt}     }}voidSerial () interrupt4{ri=0;//Accept the flag-clear 0I=sbuf;//Read data from Sbufflag=1;//instructions to receive data}

Baud rate calculation and operation of the introduction of a small, commonly used baud rate of 9600, crystal oscillator frequency of 11.0592MHZ, the error is 0%. I use a crystal oscillator frequency of 12MHZ, there is a certain error.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

51 Single chip microcomputer learning note "Six"--serial communication experiment

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.