Summary of serial communication (realize communication between two SCM) __ Communication

Source: Internet
Author: User
This paper mainly introduces the application of serial communication and serial communication. The goal is to realize the communication between the SCM.
1. The basic concept of serial communication

The serial is corresponding to the parallel, and the parallel communication means that the data are transmitted at the same time. Serial communication is the order in which the bits of data to be transmitted are sent sequentially.
The serial communication realizes the data transfer between two objects, and the object is usually a single-chip microcomputer. Communication is actually on the two single chip computer connected to the line, through the transmission of information.

As the diagram, the modem is very important, its function is realizes the digital signal and the analog signal conversion. Note, however, that modems are useful for long-distance transmissions. The modem (0 modem mode) is not required for near-distance transmission. Therefore, the experiment of single chip computer only need to connect the line of the corresponding interface. Schematic diagram of the connection
2.stm32 Single-chip computer and PC serial communication 1. Connection of Signal line

SCM and computer communication is usually used USB interface to connect the computer. Then we need to first turn the serial port to usb,stm32 on the corresponding hardware to achieve this function, we only need to see whether the circuit diagram wiring connection.
Here's the wiring step for the miniSTM32 atom:
(1) Check SCM circuit diagram, find the motherboard chip u1_rxd and U_TXD interface.
(2) Find the RXD and Txd interface of usb_232
(3) If the circuit diagram on the line is not connected, the motherboard chip u1_rxd through the jumper and the usb_232 on the TXD connection, the motherboard chip u1_txd through the jumper and usb_232 on the link. 2. Preparation of Procedures

Because the use of STM32 official firmware library, so the preparation of serial communication program is very simple.
Ideas:
(1) Initialization of the serial port
(2) Call the Usart_senddata function to send data to the serial port.
Where the initialization of the serial port includes
1 serial clock to enable, GPIO clock to enable
2) Serial Reset
3) GPIO port mode set Port mode setting
4 Serial parameter initialization
5 Open the interrupt and initialize the NVIC (this step if you need to open the interrupt)
6) enable the serial port
7 Write Interrupt function

The simplest serial communication program is as follows, note that this program is not sent until the interrupt function is written. The data sent is always 01.
Code 1

#include "stm32f10x.h" void My_delay_ms (int time);
      void My_delay_ms (int time) {int i=0;
            while (time--) {i=12000;
            while (i) {i--;
    }} void Uart_init (U32 bound) {//gpio port settings gpio_inittypedef gpio_initstructure;
    Usart_inittypedef usart_initstructure;

    Nvic_inittypedef nvic_initstructure; Rcc_apb2periphclockcmd (rcc_apb2periph_usart1| Rcc_apb2periph_gpioa, ENABLE); Enable to Usart1,gpioa clock//usart1_tx gpioa.9 gpio_initstructure.gpio_pin = gpio_pin_9;
    pa.9 gpio_initstructure.gpio_speed = Gpio_speed_50mhz; Gpio_initstructure.gpio_mode = gpio_mode_af_pp; Duplicate push-Pull output gpio_init (Gpioa, &gpio_initstructure);//Initialize gpioa.9//usart1_rx gpioa.10 initialize gpio_initstructure. Gpio_pin = gpio_pin_10;//pa10 Gpio_initstructure.gpio_mode = gpio_mode_in_floating;//float input GPIO_Init (GPIOA, &GP Io_initstructure)//Initialize gpioa.10//usart1 NVIC configuration Nvic_Initstructure.nvic_irqchannel = USART1_IRQN; Nvic_initstructure.nvic_irqchannelpreemptionpriority=3//preemption Priority 3 Nvic_initstructure.nvic_irqchannelsubpriority = 3      ;         Sub-priority 3 Nvic_initstructure.nvic_irqchannelcmd = ENABLE; IRQ Channel enables Nvic_init (&nvic_initstructure); Initializes the VIC register//usart initialization setting Usart_initstructure.usart_baudrate = bound;//serial port baud rate based on the specified parameters Usart_initstructure.usart _wordlength = usart_wordlength_8b;//Word length to 8-bit data format usart_initstructure.usart_stopbits = usart_stopbits_1;//a stop bit USART_ initstructure.usart_parity = usart_parity_no;//no parity bit usart_initstructure.usart_hardwareflowcontrol = USART_ hardwareflowcontrol_none;//No hardware data flow control Usart_initstructure.usart_mode = Usart_mode_rx | Usart_mode_tx; Transceiver Mode Usart_init (USART1, &usart_initstructure);                    Initialize serial port 1 usart_itconfig (USART1, Usart_it_rxne, enable);//Open serial port to accept interrupt Usart_cmd (USART1, enable);
Enable serial port 1} uint16_t str=1;
    int main () {U16 times=0; Uart_init (115200);
    Key_init ();
        while (1) {times++;
        My_delay_ms (10); if (times%10000) {usart_senddata (USART1, str);//Send data to serial 1 while (Usart_getflagsta  
        Tus (USART1,USART_FLAG_TC)!=set);   

} return 0;                   
 }

On the PC side to open the serial debugging assistant, you can see the continuous receipt of data 01
3.linux System single chip microcomputer and computer serial communication 1. Connection of Signal line

This system uses the inquiry as the Development Board, Development Board for the Linux system, because the development of the Board with UART (serial) interface, so the use of UART USB cable, one end of the UART interface with the development Board, one end of the computer USB on the line, open the Serial debugging assistant, you can view the serial data. 2. Preparation of Procedures

Ideas:
(1) Install serial port driver under Linux system
(2) write the serial port to send the function
The serial port sends the function step is:
1) fopen Open the corresponding device of the serial port
2 set parameters, such as baud rate, etc.
3 Write data to the string using the Write function
The code is similar to section 4th.
Open the Serial debugging assistant, you can see the data sent on the computer screen. single-chip microcomputer serial communication between 4.stm32 MCU and Linux system 1. Connection of Signal line

If the SCM can communicate with the computer, then two single chip microcomputer serial communication, just connect the serial port line, prepare three jumper, the first connection single 1 Rxd and single 2 txd, the second connection single 1 Txd and single 2 rxd, the third connection single 1 gnd and the Gnd of single 2. OK, you can send the data. 2. Preparation of Procedures

This code realizes the lower machine STM32 sends the numeral 1, the host computer Linux system monolithic processor receives the numeral 1 and prints out.
1.stm32 Program and Code 1, just keep sending 1.
2.linux system SCM Code such as code 2, simply read the sent data and output.
Code 2

#include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include ;sys/stat.h> #include <fcntl.h> #include <termios.h> #include <errno.h> int set_opt (Int,int,int,
Char,int);

void Leds_control (int);
    int main (int argc, char* argv[]) {printf ("Hello,run ok\n");
    int fd, read_num = 0;

    Char *uart3 = "/DEV/TTYSAC3";
    Char buffer[1024],buffer_test[1024];
    memset (buffer, 0, 1024);

    memset (buffer_test, 0, 1024);
        if (ARGC < 2) {printf ("Usage:./uarttest/dev/ttysac3\n");
    return 0; } if (fd = open (argv[1), o_rdwr| o_noctty|

        O_ndelay)) (<0) {printf ("Open%s is failed\n", argv[1]);
    return 0;

    else{set_opt (FD, 115200, 8, ' N ', 1);
    int n=10000000;
    int k=0;
        while (k<n) {k++;
        printf ("%d\n", K);
        Sleep (1);
        memset (buffer, 0, 256);
        Read_num = Read (fd, buffer, 255); printf ("REad_num=%d\n ", read_num);
        if (read_num>0) {printf ("%s\n", buffer);       

        }else{printf ("Read error\n");
    } fd=close (FD);
return 0;
    int set_opt (int fd,int nspeed, int nbits, char nevent, int nstop) {struct Termios newtio,oldtio;
        if (tcgetattr (fd,&oldtio)!= 0) {perror ("Setupserial 1");
    return-1;
    } bzero (&newtio, sizeof (Newtio)); Newtio.c_cflag |= clocal |
    Cread;

    Newtio.c_cflag &= ~csize;
        Switch (nbits) {case 7:newtio.c_cflag |= CS7;
    Break
        Case 8:newtio.c_cflag |= CS8;
    Break
        Switch (nevent) {case ' O ': Newtio.c_cflag |= Parenb;
        Newtio.c_cflag |= parodd; Newtio.c_iflag |= (INPCK |
        Istrip);
    Break Case ' E ': Newtio.c_iflag |= (INPCK |
        Istrip);
        Newtio.c_cflag |= Parenb;
        Newtio.c_cflag &= ~parodd;
    Break CaSe ' N ': Newtio.c_cflag &= ~parenb;
    Break
        Switch (nspeed) {case 2400:cfsetispeed (&newtio, B2400);
        Cfsetospeed (&newtio, B2400);
    Break
        Case 4800:cfsetispeed (&newtio, B4800);
        Cfsetospeed (&newtio, B4800);
    Break
        Case 9600:cfsetispeed (&newtio, B9600);
        Cfsetospeed (&newtio, B9600);
    Break
        Case 115200:cfsetispeed (&newtio, B115200);
        Cfsetospeed (&newtio, B115200);
    Break
        Case 460800:cfsetispeed (&newtio, B460800);
        Cfsetospeed (&newtio, B460800);
    Break
        Case 921600:printf ("b921600\n");
                Cfsetispeed (&newtio, B921600);
        Cfsetospeed (&newtio, B921600);
    Break
        Default:cfsetispeed (&newtio, B9600);
        Cfsetospeed (&newtio, B9600);
    Break
} if (nstop = = 1) newtio.c_cflag &= ~CSTOPB;    else if (nstop = 2) newtio.c_cflag |= CSTOPB;
    Newtio.c_cc[vtime] = 0;
    Newtio.c_cc[vmin] = 0;
    Tcflush (Fd,tciflush);
        if ((Tcsetattr (fd,tcsanow,&newtio))!=0) {perror ("com set error");
    return-1;
    }//printf ("Set done!\n\r");
return 0; }

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.