1、GIPO端口初始化
本次实验采用GPIOA、GPIOB、GPIOC三个端口。该三个端口都属于APB2总线
找到端口偏移地址以及对应端口所在位置?
-
//----------------APB2使能时钟寄存器 ---------------------
#define RCC_APB2ENR *((unsigned volatile int*)0x40021018)
RCC_APB2ENR|=1<<2|1<<3|1<<4; //APB2-GPIOA、GPIOB、GPIOC外设时钟使能
?本次实验采用通用推挽输出模式,最高输出时钟频率2Mhz。分别用到A4、B5、C14三个引脚。其中A4、B5属于端口配置低寄存器偏移地址为0x00,C13属于端口配置高寄存器偏移地址为0x04。
?
//----------------GPIOA配置寄存器 -----------------------
#define GPIOA_CRL *((unsigned volatile int*)0x40010800)
//----------------GPIOB配置寄存器 -----------------------
#define GPIOB_CRL *((unsigned volatile int*)0x40010C00)
//----------------GPIOC配置寄存器 -----------------------
#define GPIOC_CRH *((unsigned volatile int*)0x40011004)
?
GPIOA_CRL&=0xFFF0FFFF; //设置位 清零
GPIOA_CRL|=0x00020000; //PA4推挽输出,把第19、18、17、16位变为0010
GPIOB_CRL&=0xFF0FFFFF; //设置位 清零
GPIOB_CRL|=0x00200000; //PB5推挽输出,把第23、22、21、20变为0010
GPIOC_CRH&=0xFF0FFFFF; //设置位 清零
GPIOC_CRH|=0x00200000; //PC14推挽输出,把第23、22、21、20变为0010
实现代码
C语言
#include "stm32f10x.h"
//----------------APB2??????? ---------------------
#define RCC_APB2ENR *((unsigned volatile int*)0x40021018)
//----------------GPIOA????? -----------------------
#define GPIOA_CRL *((unsigned volatile int*)0x40010800)
#define GPIOA_ODR *((unsigned volatile int*)0x4001080C)
//----------------GPIOB????? -----------------------
#define GPIOB_CRL *((unsigned volatile int*)0x40010C00)
#define GPIOB_ODR *((unsigned volatile int*)0x40010C0C)
//----------------GPIOC????? -----------------------
#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; //APB2-GPIOA?GPIOB?GPIOC??????
GPIOA_CRL&=0xFFF0FFFF; //??? ??
GPIOA_CRL|=0x00020000; //PB5????
GPIOA_ODR&=~(1<<4); //???????
GPIOB_CRL&=0xFF0FFFFF; //??? ??
GPIOB_CRL|=0x00200000; //PB5????
GPIOB_ODR&=~(1<<5); //???????
GPIOC_CRH&=0xF0FFFFFF; //??? ??
GPIOC_CRH|=0x02000000; //PB5????
GPIOC_ODR&=~(1<<14); //???????
while(1){
//A?
GPIOA_ODR|=1<<4; //PB5???
Delay();
GPIOA_ODR&=~(1<<4); //PB5???,????0,??????
Delay();
//B?
GPIOB_ODR|=1<<5; //PB5???
Delay();
GPIOB_ODR&=~(1<<5); //PB5???,????0,??????
Delay();
//C?
GPIOC_ODR|=1<<14; //PB5???
Delay();
GPIOC_ODR&=~(1<<14); //PB5???,????0,??????
Delay();
}
}
汇编代码
部分参考
https://blog.csdn.net/m0_51120713/article/details/120832645?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163478608116780274151410%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=163478608116780274151410&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-3-120832645.pc_search_result_control_group&utm_term=STM32F103%E5%AF%84%E5%AD%98%E5%99%A8%E6%96%B9%E5%BC%8F%E7%82%B9%E4%BA%AELED%E6%B5%81%E6%B0%B4%E7%81%AF&spm=1018.2226.3001.4187
|