STM32 serial communication

1. Comparison of the programming method difference between the stm32 LED running water lamp example in the register and the firmware library

Compared with the direct configuration of the register, the call of the peripheral library function will have additional consumption in terms of execution efficiency: the process of initializing the variable assignment and the library function will consume the calling time when being called; inside the function, the input parameter is converted The additional calculations required also consume some time.
1. Development characteristics based on the register method:
(1) The specific parameters are more intuitive;
(2) The program runs less resources.
But its shortcomings cannot be ignored, as follows:
(1) The development speed is slow;
(2) The program is poorly readable;
(3) The maintenance is complicated.
The above-mentioned defects directly affect development efficiency, program maintenance costs, and communication costs. Under normal circumstances, only use the method of directly configuring the register when the frequently called interrupt service function is used.
2. Developed based on the solid-state library method, that is, directly calling library functions. The features are:
(1) Easy to communicate with peripherals;
(2) Simple to check errors;
(3) Easy to get started with the main controller STM32.

Two, STM32 line port communication

Development board: Wildfire Guide (STM32F103VE) with 3.2 inch screen
Code compilation: Keil5
USBQ driver: CH340

1. Check the integrity of the development board.
Insert picture description here
2. Turn the switch on the development board to "ON", and the LED next to it will light up.
Insert picture description here
3. Install the CH340 driver.
Insert picture description here
4. Download the Wildfire serial debugging assistant v1.0.1.5.
Insert picture description here
5. Serial communication. Verify the code, the manufacturer has prepared it for us, we only need to debug the code
Insert picture description here
and open it in keil5, and need to modify the specific program code. The modified source file code is as follows:
(1) Modify the serial port interrupt service function of the stm32f10x_it.c file:

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("收到!");
        while(1);
			}
	}
	i++;
}

(2) Main.c is modified as follows:

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


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


6. Perform programming
(1) Ensure that the computer and the development board driver are successfully connected
Insert picture description here
(2) Then go to keil to debug settings
Insert picture description here

Insert picture description here
(3) Then click compile, download and burn, as shown below.
Insert picture description here
At this time , click "open serial port" in the wildfire serial debugging assistant to show that stm32 is sending messages to the upper station. When we enter "stop stm32!", stm32 stops sending messages.
Insert picture description here

Third, the concepts of global variables, local variables, heap, stack, etc. in C language programs

First write a C language program and enter it under Ubantu

#include <stdio.h>
#include <stdlib.h>
int k1 = 1;
int k2;
static int k3 = 2;
static int k4;
int main( )
{
    
       static int m1=2, m2;
    int i = 1;
    char *p;
    char str[10] = "hello";
    char *var1 = "123456";
    char *var2 = "abcdef";
    int *p1=malloc(4);
    int *p2=malloc(4);
    free(p1);
    free(p2);
    printf("栈区-变量地址\n");
    printf("                i:%p\n", &i);
    printf("                p:%p\n", &p);
    printf("              str:%p\n", str);
    printf("\n堆区-动态申请地址\n");
    printf("                   %p\n", p1);
    printf("                   %p\n", p2);
    printf("\n.bss段\n");
    printf("全局外部无初值 k2:%p\n", &k2);
    printf("静态外部无初值 k4:%p\n", &k4);
    printf("静态内部无初值 m2:%p\n", &m2);
    printf("\n.data段\n");
    printf("全局外部有初值 k1:%p\n", &k1);
    printf("静态外部有初值 k3:%p\n", &k3);
    printf("静态内部有初值 m1:%p\n", &m1);
    printf("\n常量区\n");
    printf("文字常量地址     :%p\n",var1);
    printf("文字常量地址     :%p\n",var2);
    printf("\n代码区\n");
    printf("程序区地址       :%p\n",&main);
    return 0;
}

After compilation, the display is as follows: The
Insert picture description here
explanation is as follows:
In C\C++, memory can usually be understood as 4 partitions: stack, heap, global/static storage area and constant storage area.
1 Memory stack area stack: store local variable names;
2. Memory heap area heap: store new or malloc objects;
3. Text & Data & Bss: code segment and static allocation
4. BSS area (uninitialized data segment): No space is allocated for the data in this segment, only the size of the space required for the data is recorded.
5. DATA (initialized data segment): allocate space for data, and the data is stored in the target file.

Four, stm32 heap, stack, global variable allocation address

1. Program the stm32 system in Keil, debug variables, and output information to the host computer through the serial port.
Still the previous project file, and then modify the main.c file, the code is as follows:

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

char global1[16];
char global2[16];
char global3[16];
	
int main(void)
{
    
    	
  char part1[16];
  char part2[16];
  char part3[16];

  USART_Config();

  printf("part1: 0x%p\n", part1);
  printf("part2: 0x%p\n", part2);
  printf("part3: 0x%p\n", part3);
	 
  printf("global1: 0x%p\n", global1);
  printf("global2: 0x%p\n", global2);
  printf("global3: 0x%p\n", global3);
  while(1)
	{
    
    	
		
	}	
}

After we burn the program:
Insert picture description here
Obviously:
part1, part2, and part3 are local variables in the stack, and the addresses are gradually decreasing.
global1, global2, and global3 are global variables in the static area, and the address gradually increases.

2. Then modify the main function again

#include "stm32f10x.h"
#include "bsp_usart.h"
#include <stdlib.h>

int main(void)
{
    
    	
  static char st1[16];
  static char st2[16];
  static char st3[16];
  char *p1;
  char *p2;
  char *p3;

 
  USART_Config();

  printf("st1: 0x%p\n", st1);
  printf("st2: 0x%p\n", st2);
  printf("st3: 0x%p\n", st3);
	 
  p1 = (char *)malloc(sizeof(char) * 16);
  p2 = (char *)malloc(sizeof(char) * 16);
  p3 = (char *)malloc(sizeof(char) * 16);
	
  printf("p1: 0x%p\n", p1);
  printf("p2: 0x%p\n", p2);
  printf("p3: 0x%p\n", p3);
  while(1)
	{
    
    	
		
	}	
}

Insert picture description here
Obviously:
st1, st2, and st3 are all static variables, and their addresses increase sequentially.
P1, p2, and p3 are pointers in the heap, and their addresses increase in sequence.

Guess you like

Origin blog.csdn.net/weixin_47357131/article/details/110495349