当前位置:网站首页>STM32 serial communication

STM32 serial communication

2020-12-08 12:33:55 osc_ 52r4y6wv

One 、 Learning tasks

  1. Learn the relevant materials , Name based on register and firmware library stm32 LED What's the difference between the programming methods of the water lamp example .
  2. Learning and reading “ Zero dead angle play STM32F103– Guider ” Number... In the document 20、21 Chapter content , complete STM32 Of USART Window communication program , requirement :1) Set the baud rate to 115200,1 Bit stop bit , No parity bit .2)STM32 The system gives the upper computer (win10) Continuous transmission “hello windows!”, Host computer receiving program can use “ Serial debugging assistant “, You can also program yourself .3) When the upper computer gives stm32 send out “Stop,stm32” after ,stm32 Stop sending .

Two 、 Learning content

1. Register based and firmware library based stm32 LED Water lamp programming differences

1.1 Register based stm32 LED Water lamp program

1.1.1 Register Introduction

The function of register is to store binary code , It is composed of triggers with storage function . A trigger can store 1 Bit binary code , Therefore, it should be stored n Bit binary code register , need n A trigger to make up .
According to different functions , The register can be divided into basic register and shift register . Basic registers can only feed data in parallel , It can only output in parallel . The data in the shift register can be shifted to the right or left bit by bit under the action of shift pulse , Data can be input in parallel 、 Parallel output , You can also serial input 、 Serial output , You can also input in parallel 、 Serial output , Or serial input 、 Parallel output , Very flexible , It's also very versatile .

1.1.2. Code

led.h

#ifndef __LED_H
#define __LED_H	 
#include "sys.h"


//LED Port definition 
#define LED0 PBout(5)	// DS0
#define LED1 PEout(5)	// DS1	

void LED_Init(void);	// initialization 		 				    
#endif

led.c

#include "led.h"


// initialization PB5 and PE5 For the output port . And enable the clock of these two ports 		    
//LED IO initialization 
void LED_Init(void)
{
   
   
	RCC->APB2ENR|=1<<3;    // Can make PORTB The clock 	   	 
	RCC->APB2ENR|=1<<6;    // Can make PORTE The clock 	
	   	 
	GPIOB->CRL&=0XFF0FFFFF; 
	GPIOB->CRL|=0X00300000;//PB.5  Push pull output    	 
  GPIOB->ODR|=1<<5;      //PB.5  High output 
											  
	GPIOE->CRL&=0XFF0FFFFF;
	GPIOE->CRL|=0X00300000;//PE.5 Push pull output 
	GPIOE->ODR|=1<<5;      //PE.5 High output  
}

main.c

#include "sys.h"
#include "usart.h"		
#include "delay.h"	
#include "led.h"

int main(void)
{
   
   				  
	Stm32_Clock_Init(9);// System clock settings 
	delay_init(72);	  	// Delay initialization 
	LED_Init();		  	// Initialization and LED Connected hardware interface 
	while(1)
	{
   
   
		LED0=0;		// Low level active 
		LED1=1;
		delay_ms(1000);
		LED0=1;
		LED1=0;
		delay_ms(1000);
	}	 
}

1.2 Firmware library based stm32 LED Water lamp program

1.2.1 Firmware library Overview

Firmware is a product between software and hardware , Provide developers with an intermediate access to the underlying hardware API, Greatly improve the efficiency of application development .CMSIS: ARM Cortex Microcontroller software interface standard (CMSIS:Cortex Microcontroller Software Interface Standard) yes Cortex-M The vendor independent hardware abstraction layer of the processor family , Use CMSIS, A consistent and simple software interface can be implemented for processors and peripherals , So as to simplify the reuse of software 、 Shorten the learning process for new microcontroller developers , And shorten the time to market for new devices .

1.2.2 Code

led.h

#ifndef __LED_H
#define __LED_H	 
#include "sys.h"

#define LED0 PBout(5)// PB5
#define LED1 PEout(5)// PE5	
void LED_Init(void);// initialization 				    
#endif

led.c

#include "led.h"
// initialization PB5 and PE5 For the output port . And enable the clock of these two ports 		    
//LED IO initialization 
void LED_Init(void)
{
   
   
 GPIO_InitTypeDef  GPIO_InitStructure;	
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE, ENABLE);	 // Can make PB,PE Port clock 	
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;				 //LED0-->PB.5  port configuration 
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 // Push pull output 
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 //IO The mouth speed is 50MHz
 GPIO_Init(GPIOB, &GPIO_InitStructure);					 // Initialize according to the set parameters GPIOB.5
 GPIO_SetBits(GPIOB,GPIO_Pin_5);						 //PB.5  High output 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;	    		 //LED1-->PE.5  port configuration ,  Push pull output 
 GPIO_Init(GPIOE, &GPIO_InitStructure);	  				 // Push pull output  ,IO The mouth speed is 50MHz
 GPIO_SetBits(GPIOE,GPIO_Pin_5); 						 //PE.5  High output  
}

main.c

#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"

 int main(void)
 {
   
   	
	delay_init();	    // Delay function initialization 	  
	LED_Init();		  	// Initialization and LED Connected hardware interface 
     
    
	while(1)
	{
   
   
		LED0=0;
		LED1=1;
		delay_ms(300);	 // Time delay 300ms
		LED0=1;
		LED1=0;
		delay_ms(300);	// Time delay 300ms
	}    
 }


 /**
 ***************** The following code is implemented by calling library functions IO The way to control *****************************************
int main(void)
{ 
 
	delay_init();		  // Initialization delay function 
	LED_Init();		        // initialization LED port 
	while(1)
	{
			GPIO_ResetBits(GPIOB,GPIO_Pin_5);  //LED0 Corresponding pin GPIOB.5 Pull it down , bright    equivalent LED0=0;
			GPIO_SetBits(GPIOE,GPIO_Pin_5);   //LED1 Corresponding pin GPIOE.5 pull up , destroy   equivalent LED1=1;
			delay_ms(300);  		   // Time delay 300ms
			GPIO_SetBits(GPIOB,GPIO_Pin_5);	   //LED0 Corresponding pin GPIOB.5 pull up , destroy    equivalent LED0=1;
			GPIO_ResetBits(GPIOE,GPIO_Pin_5); //LED1 Corresponding pin GPIOE.5 Pull it down , bright   equivalent LED1=0;
			delay_ms(300);                     // Time delay 300ms
	}
} 

1.3 differences

A close look at the source code shows that it is mainly led.c Medium LED_Init() Functions are very different . Register based is mainly to use pointer to address to enable and assign value ; Based on the main function package of firmware library to enable and assign value . In small projects, it can be seen that there is no big difference between using registers and firmware libraries , But from the point of view of multi-function and multi port engineering design , Using the firmware library can greatly reduce the development time , Improve work efficiency .
Using the firmware library , At present, many routines are written with firmware library . Official examples also use firmware library . The feature is simplicity , Easy to understand , More information . If you don't CortexM The development foundation of series kernel , It is recommended to start from the firmware library . There is a certain foundation , Or use registers when necessary .
Using registers , I want to understand CortexM3 The kernel may be needed for better portability , Learning register programming can be helpful . But from a professional point of view , Registers are closer to the bottom , Have a deeper understanding of the working principle and operating mechanism of peripherals .

2.STM32 Of USART Window communication program

2.1 Experimental environment

Development board : Wildfire guider (STM32F103)
STM32 Library version :STM32F10x_StdPeriph_Lib_V3.5.0
IDE:KEIL5
Serial port used :USART1
Hardware schematic diagram :
 Insert picture description here




2.2 Experimental documents

link : Book matching routines -F103VE Guider
Extraction code :k5rw
When the download is complete, the arrow will open keil file .
 Insert picture description here


2.3. Related code and modification

2.3.1 find stm32f10x_it.c, And main.c;
 Insert picture description here
2.3.2 Code
stm32f10x_it.c:


int i=0;
uint8_t ucTemp[50];
void DEBUG_USART_IRQHandler(void)
{
   
   
	if(USART_GetITStatus(DEBUG_USARTx,USART_IT_RXNE)!=RESET)
	{
   
   
		ucTemp[i] = USART_ReceiveData(USART1);	
	}
  if(ucTemp[i] == '!')
	{
   
   
		if(ucTemp[i-1] == '2'&&ucTemp[i-2] == '3'&&ucTemp[i-3] == 'm'&&ucTemp[i-4] == 't'&&ucTemp[i-5] == 's'&&ucTemp[i-6] == ' ')
			if(ucTemp[i-7] == 'p'&&ucTemp[i-8] == 'o'&&ucTemp[i-9] == 't'&&ucTemp[i-10] == 's')
			{
   
   
				printf(" closed   To !");
        while(1);
			}
	}
	i++;
}

main.c:

#include "stm32f10x.h"
#include "bsp_usart.h"


void delay(uint32_t count)
{
   
   
	while(count--);
}
int main(void)
{
   
   	
  USART_Config();
  while(1)
	{
   
   	
		printf("hello windows!\n");
		delay(5000000);
	}	
}

2.3.3. Revised as follows
stm32f10x_it.c:
 Insert picture description here
main.c:
.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Jhd2VpOTM5,size_16,color_FFFFFF,t_70)
2.3.4 Compile and generate hex file
 Insert picture description here





2.4. Burn program

2.4.1 Connect the development board
 Insert picture description here
2.4.2 Open the serial port downloader and select the newly compiled .hex file , And then burn it
 Insert picture description here


3. experimental result

 Insert picture description here

3、 ... and 、 Refer to the website

STM32 Serial port communication USART Learning notes

版权声明
本文为[osc_ 52r4y6wv]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201208123300486r.html