STM32笔记4:基本库函数写法(结构体指针的应用)

//构建库函数雏形
//peripheral:外设
#include"stm32f4xx.h"

//peripheral
#define PERIPHERAL	((unsigned int)(0x40000000))
#define AHB1_BASE	((unsigned int)(PERIPHERAL + 0x0002 0000))
#define GPIOA_BASE	((unsigned int)(AHB1_BASE+0x0000 0000))
#define GPIOB_BASE	((unsigned int)(AHB1_BASE+0x0002 0400))
#define GPIOC_BASE	((unsigned int)(AHB1_BASE+0x0002 0800))
#define GPIOD_BASE	((unsigned int)(AHB1_BASE+0x0002 0c00))
#define GPIOE_BASE	((unsigned int)(AHB1_BASE+0x0002 1000))
#define GPIOF_BASE	((unsigned int)(AHB1_BASE+0x0002 1400)
#define GPIOG_BASE	((unsigned int)(AHB1_BASE+0x0002 1800))
#define GPIOH_BASE	((unsigned int)(AHB1_BASE+0x0002 1C00))
#define GPIOI_BASE	((unsigned int)(AHB1_BASE+0x0002 2000))


															// #define GPIOH_MODER 	*(unsigned int*)(GPIOH_BASE+0x00)
															// #define GPIOH_OTYPER 	*(unsigned int*)(GPIOH_BASE+0x04)
															// #define GPIOH_OSPEEDR	*(unsigned int*)(GPIOH_BASE+0x08)
															// #define GPIOH_PUPDR 	*(unsigned int*)(GPIOH_BASE+0x0c)
															// #define GPIOH_IDR		*(unsigned int*)(GPIOH_BASE+0x10)
															// #define GPIOH_ODR 		*(unsigned int*)(GPIOH_BASE+0x14)
															// #define GPIOH_BSRR 		*(unsigned int*)(GPIOH_BASE+0x18)
															// #define GPIOH_LCKR 		*(unsigned int*)(GPIOH_BASE+0x1c)
															// #define GPIOH_AFRL		*(unsigned int*)(GPIOH_BASE+0x20)
															// #define GPIOH_AFRH 		*(unsigned int*)(GPIOH_BASE+0x24)

typedef unsigned int	uint32_t;
typedef unsigned int 	uint16_t;

typedef struct
{
	uint32_t MODER;
	uint32_t OTYPER;
	uint32_t OSPEEDR;
	uint32_t PUPDR;
	uint32_t IDR;
	uint32_t ODR;
	uint16_t BSRRL;
	uint16_t BSSRH;
	uint32_t LCKR;
	uint32_t AFR[2];	
}GPIO_Typedef;

//将GPIOA_BASE变成一个结构体指针
#define GPIOA ((GPIO_Typedef *)(GPIOA_BASE))
#define GPIOB ((GPIO_Typedef *)(GPIOB_BASE))
#define GPIOC ((GPIO_Typedef *)(GPIOC_BASE))
#define GPIOD ((GPIO_Typedef *)(GPIOD_BASE))
#define GPIOE ((GPIO_Typedef *)(GPIOE_BASE))
#define GPIOF ((GPIO_Typedef *)(GPIOF_BASE))
#define GPIOG ((GPIO_Typedef *)(GPIOG_BASE))
#define GPIOH ((GPIO_Typedef *)(GPIOH_BASE))
#define GPIOI ((GPIO_Typedef *)(GPIOI_BASE))
 
void GPIO_SetBits(GPIO_Typedef *GPIOx, uint16_t GPIO_Pin);
void GPIO_RetsetBits(GPIO_Typedef *GPIOx, uint16_t GPIO_Pin);

 int main(void)
{
																		// RCCAHB1PERIPH_BASE |= (1<<7);
																		 
																		// GPIOH_MODER &= ~(3<<2*10); 
																		// GPIOH_MODER |= (1<<2*10); 
																	 
																		// GPIOH->MODER &= ~(3<<2*10);
																		// GPIO->MODER |= (1<<2*10);
																		// PH10输出低电平
																		// GPIOH_ODR &= ~(1<<10);

	RCC_AHB1ENR |= (1<<7);
	GPIO_SetBits(GPIOH, 10);	//若想要找到GPIOH的definition,若是找不到,可能是keil的options for target中的output中的Brows information 没有勾上。
	GPIO_ReSetBits(GPIOH, 10);
	
	while(1);
}

/*	函数功能:设置引脚为高电平
	参数说明:GPIOx:结构体指针,指向GPIO端口。GPIO_Pin:要被置位引脚号

*/
void GPIO_SetBits(GPIO_Typedef *GPIOx, uint16_t GPIO_Pin)
{
	GPIOx->BSSRL |= (1<<GPIO_Pin)
}

/*	函数功能:设置引脚为低电平
	参数说明:GPIOx:结构体指针,指向GPIO端口。GPIO_Pin:要被置位引脚号

*/
void GPIO_ResetBits(GPIO_Typedef *GPIOx, uint16_t GPIO_Pin)
{
	GPIOx->BSSRL |= (1<<GPIO_Pin)
}

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值