1, The development environment equipped with stm32 is to install STM32CubeMX
1. Install jdk
Since STM32CubeMX is implemented in Java, the jdk environment needs to be installed
Download address: Java Downloads | Oracle
2. Install stm32CubeMX (address naming, etc. do not appear in Chinese during installation)
STM32CubeMX - STM32Cube initialization code generator - STMicroelectronics
2, Use CubeMX new project to light LED lights
1. New project
(1) Create a new project and select the model you need
(2) Modular sys interface
(3) Configure the pin. According to the keil5 configuration pin in the previous article, only PA1, PB0 and PB5 need to be set to GPIO_ Just out.
(4) Clock source configuration
The default clock is an internal RC oscillator.
The place shown in the figure is changed to 72 enter.
(5) Engineering Management
Do not appear in Chinese in all naming
Click Code Generator in the upper left corner to configure the next step.
Click GENERATE CODE in the upper right corner to create a project and open the project file
2. New code
1. The code is as follows
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOB,GPIO_PIN_5,GPIO_PIN_SET); HAL_Delay(1000); HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOB,GPIO_PIN_5,GPIO_PIN_SET); HAL_Delay(1000); HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOB,GPIO_PIN_5,GPIO_PIN_RESET); HAL_Delay(1000);
In the while loop under the main function of the main.c file, add the above lines of code.
2. Compiling and burning code
Compile and run the code without error.
3. Result display
3, MDK5 observes the output waveform of three GPIO ports
1. Set simulation mode
2. The compilation run has no errors
3. Modal session
4. Turn on the logic analyzer
5. Click setup to add the IO port to be observed to the logic analyzer
Create a new target port for inputting PORTA.,PORTB. And set the Display Type to Bit
The simulation diagram is shown in the figure:
4, Serial communication, send Hello Windows example
To install the driver software, connect the J-link to the computer.
Hardware connection reference led water lamp production blog STM32F103C8T6 lighting LED water lamp details_ Xiao Yang's diligent blog - CSDN blog
When installed, check whether the device manager has a port.
After you have a port, you can refer to the STM32CubeMX code above or write it directly.
After compiling and running without errors, burn it into the chip.
The code reference is as follows:
Code under usart.c
Under the SYSTEM group, double-click usart.c, uart_init function
void uart_init(u32 pclk2,u32 bound) { float temp; u16 mantissa; u16 fraction; temp=(float)(pclk2*1000000)/(bound*16);//Get USARTDIV mantissa=temp; //Get integer part fraction=(temp-mantissa)*16; //Get decimal part mantissa<<=4; mantissa+=fraction; RCC->APB2ENR|=1<<2; //Enable PORTA port clock RCC->APB2ENR|=1<<14; //Enable serial port clock GPIOA->CRH&=0XFFFFF00F;//IO status settings GPIOA->CRH|=0X000008B0;//IO status settings RCC->APB2RSTR|=1<<14; //Reset serial port 1 RCC->APB2RSTR&=~(1<<14);//Stop reset //Baud rate setting USART1->BRR=mantissa; // Baud rate setting USART1->CR1|=0X200C; //1 bit stop, no check bit #if EN_USART1_RX // If reception is enabled //Enable receive interrupt USART1->CR1|=1<<5; //Receive buffer non air interrupt enable MY_NVIC_Init(3,3,USART1_IRQn,2);//Group 2, lowest priority #endif }
Write the following code in test.c:
#include "sys.h" #include "usart.h" #include "delay.h" int main(void) { u16 t; u16 len; u16 times=0; Stm32_Clock_Init(9); //System clock setting delay_init(72); //Delay initialization uart_init(72,115200); //The serial port is initialized to 115200 while(1) { if(USART_RX_STA&0x8000) { len=USART_RX_STA&0x3FFF;//Get the length of the data received this time printf("\r\n Hello Windows! \r\n\r\n"); for(t=0;t<len;t++) { USART1->DR=USART_RX_BUF[t]; while((USART1->SR&0X40)==0);//Wait for sending to end } printf("\r\n\r\n");//Insert wrap USART_RX_STA=0; }else { times++; if(times%200==0)printf("Hello Windows!\r\n"); delay_ms(10); } } }
Successfully burned after compilation
5, According to the above example, observe the waveforms of PA9 and PA10
6, Summary
This time I learned to use STM32CubeMX to directly generate code, which makes compilation more convenient and fast in the future.
7, Reference
STM32 minimum core board F103 serial communication USART_ vic_ to_ CSDN blog
Creating STM32 assembler based on MDK: serial port output Hello world_ssj925319 blog - CSDN blog