一、GPIO端口初始化
1.时钟配置
本次实验采用GPIOA、B、C三个端口。该三个端口都属于APB2总线
-
找到时钟使能寄存器映射基地址 -
找到端口偏移地址以及对应端口所在位置 -
使能对应端口时钟
#define RCC_APB2ENR *((unsigned volatile int*)0x40021018)
RCC_APB2ENR|=1<<2|1<<3|1<<4;
2.输入输出模式和输出速率设置
本次实验采用通用推挽输出模式,最高输出时钟频率2Mhz。分别用到A4、B5、C14三个引脚。其中A4、B5属于端口配置低寄存器偏移地址为0x00,C13属于端口配置高寄存器偏移地址为0x04。
- 找到GPIOx端口基地址
- 配置对应引脚寄存器,基地址+偏移量
#define GPIOA_CRL *((unsigned volatile int*)0x40010800)
#define GPIOB_CRL *((unsigned volatile int*)0x40010C00)
#define GPIOC_CRH *((unsigned volatile int*)0x40011004)
- 设置输出模式为推挽输出,输出速度为2Mhz
GPIOA_CRL&=0xFFF0FFFF;
GPIOA_CRL|=0x00020000;
GPIOB_CRL&=0xFF0FFFFF;
GPIOB_CRL|=0x00200000;
GPIOC_CRH&=0xFF0FFFFF;
GPIOC_CRH|=0x00200000;
二、代码实现
1.流水灯原理
本次实验采用三个灯实现,亮灯状态用1表示,灭灯状态用0表示。 初始状态为0 0 0, 状态一为1 0 0 状态二为0 1 0 状态三为0 0 1 状态三结束后继续进入状态一,一直循环达到流水灯效果。
2.c语言实现
代码如下
#include "stm32f10x.h"
#define RCC_APB2ENR *((unsigned volatile int*)0x40021018)
#define GPIOA_CRL *((unsigned volatile int*)0x40010800)
#define GPIOA_ODR *((unsigned volatile int*)0x4001080C)
#define GPIOB_CRL *((unsigned volatile int*)0x40010C00)
#define GPIOB_ODR *((unsigned volatile int*)0x40010C0C)
#define GPIOC_CRH *((unsigned volatile int*)0x40011004)
#define GPIOC_ODR *((unsigned volatile int*)0x4001100C)
void Delay()
{
u32 i=0;
for(;i<5000000;i++);
}
int main(void)
{
RCC_APB2ENR|=1<<2|1<<3|1<<4;
GPIOA_CRL&=0xFFF0FFFF;
GPIOA_CRL|=0x00020000;
GPIOA_ODR&=~(1<<4);
GPIOB_CRL&=0xFF0FFFFF;
GPIOB_CRL|=0x00200000;
GPIOB_ODR&=~(1<<5);
GPIOC_CRH&=0xF0FFFFFF;
GPIOC_CRH|=0x02000000;
GPIOC_ODR&=~(1<<14);
while(1){
GPIOA_ODR|=1<<4;
Delay();
GPIOA_ODR&=~(1<<4);
GPIOB_ODR|=1<<5;
Delay();
GPIOB_ODR&=~(1<<5);
GPIOC_ODR|=1<<14;
Delay();
GPIOC_ODR&=~(1<<14);
}
}
3.汇编代码
代码如下
AREA MYDATA, DATA
AREA MYCODE, CODE
ENTRY
EXPORT led
led
;使能A,B,C
ldr r0, =0x40021018
ldr r1, =0x0000001c
str r1, [r0]
;配置端口A4
ldr r0, =0x40010800
ldr r1, [r0]
bic r1, r1, #0x000f0000
orr r1, r1, #0x00010000
str r1, [r0]
;配置端口B5
ldr r0, =0x40010c00
ldr r1, [r0]
bic r1, r1, #0x00f00000
orr r1, r1, #0x00100000
str r1, [r0]
;配置端口C14
ldr r0, =0x40011004
ldr r1, [r0]
bic r1, r1, #0x0f000000
orr r1, r1, #0x01000000
str r1, [r0]
;初始为A4亮灯
ldr r0, =0x4001080c
ldr r1, =0x00000010
str r1, [r0]
ldr r0, =5000000;频率
ldr r1, =0
;循环亮灯
blink
add r1, r1, #1
cmp r1, r0
blt blink
;A4灭
ldr r1, =0x4001080c
ldr r2, [r1]
eor r2, r2, #0x00000010
str r2, [r1]
;B5亮
ldr r1, =0x40010c0c
ldr r2, [r1]
eor r2, r2, #0x00000020
str r2, [r1]
ldr r1, =0
blink1
add r1, r1, #1
cmp r1, r0
blt blink1
;B5灭
ldr r1, =0x40010c0c
ldr r2, [r1]
eor r2, r2, #0x00000020
str r2, [r1]
;C14亮
ldr r1, =0x4001100c
ldr r2, [r1]
eor r2, r2, #0x00004000
str r2, [r1]
ldr r1, =0
blink2
add r1, r1, #1
cmp r1, r0
blt blink2
;C14灭
ldr r1, =0x4001100c
ldr r2, [r1]
eor r2, r2, #0x00004000
str r2, [r1]
;A4亮
ldr r1, =0x4001080c
ldr r2, [r1]
eor r2, r2, #0x00000010
str r2, [r1]
ldr r1, =0
b blink
END
三、效果
四、总结
1、学习和理解STM32F103系列芯片的地址映射和寄存器映射原理 2、了解GPIO端口的初始化设置三步骤(时钟配置、输入输出模式设置、最大速率设置)
五、参考
1.https://blog.csdn.net/gelad_w/article/details/115555631 2.https://blog.csdn.net/qq_26174161/article/details/108210382
|