Cainiao single-chip microcomputer write-on (51 Single-Chip Microcomputer Serial Communication)

Source: Internet
Author: User
 

You can see usart in many data manuals. UART, SPI, IIC, USB, and CAN communication are messy. You know this is sharp. Serial Communication, there are data formats and data transmission rates. At present, many single-chip computers have UART, especially 51 in our university, which is everywhere on the network. So if I think about it, I will also flood it, or I will forget it later, haha.

51 The serial communication is relatively simple. Set the transmission mode: Mode 1

The baud rate is set. The timer T1 works in Mode 2 and the auto-Reload mode provides the UART clock.

Let's look at the mark of the interruption, so everything will be OK. Let's also look at the clock source frequency you are using. So when you learn UART, you will know why the single-chip microcomputer gets a strange 11.0592m or some single-chip microcomputer is 22.1184 M.

This is to generate an accurate baud rate.

Another important thing is sbuf. Addressing is the same address, but they are all independent single-chip computers. That is to say, every time you use them, you write eg: sbuf = X; this is the sbuf of TX,

X = sbuf. This is the RX sbuf.

Note the buffer overflow problem. Some can contain several bytes, and some single-chip microcomputer will crash if it overflows. Please pay attention to it. Please carefully read the detailed instructions in the Data Manual.

Below is a simple example program, 51 SCM resources are basically exhausted, haha.

 

# Include <reg52.h> # include <stdio. h> # define OSC 12 typedef unsigned int uint16; typedef unsigned char uint8; void uart_init (void); void main (void) {uart_init (); printf ("********************* welcome to MCU world! * ***************** \ N "); printf (" ### let's begin! \ N "); While (1) {printf ("> "); If (getchar () = 'C') {puts (" "); printf (" Yeah! You input a Char! \ N "); printf (_ time _);} puts (" ") ;}} void uart_init () {scon = 0x50; tmod | = 0x20; # If (OSC = 12) Th1 = 0xfd; TL1 = 0xfd; # endifea = 1, ES = 1, tr1 = 1; Ti = 1; // to use the functions in the stdio library, you must set them to Ti = 1}

 
# Include <reg51.h> # include <stdio. h> typedef unsigned int uint16_t; typedef unsigned char uint8_t; # define OSC (unsigned long) 11.0592e6) # define baud 9600 void uartinit (void); void main (void) {uartinit (); printf ("UART"); While (1) ;}void uartinit (void) {scon = 0x50; tmod = 0x20; th1 = TL1 = 256-(unsigned INT) (OSC/12/32/baud); tr1 = 1; Ti = 1; // printf first checks whether Ti is the same. Otherwise, it will be stuck there. If the data is sent to sbuf first, no error will occur .}

The disassembly has seen that it is a while (! Ti); Ti = 0; sbug = var; first judge and clear the flag bit, and then send data to the buffer. Ti indicates that sbuf has not been sent. If the data in the serial port has not been sent, the hardware will not be set, therefore, when writing a round-robin serial port, you must wait for the flag and then clear the sent data!

 

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.