STM32学习笔记——点亮一个LED灯

STM32学习笔记——点亮一个LED灯

  • 用寄存器点亮LED0
    • 1.硬件电路
    • 2.地址操作
    • 3.寄存器映射操作

用寄存器点亮LED0

1.硬件电路

STM32学习笔记——点亮一个LED灯_第1张图片
由硬件电路图可知,LED0对应端口PB5

STM32学习笔记——点亮一个LED灯_第2张图片
通过此图可知,当PB5口清零时,LED0就会亮起。

2.地址操作

int main()
{
	//打开GPIOB端口时钟
	*(unsigned int*)0x40021018 |= (1<<3);
	
	//配置PB5为输出模式 
	*(unsigned int*)0x40010C00 &= ~((0x0f)<<(5*4));
	*(unsigned int*)0x40010C00 |= ((1)<<(5*4));
	
	//使得PB5_ODR=0,PB5端口置为0
	*(unsigned int*)0x40010C0C &= ~(1<<5);
}

PB5端口清零分为三步:

  1. 打开端口时钟RCC(挂在AHB总线上)
  2. 配置对应端口为输出模式
  3. 将端口输出为0

3.寄存器映射操作

//stm32f10x.h
//用于存放stm32寄存器映射地址
#define	PERIPH_BASE			((unsigned int)0x40000000)
#define APB1PERIPH_BASE		PERIPH_BASE
#define APB2PERIPH_BASE		(PERIPH_BASE + 0x10000)
#define AHBPERIPH_BASE		(PERIPH_BASE + 0x20000)

#define RCC_BASE				(AHBPERIPH_BASE + 0x1000)
#define GPIOB_BASE				(APB2PERIPH_BASE + 0x0C00)

#define RCC_PB2ENR				*(unsigned int*)(RCC_BASE + 0x18)
#define GPIOB_CRL				*(unsigned int*)GPIOB_BASE
#define GPIOB_CRH				*(unsigned int*)(GPIOB_BASE + 0x04)
#define GPIOB_IDR				*(unsigned int*)(GPIOB_BASE + 0x08)
#define GPIOB_ODR				*(unsigned int*)(GPIOB_BASE + 0x0C)
#define GPIOB_BSRR				*(unsigned int*)(GPIOB_BASE + 0x10)
#define GPIOB_BRR				*(unsigned int*)(GPIOB_BASE + 0x14)
#define GPIOB_LCKR				*(unsigned int*)(GPIOB_BASE + 0x18)
//main.c
#include "stm32f10x.h"
int main()
{
	//打开GPIOB端口时钟
	RCC_PB2ENR |= (1<<3);
	
	//配置PB5为输出模式 
	GPIOB_CRL &= ~((0x0f)<<(5*4));
	GPIOB_CRL |= ((1)<<(5*4));
	
	//使得PB5_ODR=0,PB5端口置为0
	GPIOB_ODR &= ~(1<<5);

	//PB5端口置为0也可以使用位清零寄存器
	//GPIOB_BRR |= (1<<5);
}

未完待续…


你可能感兴趣的:(STM32学习笔记)