拷贝.docx

上传人:b****8 文档编号:9468065 上传时间:2023-05-19 格式:DOCX 页数:14 大小:15.90KB
下载 相关 举报
拷贝.docx_第1页
第1页 / 共14页
拷贝.docx_第2页
第2页 / 共14页
拷贝.docx_第3页
第3页 / 共14页
拷贝.docx_第4页
第4页 / 共14页
拷贝.docx_第5页
第5页 / 共14页
拷贝.docx_第6页
第6页 / 共14页
拷贝.docx_第7页
第7页 / 共14页
拷贝.docx_第8页
第8页 / 共14页
拷贝.docx_第9页
第9页 / 共14页
拷贝.docx_第10页
第10页 / 共14页
拷贝.docx_第11页
第11页 / 共14页
拷贝.docx_第12页
第12页 / 共14页
拷贝.docx_第13页
第13页 / 共14页
拷贝.docx_第14页
第14页 / 共14页
亲,该文档总共14页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

拷贝.docx

《拷贝.docx》由会员分享,可在线阅读,更多相关《拷贝.docx(14页珍藏版)》请在冰点文库上搜索。

拷贝.docx

拷贝

/******************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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2