当前位置:网站首页>Learning record: USART serial communication
Learning record: USART serial communication
2022-07-06 15:33:00 【Bitter tea seeds】
Catalog
1. Introduction to serial communication protocol
3.printf Support code for function
1. Introduction to serial communication protocol
For communication protocols , We also understand it in a hierarchical way , The most basic thing is to divide it into Physical layer and protocol layer . The physical layer specifies that the communication system has mechanical 、 The characteristics of the electronic functional part , Ensure the transmission of raw data on physical media . The protocol layer mainly specifies the communication logic , Unify the data packaging of both sender and receiver 、 Unpacking standard .
1.1 The physical layer
It's about understanding :RS-232 standard ,RS-232 The standard mainly stipulates The signal Use of 、 Communication interface and signal level standard .
Use RS-232 Common among standard serial port devices Communication structure , As shown in the figure below
In the communication mode above , Two communication devices “DB9 Interface ” The connection between them is established through the serial signal line , Used in serial port signal line “RS-232 standard ” Transmitting data signals . because RS-232 The level standard signal cannot be directly recognized by the controller , So these signals go through a “ Level conversion chip ” Convert to something the controller can recognize “TTL standard ” The level of the signal , To communicate .
1.2 Level standard
According to the level standard used in communication , Serial communication can be divided into TTL Standards and RS-232 standard ;
It is often used in common electronic circuits TTL The level standard of , ideally , Use 5V Represents binary logic 1, Use 0V To express logic 0; In order to increase the ability of long-distance transmission and anti-interference of serial communication ,RS-232 Use -15V To express logic 1, +15V To express logic 0. Use RS232 And TTL Level calibration represents the comparison of the same signal , As shown in the figure below .
Because controllers generally use TTL Level standard , So we often use MA3232 Chip pair TTL And RS-232 Level signals are converted to each other .
2. Protocol layer
The data packet of serial communication is sent by the sending device through its own TXD Interface to the receiving device RXD Interface . In the protocol layer of serial communication , Specifies the content of the packet , It starts from the beginning 、 Subject data 、 Check bit and stop bit , The data packet format of both communication parties shall be consistent in order to send and receive data normally ,
3.printf Support code for function
// Add the following code , Support printf function , You don't need to choose use MicroLIB
#if 1
#pragma import(__use_no_semihosting)
// Support functions required by the standard library
struct __FILE
{
int handle;
};
FILE __stdout;
// Definition _sys_exit() To avoid using semi host mode
void _sys_exit(int x)
{
x = x;
}
// redefinition fputc function
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);// Cycle to send , Until it's sent
USART1->DR = (u8) ch;
return ch;
}
#endif
4.usart.h Program
Define the maximum number of bytes received 200;
#define USART_REC_LEN 200
Receive buffer , Maximum USART_REC_LEN Bytes . The last byte is a newline character ;
extern u8 USART_RX_BUF[USART_REC_LEN];
Receive status flag ;
extern u16 USART_RX_STA;
Can make (1)/ prohibit (0) A serial port 1 receive ;
#define EN_USART1_RX 1
usart.c and usart.h Function program in column Stm32 In the study notes Blog 《 Learning record : Serial communication and solutions to errors encountered 》 Yes .
5.main.c The main function
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
int main(void)
{
u16 t;
u16 len;
u16 times=0;
delay_init(); // Delay function initialization
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); // Set up NVIC Interrupt grouping 2:2 Bit preemption priority ,2 Bit response priority
uart_init(115200); // The serial port is initialized to 115200
LED_Init(); //LED Port initialization
KEY_Init(); // Initialize the hardware interface connected with the key
while(1)
{
if(USART_RX_STA&0x8000)
{
len=USART_RX_STA&0x3fff;// The length of data received this time
printf("\r\n The message you sent is :\r\n\r\n");
for(t=0;t<len;t++)
{
USART_SendData(USART1, USART_RX_BUF[t]);// Serial port 1 send data
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);// Wait for the end of sending
}
printf("\r\n\r\n");// Insert newline
USART_RX_STA=0;
}
else
{
times++;
if(times%5000==0)
{
printf("\r\n Focus on Bitter tea seeds\r\n");
printf("USART— A serial port communication \r\n\r\n");
}
if(times%200==0)printf(" Please input data , End with enter \n");
if(times%30==0)LED0=!LED0;// flashing LED, Indicates that the system is running .
delay_ms(10);
}
}
}
First, determine the global variables USART_RX_STA Whether the highest position of is 1, If 1 Words , Then it means that the previous data reception has been completed , The next step is to send our customized receive buffer data to the serial port .
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
The above code is after we send a byte , To check whether this data has been sent .
6. Experimental phenomena
边栏推荐
- C4D quick start tutorial - creating models
- UCORE Lab 1 system software startup process
- ucore lab7
- What is "test paper test" in software testing requirements analysis
- Leetcode notes - dynamic planning -day7
- 想跳槽?面试软件测试需要掌握的7个技能你知道吗
- Which version of MySQL does php7 work best with?
- How to build a nail robot that can automatically reply
- STM32學習記錄:輸入捕獲應用
- LeetCode#412. Fizz Buzz
猜你喜欢
转行软件测试必需要知道的知识
ucore lab 2
How to rename multiple folders and add unified new content to folder names
FSM and I2C experiment report
Learning record: Tim - capacitive key detection
软件测试方法有哪些?带你看点不一样的东西
软件测试行业的未来趋势及规划
Do you know the advantages and disadvantages of several open source automated testing frameworks?
基于485总线的评分系统
软件测试Bug报告怎么写?
随机推荐
LeetCode#237. Delete nodes in the linked list
软件测试方法有哪些?带你看点不一样的东西
What is "test paper test" in software testing requirements analysis
MySQL数据库(二)DML数据操作语句和基本的DQL语句
Programmers, how to avoid invalid meetings?
Leetcode notes - dynamic planning -day7
全网最详细的postman接口测试教程,一篇文章满足你
STM32学习记录:玩转按键控制蜂鸣器和LED
Do you know the performance testing terms to be asked in the software testing interview?
Research Report of pharmaceutical solvent industry - market status analysis and development prospect prediction
ucore lab 6
UCORE lab2 physical memory management experiment report
What are the commonly used SQL statements in software testing?
ucore lab7
Lab 8 file system
The minimum number of operations to convert strings in leetcode simple problem
Stm32 dossiers d'apprentissage: saisie des applications
学习记录:理解 SysTick系统定时器,编写延时函数
软件测试面试回答技巧
软件测试工作太忙没时间学习怎么办?