用汇编语言实现STM32的LED和USART

;
;
;
;
				AREA RESET, DATA, READONLY
				
				DCD 0x20000000 + 1024 ;0
				DCD	UserMain ;1
				DCD 0 ;NMI 2
				DCD 0 ;HardFault 3
				DCD 0 ;MemManage 4
				DCD 0 ; 5
				DCD 0 ; 6
				DCD 0 ; 7
				DCD 0 ; 8
				DCD 0 ; 9
				DCD 0 ; 10
				DCD 0 ; 11
				DCD 0 ; 12
				DCD 0 ; 13
				DCD 0 ; 14
				DCD SysTick_Handler ; 15
HelloString		DCB "Hello Word\n"
HelloEnd
HexTable		DCB "0123456789ABCDEF"
HexTableEnd

GPIOF_BASE		EQU		0x40021400
GPIOF_MODER		EQU		GPIOF_BASE + 0x00
GPIOF_OTYPER	EQU		GPIOF_BASE + 0x04
GPIOF_OSPEEDR	EQU		GPIOF_BASE + 0x08
GPIOF_PUPDR		EQU		GPIOF_BASE + 0x0C
GPIOF_IDR		EQU		GPIOF_BASE + 0x10
GPIOF_ODR 		EQU		GPIOF_BASE + 0x14
GPIOF_BSRR		EQU		GPIOF_BASE + 0x18
GPIOF_AFRL		EQU		GPIOF_BASE + 0x20
GPIOF_AFRH		EQU		GPIOF_BASE + 0x24
	
GPIOA_BASE 		EQU		0x40020000
GPIOA_MODER		EQU		GPIOA_BASE + 0x00		
GPIOA_OTYPER	EQU		GPIOA_BASE + 0x04
GPIOA_OSPEEDR	EQU		GPIOA_BASE + 0x08
GPIOA_PUPDR		EQU		GPIOA_BASE + 0x0C
GPIOA_IDR		EQU		GPIOA_BASE + 0x10
GPIOA_ODR		EQU		GPIOA_BASE + 0x14
GPIOA_BSRR		EQU		GPIOA_BASE + 0x18
GPIOA_AFRL		EQU		GPIOA_BASE + 0x20
GPIOA_AFRH		EQU		GPIOA_BASE + 0x24
	
USART1_BASE		EQU		0x40011000
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



RCC_BASE		EQU		0x40023800
RCC_CR			EQU		RCC_BASE + 0x00
RCC_PLLCFGR		EQU		RCC_BASE + 0x04
RCC_CFGR		EQU		RCC_BASE + 0x08
RCC_CIR			EQU		RCC_BASE + 0x0C
RCC_AHB1RSTR	EQU		RCC_BASE + 0x10
RCC_AHB2RSTR	EQU		RCC_BASE + 0x14
RCC_AHB3RSTR	EQU		RCC_BASE + 0x18
RCC_APB1RSTR	EQU		RCC_BASE + 0x20
RCC_APB2RRST	EQU		RCC_BASE + 0x24
RCC_AHB1ENR		EQU		RCC_BASE + 0x30
RCC_AHB2ENR		EQU		RCC_BASE + 0x34
RCC_AHB3ENR		EQU		RCC_BASE + 0x38
RCC_APB1ENR		EQU		RCC_BASE + 0x40
RCC_APB2ENR		EQU		RCC_BASE + 0x44
	
SYSTICK_CTRL	EQU		0xE000E010
SYSTICK_LOAD	EQU		0xE000E014
SYSTICK_VAL		EQU		0xE000E018



				THUMB
				PRESERVE8
				AREA _UserMain, CODE, READONLY
					
				ENTRY
UserMain		PROC
				EXPORT UserMain
					
				LDR R0, =LedConfig
				BLX R0
				
				LDR R0, =SysTickConfig
				BLX R0
				
				MOV R10, #200
				
				LDR R0, =UartConfig
				BLX R0
					
SendString		
				LDR R0, =HelloString
				LDR R1, =HelloEnd - HelloString
				BL UartSendString
				MOV R0, #0xAB
				BL UartSendHex
				B SendString
					
				ENDP	


UartSendHex		; R0
				PUSH {R1 - R3,LR}
				
				LDR R3, =HexTable
				MOV R4, R0
				LDR R2, =28
HexLoop
				LSR R1, R4, R2
				AND R1, #0x0F
				LDR R0, [R3, R1]
				PUSH {R1-R2}
				
				BL UartSendByte
				POP {R1-R2}
				CMP R2, #0
				BEQ HexExit
				SUB R2, #4
				B HexLoop
				
HexExit			
				POP {R1-R3, PC}

UartConfig		
				PUSH {LR}
				
				LDR R0, =RCC_AHB1ENR
				LDR R1, [R0]
				ORR R1, #0x01
				STR R1, [R0]
				
				LDR R0, =RCC_APB2ENR
				LDR R1, [R0]
				ORR R1, #0x10
				STR R1, [R0]
				
				LDR R0, =GPIOA_MODER
				LDR R1, [R0]
				BIC R1, #0x3C0000
				ORR R1, #0x280000
				STR R1, [R0]
				
				LDR R0, =GPIOA_OTYPER
				LDR R1, [R0]
				BIC R1, #0x600
				STR R1, [R0]
				
				LDR R0, =GPIOA_OSPEEDR
				LDR R1, [R0]
				ORR R1, #0x3C0000
				STR R1, [R0]
				
				LDR R0, =GPIOA_PUPDR
				LDR R1, [R0]
				BIC R1, #0x3C0000
				ORR R1, #0x140000
				STR R1, [R0]
				
				LDR R0, =GPIOA_AFRH
				LDR R1, [R0]
				BIC R1, #0xFF0
				ORR R1, #0x770
				STR R1, [R0]
				
				LDR R0, =USART1_CR1
				LDR R1, [R0]
				MOV R1, #0
				ORR R1, #0x08
				STR R1, [R0]
				
				LDR R0, =USART1_CR2
				MOV R1, #0
				STR R1, [R0]
				
				LDR R0, =USART1_CR3
				MOV R1, #0
				STR R1, [R0]
				
				LDR R0, =USART1_BRR
				LDR R1, =0x682
				STR R1, [R0]
				
				LDR R0, =USART1_CR1
				LDR R1, [R0]
				ORR R1, #0x2000
				STR R1, [R0]
				
				POP {PC}
				

UartSendByte	; R0
				PUSH {LR}
SendWait
				LDR R2, =USART1_SR
				LDR R1, [R2]
				TST R1, #0x80
				BEQ SendWait
				LDR R2, =USART1_DR
				STR R0, [R2]
				
				POP {PC}
				

UartSendString	; R0 R1

				PUSH {LR}
				
				MOV R2, R0
StringLoop				
				CMP R1, #0
				BEQ SendExit
				LDRB R0, [R2]
				PUSH {R1, R2}
				BL UartSendByte
				POP {R1, R2}
				SUB R1, #1
				ADD R2, #1
				B StringLoop
				
SendExit
				POP {PC}


SysTickConfig	
				PUSH {LR}
				
				LDR R0, =SYSTICK_LOAD
				LDR R1, =1999
				STR R1, [R0]
				
				LDR R0, =SYSTICK_VAL
				LDR R1, =0
				STR R1, [R0]
				
				LDR R0, =SYSTICK_CTRL
				LDR R1, [R0]
				BIC R1, #0x04
				ORR R1, #0x03
				STR R1, [R0]
				
				POP {PC}
				
				
LedConfig		
				PUSH {LR}
				
				LDR R0, =RCC_AHB1ENR
				LDR R1, [R0]
				ORR R1, #0x20
				STR R1, [R0]
				
				LDR R0, =GPIOF_MODER
				LDR R1, [R0]
				BIC R1, #0x3C0000
				ORR R1, #0x140000
				STR R1, [R0]
				
				LDR R0, =GPIOF_OTYPER
				LDR R1, [R0]
				BIC R1, #0x600
				STR R1, [R0]
				
				LDR R0, =GPIOF_OSPEEDR
				LDR R1, [R0]
				ORR R1, #0x3C0000
				STR R1, [R0]
				
				LDR R0, =GPIOF_PUPDR
				LDR R1, [R0]
				BIC R1, #0x3C0000
				ORR R1, #0x140000
				STR R1, [R0]
				
				LDR R0, =GPIOF_BSRR
				LDR R1, [R0]
				ORR R1, #0x600
				STR R1, [R0]
				
				POP{PC}



SysTick_Handler PROC
				EXPORT SysTick_Handler
				
				PUSH {LR}
				
				CMP R10, #0
				BNE Exit
				LDR R0, =GPIOF_ODR
				LDR R1, [R0]
				EOR R1, #0x600
				STR R1, [R0]
				MOV R10, #200
				
				POP {PC}
				
Exit			
				SUB R10, #1
				
				POP{PC}

				ENDP
				
				END








你可能感兴趣的:(STM32)