拷贝.docx
《拷贝.docx》由会员分享,可在线阅读,更多相关《拷贝.docx(14页珍藏版)》请在冰点文库上搜索。
拷贝
/******************GPIO.c********************************
********************************************************************/
#include"stm32f4xx.h"
#include"Gpio.h"
Gpio_InfoGpio_info[LED_NUMBER]={
{LED1_RCC,LED1_GPIO,LED1_GPIO_PIN},
{LED2_RCC,LED2_GPIO,LED2_GPIO_PIN},
{LED3_RCC,LED3_GPIO,LED3_GPIO_PIN},
{LED4_RCC,LED4_GPIO,LED4_GPIO_PIN}
};
Gpio_InfoKey_info[KEY_NUMBER]={
{KEY_S1_RCC,KEY_S1_GPIO,KEY_S1_GPIO_PIN},
{KEY_S2_RCC,KEY_S2_GPIO,KEY_S2_GPIO_PIN},
{KEY_S3_RCC,KEY_S3_GPIO,KEY_S3_GPIO_PIN},
{KEY_S4_RCC,KEY_S4_GPIO,KEY_S4_GPIO_PIN},
{KEY_S5_RCC,KEY_S5_GPIO,KEY_S5_GPIO_PIN}
/***********************************************************/
voidLEDGpio_Init(void)
{
uint8_ti;
GPIO_InitTypeDefGPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
for(i=0;i{RCC_AHB1PeriphClockCmd(Gpio_info[i].rcc,ENABLE);GPIO_InitStructure.GPIO_Pin=Gpio_info[i].pin;GPIO_Init(Gpio_info[i].gpio,&GPIO_InitStructure);GPIO_WriteBit(Gpio_info[i].gpio,Gpio_info[i].pin,Bit_SET);}}/******************************************************************************************************************************************************/voidKEYGpio_Init(void){uint8_ti;GPIO_InitTypeDefGPIO_InitStructure;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN;GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;for(i=0;i{RCC_AHB1PeriphClockCmd(Key_info[i].rcc,ENABLE);GPIO_InitStructure.GPIO_Pin=Key_info[i].pin;GPIO_Init(Key_info[i].gpio,&GPIO_InitStructure);}}/******************GPIO.h****************************************************************************************************/#ifndef_GPIO_H_#define_GIPO_H_#defineLED_NUMBER4//LED×ÜÊý#defineKEY_NUMBER5//°´¼ü×ÜÊý/************LEDµÆ½Ó¿Ú******************/#defineLED1_RCCRCC_AHB1Periph_GPIOA#defineLED1_GPIOGPIOA#defineLED1_GPIO_PINGPIO_Pin_6#defineLED1_ONOFF(x)GPIO_WriteBit(GPIOA,GPIO_Pin_6,x)#defineLED2_RCCRCC_AHB1Periph_GPIOA#defineLED2_GPIOGPIOA#defineLED2_GPIO_PINGPIO_Pin_4#defineLED2_ONOFF(x)GPIO_WriteBit(GPIOA,GPIO_Pin_4,x)#defineLED3_RCCRCC_AHB1Periph_GPIOB#defineLED3_GPIOGPIOB#defineLED3_GPIO_PINGPIO_Pin_6#defineLED3_ONOFF(x)GPIO_WriteBit(GPIOB,GPIO_Pin_6,x)#defineLED4_RCCRCC_AHB1Periph_GPIOC#defineLED4_GPIOGPIOC#defineLED4_GPIO_PINGPIO_Pin_6#defineLED4_ONOFF(x)GPIO_WriteBit(GPIOC,GPIO_Pin_6,x)/************°´¼ü½Ó¿Ú******************/#defineKEY_S1_RCCRCC_AHB1Periph_GPIOB#defineKEY_S1_GPIOGPIOB#defineKEY_S1_GPIO_PINGPIO_Pin_2#defineKEY_S1_UPDOWM()GPIO_ReadInputDataBit(KEY_S1_GPIO,KEY_S1_GPIO_PIN)#defineKEY_S2_RCCRCC_AHB1Periph_GPIOA#defineKEY_S2_GPIOGPIOA#defineKEY_S2_GPIO_PINGPIO_Pin_0#defineKEY_S2_UPDOWM()GPIO_ReadInputDataBit(KEY_S2_GPIO,KEY_S2_GPIO_PIN)#defineKEY_S3_RCCRCC_AHB1Periph_GPIOD#defineKEY_S3_GPIOGPIOD#defineKEY_S3_GPIO_PINGPIO_Pin_3#defineKEY_S3_UPDOWM()GPIO_ReadInputDataBit(KEY_S3_GPIO,KEY_S3_GPIO_PIN)#defineKEY_S4_RCCRCC_AHB1Periph_GPIOF#defineKEY_S4_GPIOGPIOF#defineKEY_S4_GPIO_PINGPIO_Pin_7#defineKEY_S4_UPDOWM()GPIO_ReadInputDataBit(KEY_S4_GPIO,KEY_S4_GPIO_PIN)#defineKEY_S5_RCCRCC_AHB1Periph_GPIOF#defineKEY_S5_GPIOGPIOF#defineKEY_S5_GPIO_PINGPIO_Pin_6#defineKEY_S5_UPDOWM()GPIO_ReadInputDataBit(KEY_S5_GPIO,KEY_S5_GPIO_PIN)typedefstruct{uint32_trcc;GPIO_TypeDef*gpio;uint16_tpin;}Gpio_Info;voidLEDGpio_Init(void);voidKEYGpio_Init(void);#endif=================================================================Stm32f4xx_gpio.c==================================================================#include"stm32f4xx_gpio.h"#include"stm32f4xx_rcc.h"============================================InitializationandConfiguration=============================================voidGPIO_DeInit(GPIO_TypeDef*GPIOx){/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));if(GPIOx==GPIOA){RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOA,ENABLE);RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOA,DISABLE);}elseif(GPIOx==GPIOB){RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOB,ENABLE);RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOB,DISABLE);}elseif(GPIOx==GPIOC){RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOC,ENABLE);RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOC,DISABLE);}elseif(GPIOx==GPIOD){RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOD,ENABLE);RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOD,DISABLE);}elseif(GPIOx==GPIOE){RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOE,ENABLE);RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOE,DISABLE);}elseif(GPIOx==GPIOF){RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOF,ENABLE);RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOF,DISABLE);}elseif(GPIOx==GPIOG){RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOG,ENABLE);RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOG,DISABLE);}elseif(GPIOx==GPIOH){RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOH,ENABLE);RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOH,DISABLE);}else{if(GPIOx==GPIOI){RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOI,ENABLE);RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOI,DISABLE);}}}voidGPIO_Init(GPIO_TypeDef*GPIOx,GPIO_InitTypeDef*GPIO_InitStruct){uint32_tpinpos=0x00,pos=0x00,currentpin=0x00;/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));assert_param(IS_GPIO_PUPD(GPIO_InitStruct->GPIO_PuPd));/*-------------------------Configuretheportpins----------------*//*--GPIOModeConfiguration--*/for(pinpos=0x00;pinpos<0x10;pinpos++){pos=((uint32_t)0x01)</*Gettheportpinsposition*/currentpin=(GPIO_InitStruct->GPIO_Pin)&pos;if(currentpin==pos){GPIOx->MODER&=~(GPIO_MODER_MODER0<<(pinpos*2));GPIOx->MODER|=(((uint32_t)GPIO_InitStruct->GPIO_Mode)<<(pinpos*2));if((GPIO_InitStruct->GPIO_Mode==GPIO_Mode_OUT)||(GPIO_InitStruct->GPIO_Mode==GPIO_Mode_AF)){/*CheckSpeedmodeparameters*/assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));/*Speedmodeconfiguration*/GPIOx->OSPEEDR&=~(GPIO_OSPEEDER_OSPEEDR0<<(pinpos*2));GPIOx->OSPEEDR|=((uint32_t)(GPIO_InitStruct->GPIO_Speed)<<(pinpos*2));/*CheckOutputmodeparameters*/assert_param(IS_GPIO_OTYPE(GPIO_InitStruct->GPIO_OType));/*Outputmodeconfiguration*/GPIOx->OTYPER&=~((GPIO_OTYPER_OT_0)<<((uint16_t)pinpos));GPIOx->OTYPER|=(uint16_t)(((uint16_t)GPIO_InitStruct->GPIO_OType)<<((uint16_t)pinpos));}/*Pull-upPulldownresistorconfiguration*/GPIOx->PUPDR&=~(GPIO_PUPDR_PUPDR0<<((uint16_t)pinpos*2));GPIOx->PUPDR|=(((uint32_t)GPIO_InitStruct->GPIO_PuPd)<<(pinpos*2));}}}voidGPIO_StructInit(GPIO_InitTypeDef*GPIO_InitStruct){/*ResetGPIOinitstructureparametersvalues*/GPIO_InitStruct->GPIO_Pin=GPIO_Pin_All;GPIO_InitStruct->GPIO_Mode=GPIO_Mode_IN;GPIO_InitStruct->GPIO_Speed=GPIO_Speed_2MHz;GPIO_InitStruct->GPIO_OType=GPIO_OType_PP;GPIO_InitStruct->GPIO_PuPd=GPIO_PuPd_NOPULL;}voidGPIO_PinLockConfig(GPIO_TypeDef*GPIOx,uint16_tGPIO_Pin){__IOuint32_ttmp=0x00010000;/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));assert_param(IS_GPIO_PIN(GPIO_Pin));tmp|=GPIO_Pin;/*SetLCKKbit*/GPIOx->LCKR=tmp;/*ResetLCKKbit*/GPIOx->LCKR=GPIO_Pin;/*SetLCKKbit*/GPIOx->LCKR=tmp;/*ReadLCKKbit*/tmp=GPIOx->LCKR;/*ReadLCKKbit*/tmp=GPIOx->LCKR;}=============================================GPIOReadandWrite==========================uint8_tGPIO_ReadInputDataBit(GPIO_TypeDef*GPIOx,uint16_tGPIO_Pin){uint8_tbitstatus=0x00;/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));assert_param(IS_GET_GPIO_PIN(GPIO_Pin));if((GPIOx->IDR&GPIO_Pin)!=(uint32_t)Bit_RESET){bitstatus=(uint8_t)Bit_SET;}else{bitstatus=(uint8_t)Bit_RESET;}returnbitstatus;}uint16_tGPIO_ReadInputData(GPIO_TypeDef*GPIOx){/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));return((uint16_t)GPIOx->IDR);}uint8_tGPIO_ReadOutputDataBit(GPIO_TypeDef*GPIOx,uint16_tGPIO_Pin){uint8_tbitstatus=0x00;/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));assert_param(IS_GET_GPIO_PIN(GPIO_Pin));if((GPIOx->ODR&GPIO_Pin)!=(uint32_t)Bit_RESET){bitstatus=(uint8_t)Bit_SET;}else{bitstatus=(uint8_t)Bit_RESET;}returnbitstatus;}uint16_tGPIO_ReadOutputData(GPIO_TypeDef*GPIOx){/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));return((uint16_t)GPIOx->ODR);}voidGPIO_SetBits(GPIO_TypeDef*GPIOx,uint16_tGPIO_Pin){/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));assert_param(IS_GPIO_PIN(GPIO_Pin));GPIOx->BSRRL=GPIO_Pin;}voidGPIO_ResetBits(GPIO_TypeDef*GPIOx,uint16_tGPIO_Pin){/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));assert_param(IS_GPIO_PIN(GPIO_Pin));GPIOx->BSRRH=GPIO_Pin;}voidGPIO_WriteBit(GPIO_TypeDef*GPIOx,uint16_tGPIO_Pin,BitActionBitVal){/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));assert_param(IS_GET_GPIO_PIN(GPIO_Pin));assert_param(IS_GPIO_BIT_ACTION(BitVal));if(BitVal!=Bit_RESET){GPIOx->BSRRL=GPIO_Pin;}else{GPIOx->BSRRH=GPIO_Pin;}}voidGPIO_Write(GPIO_TypeDef*GP
RCC_AHB1PeriphClockCmd(Gpio_info[i].rcc,ENABLE);
GPIO_InitStructure.GPIO_Pin=Gpio_info[i].pin;
GPIO_Init(Gpio_info[i].gpio,&GPIO_InitStructure);
GPIO_WriteBit(Gpio_info[i].gpio,Gpio_info[i].pin,Bit_SET);
}
/******************************************************************************************************************************************************/
voidKEYGpio_Init(void)
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
for(i=0;i{RCC_AHB1PeriphClockCmd(Key_info[i].rcc,ENABLE);GPIO_InitStructure.GPIO_Pin=Key_info[i].pin;GPIO_Init(Key_info[i].gpio,&GPIO_InitStructure);}}/******************GPIO.h****************************************************************************************************/#ifndef_GPIO_H_#define_GIPO_H_#defineLED_NUMBER4//LED×ÜÊý#defineKEY_NUMBER5//°´¼ü×ÜÊý/************LEDµÆ½Ó¿Ú******************/#defineLED1_RCCRCC_AHB1Periph_GPIOA#defineLED1_GPIOGPIOA#defineLED1_GPIO_PINGPIO_Pin_6#defineLED1_ONOFF(x)GPIO_WriteBit(GPIOA,GPIO_Pin_6,x)#defineLED2_RCCRCC_AHB1Periph_GPIOA#defineLED2_GPIOGPIOA#defineLED2_GPIO_PINGPIO_Pin_4#defineLED2_ONOFF(x)GPIO_WriteBit(GPIOA,GPIO_Pin_4,x)#defineLED3_RCCRCC_AHB1Periph_GPIOB#defineLED3_GPIOGPIOB#defineLED3_GPIO_PINGPIO_Pin_6#defineLED3_ONOFF(x)GPIO_WriteBit(GPIOB,GPIO_Pin_6,x)#defineLED4_RCCRCC_AHB1Periph_GPIOC#defineLED4_GPIOGPIOC#defineLED4_GPIO_PINGPIO_Pin_6#defineLED4_ONOFF(x)GPIO_WriteBit(GPIOC,GPIO_Pin_6,x)/************°´¼ü½Ó¿Ú******************/#defineKEY_S1_RCCRCC_AHB1Periph_GPIOB#defineKEY_S1_GPIOGPIOB#defineKEY_S1_GPIO_PINGPIO_Pin_2#defineKEY_S1_UPDOWM()GPIO_ReadInputDataBit(KEY_S1_GPIO,KEY_S1_GPIO_PIN)#defineKEY_S2_RCCRCC_AHB1Periph_GPIOA#defineKEY_S2_GPIOGPIOA#defineKEY_S2_GPIO_PINGPIO_Pin_0#defineKEY_S2_UPDOWM()GPIO_ReadInputDataBit(KEY_S2_GPIO,KEY_S2_GPIO_PIN)#defineKEY_S3_RCCRCC_AHB1Periph_GPIOD#defineKEY_S3_GPIOGPIOD#defineKEY_S3_GPIO_PINGPIO_Pin_3#defineKEY_S3_UPDOWM()GPIO_ReadInputDataBit(KEY_S3_GPIO,KEY_S3_GPIO_PIN)#defineKEY_S4_RCCRCC_AHB1Periph_GPIOF#defineKEY_S4_GPIOGPIOF#defineKEY_S4_GPIO_PINGPIO_Pin_7#defineKEY_S4_UPDOWM()GPIO_ReadInputDataBit(KEY_S4_GPIO,KEY_S4_GPIO_PIN)#defineKEY_S5_RCCRCC_AHB1Periph_GPIOF#defineKEY_S5_GPIOGPIOF#defineKEY_S5_GPIO_PINGPIO_Pin_6#defineKEY_S5_UPDOWM()GPIO_ReadInputDataBit(KEY_S5_GPIO,KEY_S5_GPIO_PIN)typedefstruct{uint32_trcc;GPIO_TypeDef*gpio;uint16_tpin;}Gpio_Info;voidLEDGpio_Init(void);voidKEYGpio_Init(void);#endif=================================================================Stm32f4xx_gpio.c==================================================================#include"stm32f4xx_gpio.h"#include"stm32f4xx_rcc.h"============================================InitializationandConfiguration=============================================voidGPIO_DeInit(GPIO_TypeDef*GPIOx){/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));if(GPIOx==GPIOA){RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOA,ENABLE);RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOA,DISABLE);}elseif(GPIOx==GPIOB){RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOB,ENABLE);RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOB,DISABLE);}elseif(GPIOx==GPIOC){RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOC,ENABLE);RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOC,DISABLE);}elseif(GPIOx==GPIOD){RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOD,ENABLE);RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOD,DISABLE);}elseif(GPIOx==GPIOE){RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOE,ENABLE);RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOE,DISABLE);}elseif(GPIOx==GPIOF){RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOF,ENABLE);RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOF,DISABLE);}elseif(GPIOx==GPIOG){RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOG,ENABLE);RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOG,DISABLE);}elseif(GPIOx==GPIOH){RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOH,ENABLE);RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOH,DISABLE);}else{if(GPIOx==GPIOI){RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOI,ENABLE);RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOI,DISABLE);}}}voidGPIO_Init(GPIO_TypeDef*GPIOx,GPIO_InitTypeDef*GPIO_InitStruct){uint32_tpinpos=0x00,pos=0x00,currentpin=0x00;/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));assert_param(IS_GPIO_PUPD(GPIO_InitStruct->GPIO_PuPd));/*-------------------------Configuretheportpins----------------*//*--GPIOModeConfiguration--*/for(pinpos=0x00;pinpos<0x10;pinpos++){pos=((uint32_t)0x01)</*Gettheportpinsposition*/currentpin=(GPIO_InitStruct->GPIO_Pin)&pos;if(currentpin==pos){GPIOx->MODER&=~(GPIO_MODER_MODER0<<(pinpos*2));GPIOx->MODER|=(((uint32_t)GPIO_InitStruct->GPIO_Mode)<<(pinpos*2));if((GPIO_InitStruct->GPIO_Mode==GPIO_Mode_OUT)||(GPIO_InitStruct->GPIO_Mode==GPIO_Mode_AF)){/*CheckSpeedmodeparameters*/assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));/*Speedmodeconfiguration*/GPIOx->OSPEEDR&=~(GPIO_OSPEEDER_OSPEEDR0<<(pinpos*2));GPIOx->OSPEEDR|=((uint32_t)(GPIO_InitStruct->GPIO_Speed)<<(pinpos*2));/*CheckOutputmodeparameters*/assert_param(IS_GPIO_OTYPE(GPIO_InitStruct->GPIO_OType));/*Outputmodeconfiguration*/GPIOx->OTYPER&=~((GPIO_OTYPER_OT_0)<<((uint16_t)pinpos));GPIOx->OTYPER|=(uint16_t)(((uint16_t)GPIO_InitStruct->GPIO_OType)<<((uint16_t)pinpos));}/*Pull-upPulldownresistorconfiguration*/GPIOx->PUPDR&=~(GPIO_PUPDR_PUPDR0<<((uint16_t)pinpos*2));GPIOx->PUPDR|=(((uint32_t)GPIO_InitStruct->GPIO_PuPd)<<(pinpos*2));}}}voidGPIO_StructInit(GPIO_InitTypeDef*GPIO_InitStruct){/*ResetGPIOinitstructureparametersvalues*/GPIO_InitStruct->GPIO_Pin=GPIO_Pin_All;GPIO_InitStruct->GPIO_Mode=GPIO_Mode_IN;GPIO_InitStruct->GPIO_Speed=GPIO_Speed_2MHz;GPIO_InitStruct->GPIO_OType=GPIO_OType_PP;GPIO_InitStruct->GPIO_PuPd=GPIO_PuPd_NOPULL;}voidGPIO_PinLockConfig(GPIO_TypeDef*GPIOx,uint16_tGPIO_Pin){__IOuint32_ttmp=0x00010000;/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));assert_param(IS_GPIO_PIN(GPIO_Pin));tmp|=GPIO_Pin;/*SetLCKKbit*/GPIOx->LCKR=tmp;/*ResetLCKKbit*/GPIOx->LCKR=GPIO_Pin;/*SetLCKKbit*/GPIOx->LCKR=tmp;/*ReadLCKKbit*/tmp=GPIOx->LCKR;/*ReadLCKKbit*/tmp=GPIOx->LCKR;}=============================================GPIOReadandWrite==========================uint8_tGPIO_ReadInputDataBit(GPIO_TypeDef*GPIOx,uint16_tGPIO_Pin){uint8_tbitstatus=0x00;/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));assert_param(IS_GET_GPIO_PIN(GPIO_Pin));if((GPIOx->IDR&GPIO_Pin)!=(uint32_t)Bit_RESET){bitstatus=(uint8_t)Bit_SET;}else{bitstatus=(uint8_t)Bit_RESET;}returnbitstatus;}uint16_tGPIO_ReadInputData(GPIO_TypeDef*GPIOx){/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));return((uint16_t)GPIOx->IDR);}uint8_tGPIO_ReadOutputDataBit(GPIO_TypeDef*GPIOx,uint16_tGPIO_Pin){uint8_tbitstatus=0x00;/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));assert_param(IS_GET_GPIO_PIN(GPIO_Pin));if((GPIOx->ODR&GPIO_Pin)!=(uint32_t)Bit_RESET){bitstatus=(uint8_t)Bit_SET;}else{bitstatus=(uint8_t)Bit_RESET;}returnbitstatus;}uint16_tGPIO_ReadOutputData(GPIO_TypeDef*GPIOx){/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));return((uint16_t)GPIOx->ODR);}voidGPIO_SetBits(GPIO_TypeDef*GPIOx,uint16_tGPIO_Pin){/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));assert_param(IS_GPIO_PIN(GPIO_Pin));GPIOx->BSRRL=GPIO_Pin;}voidGPIO_ResetBits(GPIO_TypeDef*GPIOx,uint16_tGPIO_Pin){/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));assert_param(IS_GPIO_PIN(GPIO_Pin));GPIOx->BSRRH=GPIO_Pin;}voidGPIO_WriteBit(GPIO_TypeDef*GPIOx,uint16_tGPIO_Pin,BitActionBitVal){/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));assert_param(IS_GET_GPIO_PIN(GPIO_Pin));assert_param(IS_GPIO_BIT_ACTION(BitVal));if(BitVal!=Bit_RESET){GPIOx->BSRRL=GPIO_Pin;}else{GPIOx->BSRRH=GPIO_Pin;}}voidGPIO_Write(GPIO_TypeDef*GP
RCC_AHB1PeriphClockCmd(Key_info[i].rcc,ENABLE);
GPIO_InitStructure.GPIO_Pin=Key_info[i].pin;
GPIO_Init(Key_info[i].gpio,&GPIO_InitStructure);
/******************GPIO.h********************************
#ifndef_GPIO_H_
#define_GIPO_H_
#defineLED_NUMBER4//LED×ÜÊý
#defineKEY_NUMBER5//°´¼ü×ÜÊý
/************LEDµÆ½Ó¿Ú******************/
#defineLED1_RCCRCC_AHB1Periph_GPIOA
#defineLED1_GPIOGPIOA
#defineLED1_GPIO_PINGPIO_Pin_6
#defineLED1_ONOFF(x)GPIO_WriteBit(GPIOA,GPIO_Pin_6,x)
#defineLED2_RCCRCC_AHB1Periph_GPIOA
#defineLED2_GPIOGPIOA
#defineLED2_GPIO_PINGPIO_Pin_4
#defineLED2_ONOFF(x)GPIO_WriteBit(GPIOA,GPIO_Pin_4,x)
#defineLED3_RCCRCC_AHB1Periph_GPIOB
#defineLED3_GPIOGPIOB
#defineLED3_GPIO_PINGPIO_Pin_6
#defineLED3_ONOFF(x)GPIO_WriteBit(GPIOB,GPIO_Pin_6,x)
#defineLED4_RCCRCC_AHB1Periph_GPIOC
#defineLED4_GPIOGPIOC
#defineLED4_GPIO_PINGPIO_Pin_6
#defineLED4_ONOFF(x)GPIO_WriteBit(GPIOC,GPIO_Pin_6,x)
/************°´¼ü½Ó¿Ú******************/
#defineKEY_S1_RCCRCC_AHB1Periph_GPIOB
#defineKEY_S1_GPIOGPIOB
#defineKEY_S1_GPIO_PINGPIO_Pin_2
#defineKEY_S1_UPDOWM()GPIO_ReadInputDataBit(KEY_S1_GPIO,KEY_S1_GPIO_PIN)
#defineKEY_S2_RCCRCC_AHB1Periph_GPIOA
#defineKEY_S2_GPIOGPIOA
#defineKEY_S2_GPIO_PINGPIO_Pin_0
#defineKEY_S2_UPDOWM()GPIO_ReadInputDataBit(KEY_S2_GPIO,KEY_S2_GPIO_PIN)
#defineKEY_S3_RCCRCC_AHB1Periph_GPIOD
#defineKEY_S3_GPIOGPIOD
#defineKEY_S3_GPIO_PINGPIO_Pin_3
#defineKEY_S3_UPDOWM()GPIO_ReadInputDataBit(KEY_S3_GPIO,KEY_S3_GPIO_PIN)
#defineKEY_S4_RCCRCC_AHB1Periph_GPIOF
#defineKEY_S4_GPIOGPIOF
#defineKEY_S4_GPIO_PINGPIO_Pin_7
#defineKEY_S4_UPDOWM()GPIO_ReadInputDataBit(KEY_S4_GPIO,KEY_S4_GPIO_PIN)
#defineKEY_S5_RCCRCC_AHB1Periph_GPIOF
#defineKEY_S5_GPIOGPIOF
#defineKEY_S5_GPIO_PINGPIO_Pin_6
#defineKEY_S5_UPDOWM()GPIO_ReadInputDataBit(KEY_S5_GPIO,KEY_S5_GPIO_PIN)
typedefstruct{
uint32_trcc;
GPIO_TypeDef*gpio;
uint16_tpin;
}Gpio_Info;
voidLEDGpio_Init(void);
voidKEYGpio_Init(void);
#endif
=================================================================
Stm32f4xx_gpio.c
==================================================================
#include"stm32f4xx_gpio.h"
#include"stm32f4xx_rcc.h"
============================================
InitializationandConfiguration
=============================================
voidGPIO_DeInit(GPIO_TypeDef*GPIOx)
/*Checktheparameters*/
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
if(GPIOx==GPIOA)
RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOA,DISABLE);
elseif(GPIOx==GPIOB)
RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOB,ENABLE);
RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOB,DISABLE);
elseif(GPIOx==GPIOC)
RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOC,ENABLE);
RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOC,DISABLE);
elseif(GPIOx==GPIOD)
RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOD,ENABLE);
RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOD,DISABLE);
elseif(GPIOx==GPIOE)
RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOE,ENABLE);
RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOE,DISABLE);
elseif(GPIOx==GPIOF)
RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOF,ENABLE);
RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOF,DISABLE);
elseif(GPIOx==GPIOG)
RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOG,ENABLE);
RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOG,DISABLE);
elseif(GPIOx==GPIOH)
RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOH,ENABLE);
RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOH,DISABLE);
else
if(GPIOx==GPIOI)
RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOI,ENABLE);
RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOI,DISABLE);
voidGPIO_Init(GPIO_TypeDef*GPIOx,GPIO_InitTypeDef*GPIO_InitStruct)
uint32_tpinpos=0x00,pos=0x00,currentpin=0x00;
assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
assert_param(IS_GPIO_PUPD(GPIO_InitStruct->GPIO_PuPd));
/*-------------------------Configuretheportpins----------------*/
/*--GPIOModeConfiguration--*/
for(pinpos=0x00;pinpos<0x10;pinpos++)
pos=((uint32_t)0x01)</*Gettheportpinsposition*/currentpin=(GPIO_InitStruct->GPIO_Pin)&pos;if(currentpin==pos){GPIOx->MODER&=~(GPIO_MODER_MODER0<<(pinpos*2));GPIOx->MODER|=(((uint32_t)GPIO_InitStruct->GPIO_Mode)<<(pinpos*2));if((GPIO_InitStruct->GPIO_Mode==GPIO_Mode_OUT)||(GPIO_InitStruct->GPIO_Mode==GPIO_Mode_AF)){/*CheckSpeedmodeparameters*/assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));/*Speedmodeconfiguration*/GPIOx->OSPEEDR&=~(GPIO_OSPEEDER_OSPEEDR0<<(pinpos*2));GPIOx->OSPEEDR|=((uint32_t)(GPIO_InitStruct->GPIO_Speed)<<(pinpos*2));/*CheckOutputmodeparameters*/assert_param(IS_GPIO_OTYPE(GPIO_InitStruct->GPIO_OType));/*Outputmodeconfiguration*/GPIOx->OTYPER&=~((GPIO_OTYPER_OT_0)<<((uint16_t)pinpos));GPIOx->OTYPER|=(uint16_t)(((uint16_t)GPIO_InitStruct->GPIO_OType)<<((uint16_t)pinpos));}/*Pull-upPulldownresistorconfiguration*/GPIOx->PUPDR&=~(GPIO_PUPDR_PUPDR0<<((uint16_t)pinpos*2));GPIOx->PUPDR|=(((uint32_t)GPIO_InitStruct->GPIO_PuPd)<<(pinpos*2));}}}voidGPIO_StructInit(GPIO_InitTypeDef*GPIO_InitStruct){/*ResetGPIOinitstructureparametersvalues*/GPIO_InitStruct->GPIO_Pin=GPIO_Pin_All;GPIO_InitStruct->GPIO_Mode=GPIO_Mode_IN;GPIO_InitStruct->GPIO_Speed=GPIO_Speed_2MHz;GPIO_InitStruct->GPIO_OType=GPIO_OType_PP;GPIO_InitStruct->GPIO_PuPd=GPIO_PuPd_NOPULL;}voidGPIO_PinLockConfig(GPIO_TypeDef*GPIOx,uint16_tGPIO_Pin){__IOuint32_ttmp=0x00010000;/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));assert_param(IS_GPIO_PIN(GPIO_Pin));tmp|=GPIO_Pin;/*SetLCKKbit*/GPIOx->LCKR=tmp;/*ResetLCKKbit*/GPIOx->LCKR=GPIO_Pin;/*SetLCKKbit*/GPIOx->LCKR=tmp;/*ReadLCKKbit*/tmp=GPIOx->LCKR;/*ReadLCKKbit*/tmp=GPIOx->LCKR;}=============================================GPIOReadandWrite==========================uint8_tGPIO_ReadInputDataBit(GPIO_TypeDef*GPIOx,uint16_tGPIO_Pin){uint8_tbitstatus=0x00;/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));assert_param(IS_GET_GPIO_PIN(GPIO_Pin));if((GPIOx->IDR&GPIO_Pin)!=(uint32_t)Bit_RESET){bitstatus=(uint8_t)Bit_SET;}else{bitstatus=(uint8_t)Bit_RESET;}returnbitstatus;}uint16_tGPIO_ReadInputData(GPIO_TypeDef*GPIOx){/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));return((uint16_t)GPIOx->IDR);}uint8_tGPIO_ReadOutputDataBit(GPIO_TypeDef*GPIOx,uint16_tGPIO_Pin){uint8_tbitstatus=0x00;/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));assert_param(IS_GET_GPIO_PIN(GPIO_Pin));if((GPIOx->ODR&GPIO_Pin)!=(uint32_t)Bit_RESET){bitstatus=(uint8_t)Bit_SET;}else{bitstatus=(uint8_t)Bit_RESET;}returnbitstatus;}uint16_tGPIO_ReadOutputData(GPIO_TypeDef*GPIOx){/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));return((uint16_t)GPIOx->ODR);}voidGPIO_SetBits(GPIO_TypeDef*GPIOx,uint16_tGPIO_Pin){/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));assert_param(IS_GPIO_PIN(GPIO_Pin));GPIOx->BSRRL=GPIO_Pin;}voidGPIO_ResetBits(GPIO_TypeDef*GPIOx,uint16_tGPIO_Pin){/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));assert_param(IS_GPIO_PIN(GPIO_Pin));GPIOx->BSRRH=GPIO_Pin;}voidGPIO_WriteBit(GPIO_TypeDef*GPIOx,uint16_tGPIO_Pin,BitActionBitVal){/*Checktheparameters*/assert_param(IS_GPIO_ALL_PERIPH(GPIOx));assert_param(IS_GET_GPIO_PIN(GPIO_Pin));assert_param(IS_GPIO_BIT_ACTION(BitVal));if(BitVal!=Bit_RESET){GPIOx->BSRRL=GPIO_Pin;}else{GPIOx->BSRRH=GPIO_Pin;}}voidGPIO_Write(GPIO_TypeDef*GP
/*Gettheportpinsposition*/
currentpin=(GPIO_InitStruct->GPIO_Pin)&pos;
if(currentpin==pos)
GPIOx->MODER&=~(GPIO_MODER_MODER0<<(pinpos*2));
GPIOx->MODER|=(((uint32_t)GPIO_InitStruct->GPIO_Mode)<<(pinpos*2));
if((GPIO_InitStruct->GPIO_Mode==GPIO_Mode_OUT)||(GPIO_InitStruct->GPIO_Mode==GPIO_Mode_AF))
/*CheckSpeedmodeparameters*/
assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
/*Speedmodeconfiguration*/
GPIOx->OSPEEDR&=~(GPIO_OSPEEDER_OSPEEDR0<<(pinpos*2));
GPIOx->OSPEEDR|=((uint32_t)(GPIO_InitStruct->GPIO_Speed)<<(pinpos*2));
/*CheckOutputmodeparameters*/
assert_param(IS_GPIO_OTYPE(GPIO_InitStruct->GPIO_OType));
/*Outputmodeconfiguration*/
GPIOx->OTYPER&=~((GPIO_OTYPER_OT_0)<<((uint16_t)pinpos));
GPIOx->OTYPER|=(uint16_t)(((uint16_t)GPIO_InitStruct->GPIO_OType)<<((uint16_t)pinpos));
/*Pull-upPulldownresistorconfiguration*/
GPIOx->PUPDR&=~(GPIO_PUPDR_PUPDR0<<((uint16_t)pinpos*2));
GPIOx->PUPDR|=(((uint32_t)GPIO_InitStruct->GPIO_PuPd)<<(pinpos*2));
voidGPIO_StructInit(GPIO_InitTypeDef*GPIO_InitStruct)
/*ResetGPIOinitstructureparametersvalues*/
GPIO_InitStruct->GPIO_Pin=GPIO_Pin_All;
GPIO_InitStruct->GPIO_Mode=GPIO_Mode_IN;
GPIO_InitStruct->GPIO_Speed=GPIO_Speed_2MHz;
GPIO_InitStruct->GPIO_OType=GPIO_OType_PP;
GPIO_InitStruct->GPIO_PuPd=GPIO_PuPd_NOPULL;
voidGPIO_PinLockConfig(GPIO_TypeDef*GPIOx,uint16_tGPIO_Pin)
__IOuint32_ttmp=0x00010000;
assert_param(IS_GPIO_PIN(GPIO_Pin));
tmp|=GPIO_Pin;
/*SetLCKKbit*/
GPIOx->LCKR=tmp;
/*ResetLCKKbit*/
GPIOx->LCKR=GPIO_Pin;
/*ReadLCKKbit*/
tmp=GPIOx->LCKR;
GPIOReadandWrite
==========================
uint8_tGPIO_ReadInputDataBit(GPIO_TypeDef*GPIOx,uint16_tGPIO_Pin)
uint8_tbitstatus=0x00;
assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
if((GPIOx->IDR&GPIO_Pin)!
=(uint32_t)Bit_RESET)
bitstatus=(uint8_t)Bit_SET;
bitstatus=(uint8_t)Bit_RESET;
returnbitstatus;
uint16_tGPIO_ReadInputData(GPIO_TypeDef*GPIOx)
return((uint16_t)GPIOx->IDR);
uint8_tGPIO_ReadOutputDataBit(GPIO_TypeDef*GPIOx,uint16_tGPIO_Pin)
if((GPIOx->ODR&GPIO_Pin)!
uint16_tGPIO_ReadOutputData(GPIO_TypeDef*GPIOx)
return((uint16_t)GPIOx->ODR);
voidGPIO_SetBits(GPIO_TypeDef*GPIOx,uint16_tGPIO_Pin)
GPIOx->BSRRL=GPIO_Pin;
voidGPIO_ResetBits(GPIO_TypeDef*GPIOx,uint16_tGPIO_Pin)
GPIOx->BSRRH=GPIO_Pin;
voidGPIO_WriteBit(GPIO_TypeDef*GPIOx,uint16_tGPIO_Pin,BitActionBitVal)
assert_param(IS_GPIO_BIT_ACTION(BitVal));
if(BitVal!
=Bit_RESET)
voidGPIO_Write(GPIO_TypeDef*GP
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2