Table of contents
1. The purpose of the experiment
Second, the register realizes lighting
4. Observe the output waveform of the GPIO port
3. HAL library lights up LED running lights
(3) Configure the debugging interface
4. USART serial communication to send Hello world
1. USART function introduction
6. Observe the output waveform
1. The purpose of the experiment
Install stm32CubeMX, cooperate with Keil, and try to use the register address method (assembly or C, no limit) and HAL library to complete the following tasks:
1. Redo the last LED running light job, that is, use the GPIO port to complete the periodic flashing of 3 LED traffic lights.
2. Complete a STM32 USART serial communication program (the query method is sufficient, and the interrupt method is not required for the time being), requiring:
(1) Set the baud rate to 115200, 1 stop bit, no parity bit;
(2) The STM32 system continuously sends "hello windows!" to the host computer (win10). win10 uses the "serial port assistant" tool to receive.
In the absence of an oscilloscope, Keil's software simulation logic analyzer function can be used to observe the timing waveform of the pin, which is more convenient for dynamic tracking and debugging and locating code failure points. Please use this function to observe the output waveforms of the 3 GPIO ports in question 1 and the serial output waveforms in question 2, and analyze whether the timing status is correct or not, and what is the actual high-low level transition period (LED blinking period).
Second, the register realizes lighting
1. Project creation
Basically the same as the last experiment
Note that when this interface appears
edit
Just close this window
The selection of the chip needs to be replaced with STM32F103C8
2. Write code
Then add the .c program and write the following code
//--------------APB2???????------------------------ #define RCC_AP2ENR *((unsigned volatile int*)0x40021018) //----------------GPIOA????? ------------------------ #define GPIOA_CRL *((unsigned volatile int*)0x40010800) #define GPIOA_ORD *((unsigned volatile int*)0x4001080C) //----------------GPIOB????? ------------------------ #define GPIOB_CRH *((unsigned volatile int*)0x40010C04) #define GPIOB_ORD *((unsigned volatile int*)0x40010C0C) //----------------GPIOC????? ------------------------ #define GPIOC_CRH *((unsigned volatile int*)0x40011004) #define GPIOC_ORD *((unsigned volatile int*)0x4001100C) //-------------------???????----------------------- void Delay_wxc( volatile unsigned int t) { unsigned int i; while(t--) for (i=0;i<800;i++); } //------------------------???-------------------------- int main() { int j=100; RCC_AP2ENR|=1<<2; //APB2-GPIOA?????? RCC_AP2ENR|=1<<3; //APB2-GPIOB?????? RCC_AP2ENR|=1<<4; //APB2-GPIOC?????? //????????? RCC_APB2ENR|=1<<3|1<<4; GPIOA_CRL&=0x0FFFFFFF; //??? ?? GPIOA_CRL|=0x20000000; //PA7???? GPIOA_ORD|=1<<7; //??????? GPIOB_CRH&=0xFFFFFF0F; //??? ?? GPIOB_CRH|=0x00000020; //PB9???? GPIOB_ORD|=1<<9; //??????? GPIOC_CRH&=0x0FFFFFFF; //??? ?? GPIOC_CRH|=0x30000000; //PC15???? GPIOC_ORD|=0x1<<15; //??????? while(j) { GPIOA_ORD=0x0<<0; //PB0??? Delay_wxc(1000000); GPIOA_ORD=0x1<<0; //PB0??? Delay_wxc(1000000); GPIOB_ORD=0x0<<9; //PB9??? Delay_wxc(1000000); GPIOB_ORD=0x1<<9; //PB9??? Delay_wxc(1000000); GPIOC_ORD=0x0<<15; //PC15??? Delay_wxc(1000000); GPIOC_ORD=0x1<<15; //PC15??? Delay_wxc(1000000); } }
Note that in the Options switch to the Output interface
Check Generate hex file
Add driver files
[
edit
Copy it to the project directory
3. Burn and compile
Open mcuisp and upload the hex file generated in the project
Click to start programming
edit
4. Observe the output waveform of the GPIO port
The light of the pin is low level, the light of high level is off, and the high-low level conversion cycle (LED flashing cycle) is about 1.12s.
3. HAL library lights up LED running lights
1. Install the HAL library
After installing SMT32CubeMX, you also need to install a firmware library, because the chip I use is the STM32F103 series, so I choose the STM32F1 version for installation. After installation, the environment is set up.
2. Create a project
Click New Project to create a project
Select this, then select the chip and click Start Project
3. Configuration work
(1) Clock configuration
First select RCC in the system core, then set HSE (external high-speed clock) to Crystal/Ceramic Resonator (crystal/ceramic resonator)
Then, click Clock Configuration to enter the system clock tree settings. Since the highest clock of STM32 is 72MHz, you can set it as shown in the figure.
(2) GPIO configuration
First select GPIO in the system core, and then select the pins. The GPIO Output pins I selected are A1, B1, and C15. The default output mode is push-pull output, so no changes are required.
(3) Configure the debugging interface
Select SYS in System Core, select Debug as required, and select Serial Wire to complete the configuration.
Next, observe the clock architecture. The clock of the APB2 bus is controlled by hse. At the same time, the right side of PLLCLK must be selected on this interface.
Set hse there to Crystal/Ceramic Resonator
A total of three output s are selected, which are PA4, PB9, and PC15:
(4) Generate project
After the final completion, select the folder and project name, click GENERATE CODE to generate the project, and the generated program is in the project. Select to generate the initialization .c/.h file, then click generate code, select open project, and then go to KEIL53 Add code
Open the main.c file and slip down the part of the main function:
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET); HAL_Delay(500); HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_SET); HAL_Delay(500); HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET); HAL_Delay(500); HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET); HAL_Delay(500); HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15,GPIO_PIN_RESET); HAL_Delay(500); HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15,GPIO_PIN_SET); HAL_Delay(500);
4. USART serial communication to send Hello world
1. USART function introduction
USART (Universal Synchronous/Asynchronous Receiver/Transmitter) is a full-duplex universal synchronous and asynchronous serial transceiver module. This interface is a highly flexible serial communication device. The USART transceiver module is generally divided into three parts: clock generator, data transmitter and receiver. Control registers are shared by all modules.
2. Code implementation
Create a project with keli and add assembly code
Note that there is no need to check here because of assembly language.
Add assembly file (.s)
add code
;RCC register address map RCC_BASE EQU 0x40021000 RCC_CR EQU (RCC_BASE + 0x00) RCC_CFGR EQU (RCC_BASE + 0x04) RCC_CIR EQU (RCC_BASE + 0x08) RCC_APB2RSTR EQU (RCC_BASE + 0x0C) RCC_APB1RSTR EQU (RCC_BASE + 0x10) RCC_AHBENR EQU (RCC_BASE + 0x14) RCC_APB2ENR EQU (RCC_BASE + 0x18) RCC_APB1ENR EQU (RCC_BASE + 0x1C) RCC_BDCR EQU (RCC_BASE + 0x20) RCC_CSR EQU (RCC_BASE + 0x24) ;AFIO register address map AFIO_BASE EQU 0x40010000 AFIO_EVCR EQU (AFIO_BASE + 0x00) AFIO_MAPR EQU (AFIO_BASE + 0x04) AFIO_EXTICR1 EQU (AFIO_BASE + 0x08) AFIO_EXTICR2 EQU (AFIO_BASE + 0x0C) AFIO_EXTICR3 EQU (AFIO_BASE + 0x10) AFIO_EXTICR4 EQU (AFIO_BASE + 0x14) ;GPIOA register address map GPIOA_BASE EQU 0x40010800 GPIOA_CRL EQU (GPIOA_BASE + 0x00) GPIOA_CRH EQU (GPIOA_BASE + 0x04) GPIOA_IDR EQU (GPIOA_BASE + 0x08) GPIOA_ODR EQU (GPIOA_BASE + 0x0C) GPIOA_BSRR EQU (GPIOA_BASE + 0x10) GPIOA_BRR EQU (GPIOA_BASE + 0x14) GPIOA_LCKR EQU (GPIOA_BASE + 0x18) ;GPIO C mouth control GPIOC_BASE EQU 0x40011000 GPIOC_CRL EQU (GPIOC_BASE + 0x00) GPIOC_CRH EQU (GPIOC_BASE + 0x04) GPIOC_IDR EQU (GPIOC_BASE + 0x08) GPIOC_ODR EQU (GPIOC_BASE + 0x0C) GPIOC_BSRR EQU (GPIOC_BASE + 0x10) GPIOC_BRR EQU (GPIOC_BASE + 0x14) GPIOC_LCKR EQU (GPIOC_BASE + 0x18) ;Serial 1 control USART1_BASE EQU 0x40013800 USART1_SR EQU (USART1_BASE + 0x00) USART1_DR EQU (USART1_BASE + 0x04) USART1_BRR EQU (USART1_BASE + 0x08) USART1_CR1 EQU (USART1_BASE + 0x0c) USART1_CR2 EQU (USART1_BASE + 0x10) USART1_CR3 EQU (USART1_BASE + 0x14) USART1_GTPR EQU (USART1_BASE + 0x18) ;NVIC register address NVIC_BASE EQU 0xE000E000 NVIC_SETEN EQU (NVIC_BASE + 0x0010) ;SETENA start address of register array NVIC_IRQPRI EQU (NVIC_BASE + 0x0400) ;start address of the interrupt priority register array NVIC_VECTTBL EQU (NVIC_BASE + 0x0D08) ;address of vector table offset register NVIC_AIRCR EQU (NVIC_BASE + 0x0D0C) ;Application interrupt and reset control register address SETENA0 EQU 0xE000E100 SETENA1 EQU 0xE000E104 ;SysTick register address SysTick_BASE EQU 0xE000E010 SYSTICKCSR EQU (SysTick_BASE + 0x00) SYSTICKRVR EQU (SysTick_BASE + 0x04) ;FLASH Buffer register address map FLASH_ACR EQU 0x40022000 ;SCB_BASE EQU (SCS_BASE + 0x0D00) MSP_TOP EQU 0x20005000 ;Main stack start value PSP_TOP EQU 0x20004E00 ;Process stack start value BitAlias_BASE EQU 0x22000000 ;Bit-band alias area start address Flag1 EQU 0x20000200 b_flas EQU (BitAlias_BASE + (0x200*32) + (0*4)) ;bit address b_05s EQU (BitAlias_BASE + (0x200*32) + (1*4)) ;bit address DlyI EQU 0x20000204 DlyJ EQU 0x20000208 DlyK EQU 0x2000020C SysTim EQU 0x20000210 ;constant definition Bit0 EQU 0x00000001 Bit1 EQU 0x00000002 Bit2 EQU 0x00000004 Bit3 EQU 0x00000008 Bit4 EQU 0x00000010 Bit5 EQU 0x00000020 Bit6 EQU 0x00000040 Bit7 EQU 0x00000080 Bit8 EQU 0x00000100 Bit9 EQU 0x00000200 Bit10 EQU 0x00000400 Bit11 EQU 0x00000800 Bit12 EQU 0x00001000 Bit13 EQU 0x00002000 Bit14 EQU 0x00004000 Bit15 EQU 0x00008000 Bit16 EQU 0x00010000 Bit17 EQU 0x00020000 Bit18 EQU 0x00040000 Bit19 EQU 0x00080000 Bit20 EQU 0x00100000 Bit21 EQU 0x00200000 Bit22 EQU 0x00400000 Bit23 EQU 0x00800000 Bit24 EQU 0x01000000 Bit25 EQU 0x02000000 Bit26 EQU 0x04000000 Bit27 EQU 0x08000000 Bit28 EQU 0x10000000 Bit29 EQU 0x20000000 Bit30 EQU 0x40000000 Bit31 EQU 0x80000000 ;vector table AREA RESET, DATA, READONLY DCD MSP_TOP ;Initialize the main stack DCD Start ;reset vector DCD NMI_Handler ;NMI Handler DCD HardFault_Handler ;Hard Fault Handler DCD 0 DCD 0 DCD 0 DCD 0 DCD 0 DCD 0 DCD 0 DCD 0 DCD 0 DCD 0 DCD 0 DCD SysTick_Handler ;SysTick Handler SPACE 20 ;20 bytes of reserved space ;code snippet AREA |.text|, CODE, READONLY ;main program starts ENTRY ;Instructs the program to execute from here Start ;Clock System Settings ldr r0, =RCC_CR ldr r1, [r0] orr r1, #Bit16 str r1, [r0] ;Enable external crystal oscillator ;start external 8 M crystal oscillator ClkOk ldr r1, [r0] ands r1, #Bit17 beq ClkOk ;Wait for the external crystal to be ready ldr r1,[r0] orr r1,#Bit17 str r1,[r0] ;FLASH buffer ldr r0, =FLASH_ACR mov r1, #0x00000032 str r1, [r0] ;set up PLL The phase-locked loop multiplier is 7,HSE input is not divided ldr r0, =RCC_CFGR ldr r1, [r0] orr r1, #(Bit18 :OR: Bit19 :OR: Bit20 :OR: Bit16 :OR: Bit14) orr r1, #Bit10 str r1, [r0] ;start up PLL phase locked loop ldr r0, =RCC_CR ldr r1, [r0] orr r1, #Bit24 str r1, [r0] PllOk ldr r1, [r0] ands r1, #Bit25 beq PllOk ;choose PLL clock as system clock ldr r0, =RCC_CFGR ldr r1, [r0] orr r1, #(Bit18 :OR: Bit19 :OR: Bit20 :OR: Bit16 :OR: Bit14) orr r1, #Bit10 orr r1, #Bit1 str r1, [r0] ;other RCC Related settings ldr r0, =RCC_APB2ENR mov r1, #(Bit14 :OR: Bit4 :OR: Bit2) str r1, [r0] ;IO port settings ldr r0, =GPIOC_CRL ldr r1, [r0] orr r1, #(Bit28 :OR: Bit29) ;PC.7 output mode,max speed 50 MHz and r1, #(~Bit30 & ~Bit31) ;PC.7 Universal push-pull output mode str r1, [r0] ;PA9 Serial port 0 transmitter pin ldr r0, =GPIOA_CRH ldr r1, [r0] orr r1, #(Bit4 :OR: Bit5) ;PA.9 output mode,max speed 50 MHz orr r1, #Bit7 and r1, #~Bit6 ;10: Alternate function push-pull output mode str r1, [r0] ldr r0, =USART1_BRR mov r1, #0x271 str r1, [r0] ;Configure the baud rate-> 115200 ldr r0, =USART1_CR1 mov r1, #0x200c str r1, [r0] ;USART The module is always enabled Send and receive enable ;71 02 00 00 2c 20 00 00 ;AFIO parameter settings ;Systick parameter settings ldr r0, =SYSTICKRVR ;Systick Install the initial value mov r1, #9000 str r1, [r0] ldr r0, =SYSTICKCSR ;set up,start up Systick mov r1, #0x03 str r1, [r0] ;NVIC ;ldr r0, =SETENA0 ;mov r1, 0x00800000 ;str r1, [r0] ;ldr r0, =SETENA1 ;mov r1, #0x00000100 ;str r1, [r0] ;Switch to user-level line program mode ldr r0, =PSP_TOP ;Initialize thread stack msr psp, r0 mov r0, #3 msr control, r0 ;initialization SRAM register mov r1, #0 ldr r0, =Flag1 str r1, [r0] ldr r0, =DlyI str r1, [r0] ldr r0, =DlyJ str r1, [r0] ldr r0, =DlyK str r1, [r0] ldr r0, =SysTim str r1, [r0] ;main loop main ldr r0, =Flag1 ldr r1, [r0] tst r1, #Bit1 ;SysTick yields 0.5s,Position bit 1 beq main ;0.5s flag not yet set ;0.5s flag is set ldr r0, =b_05s ;bit-band operation clears 0.5s logo mov r1, #0 str r1, [r0] bl LedFlas mov r0, #'H' bl send_a_char mov r0, #'e' bl send_a_char mov r0, #'l' bl send_a_char mov r0, #'l' bl send_a_char mov r0, #'o' bl send_a_char mov r0, #' ' bl send_a_char mov r0, #'w' bl send_a_char mov r0, #'i' bl send_a_char mov r0, #'n' bl send_a_char mov r0, #'d' bl send_a_char mov r0, #'o' bl send_a_char mov r0, #'w' bl send_a_char mov r0, #'s' bl send_a_char mov r0, #'!' bl send_a_char mov r0, #'\n' bl send_a_char b main ;Subroutine Serial port 1 sends a character send_a_char push {r0 - r3} ldr r2, =USART1_DR str r0, [r2] b1 ldr r2, =USART1_SR ldr r2, [r2] tst r2, #0x40 beq b1 ;send completed(Transmission complete)wait pop {r0 - r3} bx lr ;subroutine led flicker LedFlas push {r0 - r3} ldr r0, =Flag1 ldr r1, [r0] tst r1, #Bit0 ;bit0 flashing flag beq ONLED ;open at 0 led lamp ;1 off led lamp ldr r0, =b_flas mov r1, #0 str r1, [r0] ;Blinking flag position is 0,The next state is to turn on the light ;PC.7 output 0 ldr r0, =GPIOC_BRR ldr r1, [r0] orr r1, #Bit7 str r1, [r0] b LedEx ONLED ;open at 0 led lamp ldr r0, =b_flas mov r1, #1 str r1, [r0] ;Blinking flag position is 1,The next state is to turn off the lights ;PC.7 output 1 ldr r0, =GPIOC_BSRR ldr r1, [r0] orr r1, #Bit7 str r1, [r0] LedEx pop {r0 - r3} bx lr ;exception program NMI_Handler bx lr HardFault_Handler bx lr SysTick_Handler ldr r0, =SysTim ldr r1, [r0] add r1, #1 str r1, [r0] cmp r1, #500 bcc TickExit mov r1, #0 str r1, [r0] ldr r0, =b_05s ;Greater than or equal to 500 times Clear the clock tick counter Set 0.5s flag bit ;bit band operation set to 1 mov r1, #1 str r1, [r0] TickExit bx lr ALIGN ;by using zero or empty instructions NOP filling,to align the current position with a specified boundary END
to compile
5. Burning operation
1. Instrument preparation
1. One piece of stm32 core board 103f
2, usb to serial port
3, a breadboard, a number of wires
2 Instrument connection
GND-G
3V3-3.3
RXD-A10
TXD-A9
3 Burn
6. Observe the output waveform
In the absence of an oscilloscope, Keil's software simulation logic analyzer function can be used to observe the timing waveform of the pin, which is more convenient for dynamic tracking and debugging and locating code failure points. Please use this function to observe the output waveforms of the three GPIO ports in question 1, and analyze whether the timing states reflected by the waveforms are correct or not, and what is the actual high-low level transition period (LED blinking period).
1. Set the emulation mode
When using the simulation mode, you need to set the Debug mode. The following figure shows the Debug setting mode.
2. Use a logic analyzer
Be sure to set Display Type to BIt!
Waveform
7. References
usart serial communication of STM32 - Programmer Sought
Use register & HAL library to complete LED running light program_weixin_45203491's blog - CSDN blog