当前位置 : 主页 > 编程语言 > c语言 >

STM32F407代码记录

来源:互联网 收集:自由互联 发布时间:2023-09-07
魔术棒c/c++中Include paths中添加所有头文件路径;define中添加USE_STDPERIPH_DRIVER,STM32F40_41xxx, .c文件创建函数后,若不在.h中声明函数会造成报警:warning: fuction"xxxx"declared implicitly 避免重复声

魔术棒c/c++中Include paths中添加所有头文件路径;define中添加USE_STDPERIPH_DRIVER,STM32F40_41xxx,

.c文件创建函数后,若不在.h中声明函数会造成报警:warning: fuction"xxxx"declared implicitly

避免重复声明:#ifndef _XXX_XXX_H

#ifndef _XXX_XXX_H
#define _XXX_XXX_H


#endif /*_XXX_XXX_H*/

stm32f4xx.h

#define GPIOF_BASE    (( unsigned int )0x40021400)
#define RCC_BASE      (( unsigned int )0x40023800) 	
	
#define GPIOF_MODER   *(unsigned int*)(GPIOF_BASE+0X00)
#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;

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)

stm32f4xx_gpio.h

#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)(1<<0xffff))

typedef enum
{
	GPIO_Mode_IN 		= 0,			//输入
	GPIO_Mode_OUT 	= 0x01,	//输出
	GPIO_Mode_AF 		= 0x02,	//复用
	GPIO_Mode_AN 		= 0x03		//模拟模式
	
}GPIOMode_TypeDef;

typedef enum
{
	GPIO_PuPd_NOPULL 	= 0x00,			
	GPIO_PuPd_UP 			= 0x01,		
	GPIO_PuPd_DOWN 		= 0x02		
	
}GPIOPuPd_TypeDef;

typedef enum
{
	GPIO_OType_PP = 0x00,			
	GPIO_OType_OD	 = 0x00
	
}GPIOOType_TypeDef;

typedef enum
{
	GPIO_Speed_2MHZ			= 0x00,			
	GPIO_Speed_25MHZ		= 0x01,	
	GPIO_Speed_50MHZ 		= 0x02,	
	GPIO_Speed_100MHZ 	= 0x03,		
	
}GPIOSpeed_TypeDef;

/*定义GPIO的初始化结构体*/ 
typedef struct
{
	uint16_t					 GPIO_Pin;
	GPIOMode_TypeDef		GPIO_Mode;
	GPIOPuPd_TypeDef		GPIO_PuPd;
	GPIOOType_TypeDef		GPIO_OType;
	GPIOSpeed_TypeDef		GPIO_Speed;

}GPIO_InitTypeDef;


void GPIO_SetBits(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin);
void GPIO_ResetBits(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin);
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);

#endif /*_STM32F4XX_GPIO_H*/

stm32f4xx_gpio.c

#include "stm32f4xx_gpio.h"


void GPIO_SetBits(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)
{
	GPIOx->BSRRL =GPIO_Pin;
}

void GPIO_ResetBits(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)
{
	GPIOx->BSRRH = GPIO_Pin;
}

void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
{
	  uint32_t pinpos = 0x00, pos = 0x00 , currentpin = 0x00;
	/*-- GPIO Mode Configuration --*/
  for (pinpos = 0x00; pinpos < 16; pinpos++)
  {
		pos = ((uint32_t)0x01) << pinpos;  
	  currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
    if (currentpin == pos)
		{		
			GPIOx->MODER  &= ~(3 << (2 *pinpos));
			GPIOx->MODER |= (((uint32_t)GPIO_InitStruct->GPIO_Mode) << (2 *pinpos));
			GPIOx->PUPDR &= ~(3 << ((2 *pinpos)));
			GPIOx->PUPDR |= (((uint32_t)GPIO_InitStruct->GPIO_PuPd) << (2 *pinpos));			
			if ((GPIO_InitStruct->GPIO_Mode == GPIO_Mode_OUT) || (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_AF))
			{
				GPIOx->OSPEEDR &= ~(3 << (2 *pinpos));
				GPIOx->OSPEEDR |= ((uint32_t)(GPIO_InitStruct->GPIO_Speed) << (2 *pinpos));
				GPIOx->OTYPER  &= ~(1 << (pinpos)) ;
				GPIOx->OTYPER |= (uint16_t)(((uint16_t)GPIO_InitStruct->GPIO_OType) << (pinpos));
			}
		}
	}
}

main.c

#include "stm32f4xx_gpio.h"

/* 魔术棒中的Target界面->Code generation->选用5,使用6会烧不进去*/
/*注意事项:需要把魔术棒界面内Target->use MicroLIB勾选才能正常使用main函数*/

void delay(unsigned int count)
{
	for( ; count != 0 ; count--);
	
}

int main(void)
{
	//第一步:开GPIO端口时钟
	RCC_AHB1ENR |= (1<<5);
	
	//第二步:配置GPIO为输出
//	GPIOF->MODER &= ~(3<<(2*6));//使控制寄存器IO模式的两值重置为00
//	GPIOF->MODER |= (1<<(2*6));

//c89的语法,变量必须紧跟大括号
	
	GPIO_InitTypeDef			GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Pin		= GPIO_Pin_6;
	GPIO_InitStruct.GPIO_Mode		= GPIO_Mode_OUT;
	GPIO_InitStruct.GPIO_PuPd		= GPIO_PuPd_UP;
	GPIO_InitStruct.GPIO_OType	= GPIO_OType_PP;
	GPIO_InitStruct.GPIO_Speed	=	GPIO_Speed_2MHZ;
	GPIO_Init(GPIOF, &(GPIO_InitStruct));
		//第三步:让GPIO输出0或者1,ODR寄存器或者BSRR寄存器
	GPIOF->ODR |= (1<<6);
//	GPIOF_ODR &= ~(1<<6);
	while(1)
	{
		GPIOF->ODR &= ~(1<<6); 
		GPIO_ResetBits(GPIOF,GPIO_Pin_6);
		delay(0x0fffff);
		//GPIOF->ODR |= (1<<6);
		GPIO_SetBits(GPIOF,GPIO_Pin_6);
		delay(0x0fffff);
	}
	
}

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


上一篇:Vs-Code—控制台+乱码问题解决
下一篇:没有了
网友评论