以 STM32最小系统核心板(STM32F103C8T6)+面板板+3只红绿蓝LED 搭建电路,使用GPIOB、GPIOC、GPIOD这3个端口控制LED灯(最高时钟2Mhz),轮流闪烁,间隔时长1秒。
一.程序设计思路
可以想象出,GPIO与时钟相关的寄存器都是很常用的寄存器,如果每一次操作这些IO口都需要看数据手册的话,太累,所以人家做芯片的把常用的寄存器对应的地址都设置好了,并放到一个头文件内,就是<stm32f10x.h>,修改main.c的代码,包含stm32f10x.h头文件。
1.GPIO模式
本次实验采用GPIOA、GPIOB、GPIOC三个端口。该三个端口都属于APB2总线
2.配置代码
在这里插入代码片#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)
extern void led(void);
void Delay()
{
u32 i=0;
for(;i < 5000000;i++);
}
int main(void)
{
led();
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);
Delay();
GPIOB_ODR|=1<<5;
Delay();
GPIOB_ODR&=~(1<<5);
Delay();
GPIOC_ODR|=1<<14;
Delay();
GPIOC_ODR&=~(1<<14);
Delay();
}
}
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
2.效果
视频
1.总结
这次博客的内容我花费了许多时间,但是也学到了非常多的知识
参考资料 STM32寄存器点亮流水灯的三种方法 STM32用汇编点亮LED灯 stm32寄存器实现流水灯
|