当前位置:网站首页>Learning records: serial communication and solutions to errors encountered

Learning records: serial communication and solutions to errors encountered

2022-07-06 15:33:00 Bitter tea seeds

List of articles

Learning notes : The concept of serial communication and programming to realize serial communication .
Encountered …\OBJ\Project.axf: Error: L6200E: Symbol USART1_IRQHandler multiply defined (by usart.o and main.o). Error resolution !



Preface

I've been reading 《 The embedded C Self cultivation of language 》 Learned a lot of knowledge , Especially compilation and C Function call for , This is also used in learning serial communication C Language calls assembly functions and NVIC Writing of interrupt program .
The following is my summary of knowledge, as well as how to write serial communication programs and NVIC Interrupt the procedure ( It mainly refers to the reference manual of punctual atoms ).


One 、 Basic knowledge of serial communication

1. There are two ways for the processor to communicate with external devices :

Parallel communication
- Transmission principle : All bits of data are transmitted at the same time .
- advantage : Fast
- shortcoming : It takes up a lot of pin resources
serial communication
- Transmission principle : Data transmission in bit order .
- advantage : Less pin resources
- shortcoming : Relatively slow

1.1 serial communication

According to the data transmission direction , It is divided into :
Simplex :
Data transmission only supports data transmission in one direction .
Half duplex :
Allows data to be transmitted in both directions , however , At some point , Only number allowed
It is transmitted in one direction , It's actually a kind of direction switching simplex communication ;
full duplex :
Allows data to be transmitted in both directions at the same time , therefore , Full duplex communication is two
Combination of simplex communication , It requires that both the sending device and the receiving device have independent
Ability to receive and send .
There are three transmission modes of serial communication :
 Insert picture description here

1.2 Communication mode of serial communication

Synchronous communication With clock Synchronous signal transmission .
-SPI,IIC communication interface ;
asynchronous communication Without clock Synchronous signal .
-UART( Universal asynchronous transceiver ), Single bus ;

2. Common serial communication interface

Communication standards Pin description communication mode Direction of communication
UART( Universal asynchronous transceiver )TXD: The sender ;RXD: The receiving end ;GND: In public asynchronous communication full duplex
Single bus (1-wire)DQ: send out / The receiving end asynchronous communication Half duplex
SPISCK: Synchronous clock ;MISO: Host input , Slave output ;MOSI: Host output , Slave input Synchronous communication full duplex
IICSCL: Synchronous clock ;SDA: data input / Output terminal Synchronous communication Half duplex

2.1、STM32 Serial communication interface

UART: Universal asynchronous transceiver ;
USART: Universal synchronous asynchronous transceiver ;

2.2、UART Asynchronous communication mode pin connection method :

-RXD: Data input pin . Data reception .
-TXD: Data transmission pin . Data sending .

 Insert picture description here

 Insert picture description here

2.3、UART Characteristics of asynchronous communication mode :

  • Full duplex asynchronous communication .
  • Fractional baud rate generator system , Provide accurate baud rate .
    - Send and receive common programmable baud rate , Up to 4.5Mbits/s
  • Programmable data word length (8 Bits or 9 position );
  • Configurable stop bits ( Support 1 perhaps 2 Bit stop bit );
  • Configurable use DMA Multi buffer communication .
  • Separate transmitter and receiver enable bits .
  • Detection marks :① Accept buffer ② Send buffer empty ③ End of transmission flag
  • Multiple interrupt sources with flags . Trigger interrupt .
  • other : Calibration control , Four error detection flags .

3.STM32 Parameters to be defined for serial asynchronous communication

  • Start bit
  • Data bits (8 Bits or 9 position )
  • Parity bit ( The first 9 position )
  • Stop bit (1,15,2 position )
  • Baud rate setting
    USART block diagram
     Insert picture description here

Two 、 Programming

1. Writing steps

1. Serial clock enable ,GPIO Clock enable :RCC_APB2PeriphClockCmd();
2. Serial reset :USART_DeInit(); This step is not necessary
3.GPIO Port mode settings :GPIO_Init(); The mode is set to GPIO_Mode_AF_PP
4. Initialization of serial port parameters :USART_Init();
5. Turn on interrupt and initialize NVIC( If you need to turn on interrupt, you need this step )
NVIC_Init();
== USART_ITConfig();==
6. Enable serial port :USART_Cmd();
7. Write interrupt handling functions :USARTx_IRQHandler();
8. Serial data transmission :
void USART_SendData();// Send data to serial port ,DR
uint16_t USART_ReceiveData();// Receive data , from DR Read the received data
9. Serial port transmission state acquisition :
FlagStatus USART_GetFlagStatus
(USART_TypeDef* USARTx, uint16_t USART_FLAG);
void USART_ClearITPendingBit
(USART_TypeDef* USARTx, uint16_t USART_IT);

2. Program instance

The code is as follows ( Shown ):

#include "stm32f10x.h"

void Bitter_USART_Init(void)
{
    
		GPIO_InitTypeDef GPIO_InitStruct;
		USART_InitTypeDef USART_InitStruct;
		NVIC_InitTypeDef NVIC_InitStruct;
	
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);// Can make GPIOA
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);// Enable serial port one 
	
		GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;
		GPIO_InitStruct.GPIO_Pin=GPIO_Pin_9;
		GPIO_InitStruct.GPIO_Speed=GPIO_Speed_10MHz;
		GPIO_Init(GPIOA,&GPIO_InitStruct);
	
		GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;
		GPIO_InitStruct.GPIO_Pin=GPIO_Pin_10;
		GPIO_InitStruct.GPIO_Speed=GPIO_Speed_10MHz;
		GPIO_Init(GPIOA,&GPIO_InitStruct);
		
		USART_InitStruct.USART_BaudRate=115200;// Baud rate 
		USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;// Hardware flow control 
		USART_InitStruct.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;// Send and receive 
		USART_InitStruct.USART_Parity=USART_Parity_No;// Parity check 
		USART_InitStruct.USART_StopBits=USART_StopBits_1;// Stop bit 
		USART_InitStruct.USART_WordLength=USART_WordLength_8b;// The word is long 
	
		USART_Init(USART1,&USART_InitStruct);// Serial initialization 
		USART_Cmd(USART1,ENABLE);// Serial port enable function 
		
		USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);// Turn on the interrupt service function ; Receive interrupt 
		
		NVIC_InitStruct.NVIC_IRQChannel=USART1_IRQn;// Interrupt channel 
		NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;// Open interrupt channel 
		NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;// Set preemption priority 
		NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;// Sub priority 
		
		NVIC_Init(&NVIC_InitStruct);
}

void USART1_IRQHandler(void)
{
    
		u8 tea;
		if(USART_GetITStatus(USART1,USART_IT_RXNE))// Determine the interrupt type 
		{
    
				tea=USART_ReceiveData(USART1);// Read the data received by the serial port ;
				USART_SendData(USART1,tea);// send data 
		}
}

int main(void)
{
    
		NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// Interrupt priority , Two people preempt priority , Two response priorities 
		Bitter_USART_Init();
		while(1);
}

3、 ... and 、…\OBJ\Project.axf: Error: L6200E: Symbol USART1_IRQHandler multiply defined (by usart.o and main.o). Error resolution

stay usart.c Found in and USART1_IRQHandler Same function , Just comment it out .


summary

Writing serial port can also use HAL Library to achieve serial port configuration and use methods .. stay HAL In the library , The functions and definitions related to serial port are mainly in the file stm32f1xx_hal_uart.c and stm32f1xx_hal_uart.h in .

原网站

版权声明
本文为[Bitter tea seeds]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060919235533.html