STM32F429 >> 2. LED_Library_Function
阿新 • • 發佈:2018-12-18
本文程式碼已上傳到GitHub:https://github.com/Waao666/STM32-2.-LED_Library_Function
main.c
#include <stm32f4xx.h>
#include <stm32f4xx_gpio.h>
int main(void)
{
GPIO_InitTypeDef InitStruct;
RCC->AHB1ENR |= (1<<7);
InitStruct.GPIO_Pin = GPIO_Pin_9;
InitStruct.GPIO_Mode = GPIO_Mode_OUT;
InitStruct.GPIO_OType = GPIO_OType_PP;
InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
InitStruct.GPIO_Speed = GPIO_Speed_25MHz;
GPIO_Init(GPIOH, &InitStruct);
while(1);
}
void SystemInit(void)
{
}
stm32f4xx.h
#ifndef __STM32F4XX_H
#define __STM32F4XX_H
#define __IO volatile
typedef unsigned int uint32_t;
typedef unsigned short uint16_t;
typedef unsigned char uint8_t;
//----------------------基地址賦值------------------------
#define PERIPH_BASE ((unsigned int)0x40000000)
//匯流排暫存器
#define APB1PERIPH_BASE PERIPH_BASE
#define APB2PERIPH_BASE (PERIPH_BASE + 0x00010000)
#define AHB1PERIPH_BASE (PERIPH_BASE + 0x00020000)
#define AHB2PERIPH_BASE (PERIPH_BASE + 0x10000000)
#define AHB3PERIPH_BASE (PERIPH_BASE + 0X60000000)
//GPIO 暫存器
#define GPIOA_BASE AHB1PERIPH_BASE
#define GPIOB_BASE (AHB1PERIPH_BASE + 0x00000400)
#define GPIOC_BASE (AHB1PERIPH_BASE + 0x00000800)
#define GPIOD_BASE (AHB1PERIPH_BASE + 0x00000C00)
#define GPIOE_BASE (AHB1PERIPH_BASE + 0x00001000)
#define GPIOF_BASE (AHB1PERIPH_BASE + 0x00001400)
#define GPIOG_BASE (AHB1PERIPH_BASE + 0x00001800)
#define GPIOH_BASE (AHB1PERIPH_BASE + 0x00001C00)
#define GPIOI_BASE (AHB1PERIPH_BASE + 0x00002000)
//復位和時鐘控制
#define RCC_BASE (AHB1PERIPH_BASE + 0x00003800)
//外設時鐘使能暫存器
#define RCC_AHB1ENR *((unsigned int *)(RCC_BASE + 0X30))
typedef struct
{
__IO uint32_t MODER;
__IO uint32_t OTYPER;
__IO uint32_t OSPEEDR;
__IO uint32_t PUPDR;
__IO uint32_t IDR;
__IO uint32_t ODR;
__IO uint16_t BSRRL;
__IO uint16_t BSRRH;
__IO uint32_t LCKR;
__IO uint32_t AFR[2];
}GPIO_TypeDef;
typedef struct
{
__IO uint32_t CR;
__IO uint32_t PLLCFGR;
__IO uint32_t CFGR;
__IO uint32_t CIR;
__IO uint32_t AHB1RSTR;
__IO uint32_t AHB2RSTR;
__IO uint32_t AHB3RSTR;
__IO uint32_t RESERVED0; //此處為適用於其他晶片版本的APB1RSTR
__IO uint32_t APB1RSTR;
__IO uint32_t RESERVED1[2]; //此處為適用於其他晶片版本的APB2RSTR
__IO uint32_t APB2RSTR;
__IO uint32_t AHB1ENR;
__IO uint32_t AHB2ENR;
__IO uint32_t AHB3ENR;
//後面還有很多,省略...
}RCC_TypeDef;
//------------------定義訪問外設的結構體指標---------------------
#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)
#define RCC ((RCC_TypeDef *) RCC_BASE)
#endif /* __STM32F4XX_H */
stm32f4xx_gpio.c
#include <stm32f4xx_gpio.h>
#include <stm32f4xx.h>
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) //Set bits
{
GPIOx->BSRRL = GPIO_Pin;
}
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) //Reset bits
{
GPIOx->BSRRH = GPIO_Pin;
}
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct) //Find the pin, and initialize the GPIO
{
uint32_t pinpos = 0x00, pin = 0x00, currency = 0x00;
currency = GPIO_InitStruct->GPIO_Pin;
for(pinpos=0x00; pinpos<16; pinpos++)
{
pin = 0x01<<pinpos;
if(pin == (pin & currency))
{
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)((GPIO_InitStruct->GPIO_OType)<<(pinpos));
}
}
}
}
stm32f4xx_gpio.h
#include <stm32f4xx.h>
#define GPIO_Pin_0 ((uint16_t)0x0001)
#define GPIO_Pin_1 ((uint16_t)0x0002)
#define GPIO_Pin_2 ((uint16_t)0x0004)
#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)
//Initialize GPIO
/*
typedef struct
{
uint32_t GPIO_Pin;
uint8_t GPIO_Mode;
uint8_t GPIO_Speed;
uint8_t GPIO_OType;
uint8_t GPIO_PuPd;
}GPIO_InitTypeDef;
*/
//-----------------------------------------------------------
//GPIO
typedef enum
{
GPIO_Mode_IN = 0X00,
GPIO_Mode_OUT = 0X01,
GPIO_Mode_AF = 0X02,
GPIO_Mode_AN = 0X03
}GPIOMode_TypeDef;
typedef enum
{
GPIO_OType_PP = 0x00,
GPIO_OType_OD = 0x01
}GPIOOType_TypeDef;
typedef enum
{
GPIO_Speed_2MHz = 0x00, //2MHz
GPIO_Speed_25MHz = 0x01, //25MHz
GPIO_Speed_50MHz = 0x10, //50MHz
GPIO_Speed_100MHz = 0x11 //100MHz
}GPIOSpeed_TypeDef;
typedef enum
{
GPIO_PuPd_NOPULL = 0X00,
GPIO_PuPd_UP = 0X01,
GPIO_PuPd_DOWN = 0X02
}GPIOPuPd_TypeDef;
//-----------------------------------------------------------
typedef struct
{
uint32_t GPIO_Pin;
GPIOMode_TypeDef GPIO_Mode;
GPIOOType_TypeDef GPIO_OType;
GPIOSpeed_TypeDef GPIO_Speed;
GPIOPuPd_TypeDef GPIO_PuPd;
}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);