STM32F4的IO設(shè)置測(cè)試
環(huán)境:
主機(jī):WIN7
開發(fā)環(huán)境:MDK4.72
MCU:STM32F407VGT6
說明:
目標(biāo)板上有一個(gè)LED,有一個(gè)按鍵,按鍵實(shí)現(xiàn)LED狀態(tài)翻轉(zhuǎn).
LED:PE2,低電平亮,高電平燈滅
按鍵:PC13,低電平按下,高電平松開
源代碼:
main.c
/*********************************************************************
*主文件
*(c)copyright2014,jdh
*AllRightReserved
*新建日期:2014/3/25byjdh
**********************************************************************/
/*********************************************************************
*頭文件
**********************************************************************/
#include"main.h"
#include"stm32f4xx_rcc.h"
#include"stm32f4xx_gpio.h"
/*********************************************************************
*全局變量
**********************************************************************/
static__IOuint32_tTimingDelay;
/*********************************************************************
*函數(shù)定義
**********************************************************************/
voidDelay(__IOuint32_tnTime);
/*********************************************************************
*函數(shù)
**********************************************************************/
intmain(void)
{
//定義IO初始化結(jié)構(gòu)體
GPIO_InitTypeDefGPIO_InitStructure;
//系統(tǒng)時(shí)鐘:1ms滴答1次
if(SysTick_Config(SystemCoreClock/1000))
{
while(1);
}
//設(shè)置LED的IO口
//初始化時(shí)鐘
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE);
//管腳模式:輸出口
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
//類型:推挽模式
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
//上拉下拉設(shè)置:不使能
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
//IO口速度
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
//管腳指定
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
//初始化
GPIO_Init(GPIOE,&GPIO_InitStructure);
//設(shè)置按鍵的IO口
//初始化時(shí)鐘
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
//管腳模式:輸出口
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN;
//類型:推挽模式
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
//上拉下拉設(shè)置:不使能
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
//IO口速度
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
//管腳指定
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;
//初始化
GPIO_Init(GPIOC,&GPIO_InitStructure);
while(1)
{
//GPIO_SetBits(GPIOE,GPIO_Pin_2);
//Delay(500);
//GPIO_ResetBits(GPIOE,GPIO_Pin_2);
//Delay(500);
//按鍵檢測(cè)
if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13)==0)
{
GPIO_ToggleBits(GPIOE,GPIO_Pin_2);
Delay(500);
}
}
}
/**
*@briefInsertsadelaytime.
*@paramnTime:specifiesthedelaytimelength,inmilliseconds.
*@retvalNone
*/
voidDelay(__IOuint32_tnTime)
{
TimingDelay=nTime;
while(TimingDelay!=0);
}
/**
*@briefDecrementstheTimingDelayvariable.
*@paramNone
*@retvalNone
*/
voidTimingDelay_Decrement(void)
{
if(TimingDelay!=0x00)
{
TimingDelay--;
}
}
#ifdefUSE_FULL_ASSERT
/**
*@briefReportsthenameofthesourcefileandthesourcelinenumber
*wheretheassert_paramerrorhasoccurred.
*@paramfile:pointertothesourcefilename
*@paramline:assert_paramerrorlinesourcenumber
*@retvalNone
*/
voidassert_failed(uint8_t*file,uint32_tline)
{
/*Usercanaddhisownimplementationtoreportthefilenameandlinenumber,
ex:printf("Wrongparametersvalue:file%sonline%drn",file,line)*/
/*Infiniteloop*/
while(1)
{
}
}
#endif
/**
*@}
*/
/**
*@}
*/
/*******************(C)COPYRIGHT2011STMicroelectronics*****ENDOFFILE****/