如何写BSRR的置位----复位的库函数:
1:在最初的时候需要先定义GPIO的结构体
#ifndef _STM32F4XX_H
#define _STM32F4XX_H
#include <stdint.h>
#define GPIOF_BASE ((unsigned int)0x40021400)
#define RCC_BASE ((unsigned int)0x40023800)
#define GPIOF_MODER *(unsigned int *)(GPIOF_BASE+0x00)//把这个数加(unsigned int *)强制转换成地址,之后加*转换为指针
#define GPIOF_OTYPER *(unsigned int *)(GPIOF_BASE+0x04)
#define GPIOF_ODR *(unsigned int *)(GPIOF_BASE+0x14)
#define RCC_AHB1ENR *(unsigned int *)(RCC_BASE+0x30)
//
//typedef unsigned int uint32_t;
//typedef unsigned short int uint16_t; //这两句在stdint.h里面有定义
//外设寄存器结构体定义
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 BSRRH;
uint32_t LCKR;
uint32_t AFRL;
uint32_t AFRH;
}GPIO_TypeDef;//结构体
#define GPIOF ((GPIO_TypeDef *)GPIOF_BASE)//结构体类型的指针,结构体指针会自动增加
#endif
//A.c
//B.c 目的是只编译一次,如果两个C文件都编译同一个头文件会报错
2:下面为头文件stm32f4xx_gpio.c的文件
#include "stm32f4xx_gpio.h"
void GPIO_SetBits(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)
{
GPIOx ->BSRRL=GPIO_Pin;//只有写1有效,写0无效;所以只要给他赋值就能达到效果
}
void GPIO_ResetBits(GPIO_TypeDef *GPIOx,uint16_t GPIO_Pin)
{
GPIOx ->BSRRH=GPIO_Pin;//只有写1有效,写0无效;所以只要给他赋值就能达到效果
}
3:接下来在stm32f4xx_gpio.h的头文件里面进行声明,并且对16个IO口进行了宏定义:
#ifndef _STM32F4XX_GPIO_H
#define _STM32F4XX_GPIO_H
#include "stm32f4xx.h"
#define GPIO_Pin_0 (uint16_t)(1<<0)
#define GPIO_Pin_1 (uint16_t)(1<<1)
#define GPIO_Pin_2 (uint16_t)(1<<2)
#define GPIO_Pin_3 (uint16_t)(1<<3)
#define GPIO_Pin_4 (uint16_t)(1<<4)
#define GPIO_Pin_5 (uint16_t)(1<<5)
#define GPIO_Pin_6 (uint16_t)(1<<6)
#define GPIO_Pin_7 (uint16_t)(1<<7)
#define GPIO_Pin_8 (uint16_t)(1<<8)
#define GPIO_Pin_9 (uint16_t)(1<<9)
#define GPIO_Pin_10 (uint16_t)(1<<10)
#define GPIO_Pin_11 (uint16_t)(1<<11)
#define GPIO_Pin_12 (uint16_t)(1<<12)
#define GPIO_Pin_13 (uint16_t)(1<<13)
#define GPIO_Pin_14 (uint16_t)(1<<14)
#define GPIO_Pin_15 (uint16_t)(1<<15)
#define GPIO_Pin_ALL (uint16_t)(0xffff)
void GPIO_SetBits(GPIO_TypeDef *GPIOx,uint16_t GPIO_Pin);
void GPIO_ResetBits(GPIO_TypeDef *GPIOx,uint16_t GPIO_Pin);
#endif
4:最后在main.c文件里对GPIO端口进行置位复位控制
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
void delay(unsigned int a)
{
for( ;a>0;a--);
}
int main (void)
{
#if 0
//第一步:开GPIOF端口时钟
RCC->AHB1ENR |=(1<<5);
//第二部:配置GPIO为输出
GPIOF->MODE &=~(3<<(2*9));
GPIOF->MODE |=(1<<(2*9));//如果在配置时之前就已经配置过11了,这时候在配置01,或的结果为11,
//所以说有两个为控制配置功能的我们首先将两个位配置清零;
//两个位为一组,只控制目标位
//第三步:让GPI0输出0或者1,ODR寄存器或者BSRR寄存器
GPIOF->ODR |=(1<<6);//先置1
GPIOF->ODR &=~(1<<6);
while(1)
{
GPIOF->ODR |=(1<<9);//先置1
delay(0xfffff);
GPIOF->ODR &=~(1<<9);
delay(0xfffff);
}
#elif 0 /*外设结构体定义*/
//第一步:开GPIOF端口时钟
RCC_AHB1ENR |=(1<<5);
//第二部:配置GPIO为输出
GPIOF->MODER &=~(3<<(2*9));
GPIOF->MODER |=(1<<(2*9));//如果在配置时之前就已经配置过11了,这时候在配置01,或的结果为11,
//所以说有两个为控制配置功能的我们首先将两个位配置清零;
//两个位为一组,只控制目标位
//
// //第三步:让GPI0输出0或者1,ODR寄存器或者BSRR寄存器
// GPIOF->ODR |=(1<<9);//先置1
GPIOF->ODR &=~(1<<9);
delay(0xfffff);
while(1)
{
delay(0xfffff);
GPIOF->ODR &=~(1<<9);
delay(0xfffff);
GPIOF->ODR |=(1<<9);//先置1
}
#elif 1 /*编写端口的复位和置位函数*/
//第一步:开GPIOF端口时钟
RCC_AHB1ENR |=(1<<5);
//第二部:配置GPIO为输出
GPIOF->MODER &=~(3<<(2*9));
GPIOF->MODER |=(1<<(2*9));//如果在配置时之前就已经配置过11了,这时候在配置01,或的结果为11,
//所以说有两个为控制配置功能的我们首先将两个位配置清零;
//两个位为一组,只控制目标位
//
// //第三步:让GPI0输出0或者1,ODR寄存器或者BSRR寄存器
// GPIOF->ODR |=(1<<9);//先置1
GPIOF->ODR &=~(1<<9);
delay(0xfffff);
while(1)
{
delay(0xfffff);
GPIO_ResetBits(GPIOF,GPIO_Pin_9);
delay(0xfffff);
GPIO_SetBits(GPIOF,GPIO_Pin_9);//这个地方位置位,为1
}
#endif
}
void SystemInit(void)
{
//骗过编译器不报错
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)