Use STM32CubeMX and Keil (HAL library)
Materials: stm32 development board, USB to TTL? CH340 module, Dupont line, st-link
Experimental principle:
Simulates asynchronous half-duplex communication
Variable baud rate
Start bit: 1
Data bit: 8
Stop bit: 1
(10 bits per data)
No parity bit
When transmitting a character, a start bit is sent first, followed by 8 data bits (from low to high), and finally a stop bit.
Use an ordinary GPIO port output (simulating TXD) to simulate the high and low levels sent above, and use timer delay (the duration of the level corresponding to one bit is 1000000/baud rate μs)
Use an ordinary GPIO port interrupt input (simulating RXD) to simulate reception, trigger interrupts on the falling edge, use timer delay, and collect level data once in the center of the data bit
Experimental design:
Use PA1 as TXD, PA2 as RXD, and then connect to the CH340 module and then to the computer
TIM2 delay
experiment procedure:
The test uses a baud rate of 9600
At first, we only realized the initial sending of "hello" and the correct sending and receiving of 2 bits of data in the loopback test. . .
It is inferred that the sending function of PA1 should be successful (however, after testing with a logic analyzer, the delay of one bit sent by PA1 reaches about 106us, which should be 104us under normal circumstances). There is a bug in the receiving function of PA2, which can only correctly receive 2 bits of data at a time, and the characters received afterwards are all wrong. It is speculated that it is a problem of time error in the delay, and it will automatically increase characters like spaces (after testing with a logic analyzer, it is actually a character with an ascii code of 255. After multiple tests, it was found that the interrupt should be triggered after receiving all the characters, but the subsequent levels are all high, so this character is received). . .
Wrong result:
Then change the code based on the speculation, reduce the delay time in the received data, and ignore the characters with ascii code 255 (a simple and crude method...)
Correct result:
Main settings in STM32CubeMX:
Main code (the following is only the code in USER CODE BEGIN):
/* USER CODE BEGIN Includes */
#include
/* USER CODE END Includes */
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
uint32_t baudrate=9600; //Set the baud rate, for example 9600bps
uint8_t message[20]; //The character array where the received characters are stored
uint8_t length=0; //Number of characters received
uint8_t welcome[]="hello"; //An initial message to be sent
uint32_t bit_time;
/* USER CODE END PV */
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
void delay_us(uint32_t counter);
void transmitChar(uint8_t ch);
uint8_t receiveChar(void);
void transmitString(uint8_t a[]);
/* USER CODE END PFP */
/* USER CODE BEGIN 1 */
bit_time=1000000/baudrate; //One bit time, unit is us
/* USER CODE END 1 */
/* USER CODE BEGIN 2 */
transmitString(welcome); //Send initial information
/* USER CODE END 2 */
/* USER CODE BEGIN 4 */
//Delay counter (us)
void delay_us(uint32_t counter){
counter++;
HAL_TIM_Base_Start(&htim2);
__HAL_TIM_SET_COUNTER(&htim2,counter);
while(counter>1){
counter=__HAL_TIM_GET_COUNTER(&htim2);
}
HAL_TIM_Base_Stop(&htim2);
}
//Send characters
void transmitChar(uint8_t ch){
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,(GPIO_PinState)0);
delay_us(bit_time);
uint8_t i,temp;
for(i=0;i<8;i++){
temp=ch&0x01;
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,(GPIO_PinState)temp);
delay_us(bit_time);
ch>>=1;
}
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,(GPIO_PinState)1);
delay_us(bit_time);
}
//Send string
void transmitString(uint8_t a[]){
uint8_t i,j;
j=strlen(a);
for(i=0;i transmitChar(a[i]); } } //Receive characters uint8_t receiveChar(void){ uint8_t bit,i,ch=0; delay_us(bit_time*1.5); for(i=0;i<8;i++){ bit=HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_2); ch>>=1; if(bit){ ch=ch|0x80; } delay_us(bit_time); } delay_us(bit_time*0.2); return ch; } //GPIO interrupt callback function void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { /* Prevent unused argument(s) compilation warning */ UNUSED(GPIO_Pin); uint8_t ch; ch=receiveChar(); if(ch=='#'){ uint8_t i; for(i=0;i transmitChar(message[i]); } length=0; } else if(ch!=255){ message[length++]=ch;} } /* USER CODE END 4 */ Experimental results: The correct transmission and reception of less than 19 characters is achieved (the character array of 20 elements, the reception of more characters has not been tested...)
Previous article:STM32 JTAG debug interface PB3, PB4 multiplexing problem
Next article:STM32 I/O as external interrupt input
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- Rambus Launches Industry's First HBM 4 Controller IP: What Are the Technical Details Behind It?
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- Pipeline water leakage monitor based on sound waves] Material unpacking-ESP32-S3-DEVKITC+STM32L496 Discovery kit
- [ESK32-360 Review] Potentiometer to adjust LCD text color
- Selling ZYNQ 7020 and other idle development boards
- During the Mid-Autumn Festival, engineers will not work overtime!
- Different types of pin headers
- How to select peripheral components of DC-DC boost regulator
- Example interpretation of 51 single chip microcomputer complete learning and application
- FM33LG0xx FL Library Examples & Keil Pack
- How do zstack routers and terminal nodes share a common set of programs?
- The efficiency has little to do with IC.