以下是一个简单的汇编程序,用于在STM32F103微控制器上实现流水灯效果。这个程序使用了汇编语言来直接控制GPIO口以控制LED的亮灭。
; STM32F103 LED Blinking Assembly Program
; Author: ChatGPT
; Date: April 2024
; Define STM32F103 register addresses
GPIOC_BASE equ 0x40011000 ; GPIOC base address
RCC_APB2ENR equ 0x40021018 ; RCC APB2 Enable Register address
; Define GPIOC registers offsets
GPIOC_CRL equ 0x00 ; GPIOC Port Configuration Register Low
GPIOC_CRH equ 0x04 ; GPIOC Port Configuration Register High
GPIOC_ODR equ 0x0C ; GPIOC Output Data Register
; Define bit masks
GPIOC13 equ (1 << 13) ; GPIOC Pin 13
; Define delay subroutine
Delay:
ldr r0, =0x3FFFFF ; Load delay value
Loop:
subs r0, #1 ; Decrement delay counter
bne Loop ; Repeat until zero
bx lr ; Return
; Main program
.section .text
.global _start
_start:
; Enable GPIOC clock
ldr r1, =RCC_APB2ENR ; Load RCC_APB2ENR address into r1
ldr r0, [r1] ; Load RCC_APB2ENR value into r0
orr r0, r0, #(1 << 4) ; Set bit 4 (IOPCEN) to enable GPIOC clock
str r0, [r1] ; Write back to RCC_APB2ENR
; Configure GPIOC Pin 13 as output
ldr r1, =GPIOC_BASE ; Load GPIOC base address into r1
ldr r0, [r1, #GPIOC_CRH] ; Load GPIOC_CRH value into r0
bic r0, r0, #(0xF << 20) ; Clear bits 20-23 to configure PC13 as output
orr r0, r0, #(0x3 << 20) ; Set bits 20-23 to configure PC13 as output push-pull
str r0, [r1, #GPIOC_CRH] ; Write back to GPIOC_CRH
Loop:
; Turn on LED (set PC13 high)
ldr r0, =GPIOC13 ; Load GPIOC13 bitmask into r0
ldr r1, =GPIOC_BASE ; Load GPIOC base address into r1
ldr r2, [r1, #GPIOC_ODR] ; Load GPIOC_ODR value into r2
orr r2, r2, r0 ; Set PC13 to high
str r2, [r1, #GPIOC_ODR] ; Write back to GPIOC_ODR
; Delay
bl Delay
; Turn off LED (set PC13 low)
ldr r0, =GPIOC13 ; Load GPIOC13 bitmask into r0
ldr r1, =GPIOC_BASE ; Load GPIOC base address into r1
ldr r2, [r1, #GPIOC_ODR] ; Load GPIOC_ODR value into r2
bic r2, r2, r0 ; Set PC13 to low
str r2, [r1, #GPIOC_ODR] ; Write back to GPIOC_ODR
; Delay
bl Delay
; Repeat forever
b Loop
; End of program
|