stm32F10xx_点灯进阶

//野火的视频总结

#ifndef #define #endif 防止该头文件被重复引用

具体用法
例如引用头文件stm32f10x.h

#ifndef __STM32F10X_H
#define __STM32F10X_H

头文件内容

#endif /* __STM32F10X_H */

main.c

#include "stm32f10x.h"
#include "stm32f10x_gpio.h"

int main(void)
{
#if 0
	//存储器映射,直接访问内存地址
	//打开APB2总线的时钟
	*(unsigned int *)0x40021018 |= (1 << 3);
	//配置IO口为输出模式
	*(unsigned int *)0x40010C00 &= ~(0x0f << (4*0));
	*(unsigned int *)0x40010C00 |= (1 << (4*0));
	//控制ODR寄存器PB0输出0
	*(unsigned int *)0x40010C00 &= ~(1 << 0);
	
#elif0
	//寄存器映射,内存地址被封装在了头文件中
	//打开APB2总线的时钟
	RCC_APB2ENR |= (1 << 3);
	//配置IO口为输出模式
	GPIOB_CRL &= ~(0x0f << (4*0));
	GPIOB_CRL |= (1 << (4*0));
	//控制ODR寄存器PB0输出0
	GPIOB_ODR &= ~(1 << 0);
	
#elif0
	/*寄存器映射要操作的寄存器太多了,继续封装成结构体,利用结构体的存储连续性。
	原来的IO口基址是32位的,只能访问4字节地址把要操作的IO口基址强制转换成
	结构体类型,即可访问整个结构体成员,即所有寄存器*/
	//打开APB2总线的时钟
	RCC->APB2ENR |= (1 << 3);
	//配置IO口为输出模式
	GPIOB->CRL &= ~(0x0f << (4*0));
	GPIOB->CRL |= (1 << (4*0));
	//控制ODR寄存器PB0输出0
	GPIOB->ODR &= ~(1 << 0);
	
#elif1
	//为了程序的可读性,把具体的操作继续封装成函数,或者调用标准库
	//打开APB2总线的时钟
	RCC->APB2ENR |= (1 << 3);
	//配置IO口为输出模式
	GPIOB->CRL &= ~(0x0f << (4*0));//清0
	GPIOB->CRL |= (1 << (4*0));
	//控制BSRR寄存器PB0置位1,即设置PB0为1,关LED
	set_BSRR(GPIOB,GPIO_Pin_0);
	//控制BRR寄存器PB0置位1,即设置PB0为0,开LED
	set_BRR(GPIOB,GPIO_Pin_0);
	
#endif

	return 0;
}

void SystemInit(void)
{
	//函数体为空,目的为了骗过编译器不报错
}

stm32f10x.h

#ifndef __STM32F10X_H
#define __STM32F10X_H
//用来存放STM32寄存器映射的代码

//外设  perirhral

#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_APB2ENR       *(unsigned int *)(RCC_BASE + 0x18)
	
#define GPIOB_CRL         *(unsigned int *)(GPIOB_BASE + 0x00)
#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)
	
typedef unsigned int uint32_t;
typedef unsigned short uint16_t;

typedef struct
{
	uint32_t CR;
	uint32_t CFGR;
	uint32_t CIR;
	uint32_t APB2RSTR;
	uint32_t APB1RSTR;
	uint32_t AHBENR;
	uint32_t APB2ENR;
	uint32_t APB1ENR;
	uint32_t BDCR;
	uint32_t CSR;
}RCC_Reg;

#define RCC ((RCC_Reg *)RCC_BASE)

typedef struct
{
	uint32_t CRL;
	uint32_t CRH;
	uint32_t IDR;
	uint32_t ODR;
	uint32_t BSRR;
	uint32_t BRR;
	uint32_t LCKR;
}GPIO_Reg;

#define GPIOB ((GPIO_Reg *)GPIOB_BASE)

#endif /* __STM32F10X_H */

stm32f10x_gpio.c

#include "stm32f10x_gpio.h"

void set_BSRR(GPIO_Reg *GPIOx,uint16_t GPIO_Pin)
{
	GPIOx->BSRR |= GPIO_Pin;
}

void set_BRR(GPIO_Reg *GPIOx,uint16_t GPIO_Pin)
{
	GPIOx->BRR |= GPIO_Pin;
}

stm32f10x_gpio.h

#ifndef __STM32F10X_GPIO_H
#define __STM32F10X_GPIO_H

#include "stm32f10x.h"

#define GPIO_Pin_0 		((uint16_t)0x0001)		//(0x0000 0001)b
#define GPIO_Pin_1 		((uint16_t)0x0002)		//(0x0000 0010)b
#define GPIO_Pin_2 		((uint16_t)0x0004)	  //(0x0000 0100)b
#define GPIO_Pin_3 		((uint16_t)0x0008)		//
#define GPIO_Pin_4 		((uint16_t)0x0010)		//
#define GPIO_Pin_5 		((uint16_t)0x0020)		//
#define GPIO_Pin_6 		((uint16_t)0x0040)  	//
#define GPIO_Pin_7 		((uint16_t)0x0080)		//
#define GPIO_Pin_8 		((uint16_t)0x0100)    //
#define GPIO_Pin_9 		((uint16_t)0x0200)		//
#define GPIO_Pin_10	    ((uint16_t)0x0400)		//	
#define GPIO_Pin_11 	((uint16_t)0x0800)		//
#define GPIO_Pin_12     ((uint16_t)0x1000)		//
#define GPIO_Pin_13 	((uint16_t)0x2000)		//
#define GPIO_Pin_14 	((uint16_t)0x4000)		//
#define GPIO_Pin_15 	((uint16_t)0x8000)		//
#define GPIO_Pin_ALL 	((uint16_t)0xFFFF)		//

void set_BSRR(GPIO_Reg *GPIOx,uint16_t GPIO_Pin);
void set_BRR(GPIO_Reg *GPIOx,uint16_t GPIO_Pin);
#endif /* __STM32F10X_GPIO_H */
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值