提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
一、GPIO端口初始化
1.时钟配置
1) 通过查询STM32中文参考手册查询时钟使能(RCC) 寄存器映射基地址 2) 本次流水灯采用GPIOA、GPIOB、GPIOC三个端口,位于APB2总线,如下图: 3) 找到对应端口总线的外设时钟使能寄存器偏移地址
查询可知GPIOA、GPIOB、GPIOC位于APB2总线上,偏移地址为:0x18
4) 使能对应端口时钟
#define RCC_APB2ENR *((unsigned volatile int*)0x40021018)
RCC_APB2ENR|=1<<2|1<<3|1<<4;
2.配置输入输出模式
本流水灯采用通用推挽输出模式,并设置输出时钟频率2Mhz,使用端口A12、B1、C14。并通过手册查询使用的GPIO端口对应的地址: 1) GPIOx端口基地址:
2) 配置端口配置低寄存器B1: 查询操作手册可知: 基地址+偏移量
#define GPIOB_CRL *((unsigned volatile int*)0x40010C00)
#define GPIOB_ODR *((unsigned volatile int*)0x40010C0C)
3) 配置端口配置高寄存器A12、C14: 还是基地址+偏移量
#define GPIOA_CRL *((unsigned volatile int*)0x40010804)
#define GPIOA_ODR *((unsigned volatile int*)0x4001080C)
#define GPIOC_CRH *((unsigned volatile int*)0x40011004)
#define GPIOC_ODR *((unsigned volatile int*)0x4001100C)
4) 设置所有的端口输出模式为推挽模式,输出速度为2Mhz
GPIOA_CRH&=0xFFF0FFFF;
GPIOA_CRH|=0x00020000;
GPIOB_CRL&=0xFFFFFF0F;
GPIOB_CRL|=0x00000020;
GPIOC_CRH&=0xF0FFFFFF;
GPIOC_CRH|=0x02000000;
二、实现代码
1.流水灯原理解释
通过控制同一时间不同颜色led灯的高低电平状态,实现流水灯。 状态1:A12亮、B1灭、C14灭 状态2:A12灭、B1亮、C14灭 状态3:A12灭、B1灭、C14亮 状态3结束后循环进行即可达到效果
1.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_CRH&=0xFFF0FFFF;
GPIOA_CRH|=0x00020000;
GPIOA_ODR&=~(1<<12);
GPIOB_CRL&=0xFFFFFF0F;
GPIOB_CRL|=0x00000020;
GPIOB_ODR&=~(1<<1);
GPIOC_CRH&=0xF0FFFFFF;
GPIOC_CRH|=0x02000000;
GPIOC_ODR&=~(1<<14);
while(1){
GPIOA_ODR|=1<<12;
Delay();
GPIOA_ODR&=~(1<<12);
GPIOB_ODR|=1<<1;
Delay();
GPIOB_ODR&=~(1<<1);
GPIOC_ODR|=1<<14;
Delay();
GPIOC_ODR&=~(1<<14);
}
}
2.汇编方式实现
代码如下:
AREA MYDATA, DATA
AREA MYCODE, CODE
ENTRY
EXPORT led
led
;使能A,B,C
ldr r0, =0x40021018
ldr r1, =0x0000001c
str r1, [r0]
;配置端口A12
ldr r0, =0x40010804
ldr r1, [r0]
bic r1, r1, #0x000f0000
orr r1, r1, #0x00010000
str r1, [r0]
;配置端口B1
ldr r0, =0x40010c00
ldr r1, [r0]
bic r1, r1, #0x000000f0
orr r1, r1, #0x00000010
str r1, [r0]
;配置端口C14
ldr r0, =0x40011004
ldr r1, [r0]
bic r1, r1, #0x0f000000
orr r1, r1, #0x01000000
str r1, [r0]
;初始为A12亮灯
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]
;B1亮
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
;B1灭
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]
;A12亮
ldr r1, =0x4001080c
ldr r2, [r1]
eor r2, r2, #0x00000010
str r2, [r1]
ldr r1, =0
b blink
END
三、效果演示
总结
GPIO点亮流水灯需要首先打开GPOI时钟然后配置其输出模式和输出频率,最后通过3个状态调整led灯的高低电平,不断循环,即可实现流水灯
参考
https://blog.csdn.net/qq_47281915/article/details/120812867?spm=1001.2014.3001.5501 https://blog.csdn.net/geek_monkey/article/details/86293880 STM32中文参考手册
|