您的位置:首页 > 其它

STM32F4的IO设置测试

2014-03-25 11:49 274 查看
STM32F4的IO设置测试

本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.

环境:

主机:WIN7

开发环境:MDK4.72

MCU:STM32F407VGT6

说明:

目标板上有一个LED,有一个按键,按键实现LED状态翻转.

LED:PE2,低电平亮,高电平灯灭

按键:PC13,低电平按下,高电平松开

源代码:
main.c

/*********************************************************************
*							  主文件
*						(c)copyright 2014,jdh
*						  All Right Reserved
*新建日期:2014/3/25 by jdh
**********************************************************************/

/*********************************************************************
*							头文件
**********************************************************************/

#include "main.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_gpio.h"

/*********************************************************************
*							全局变量
**********************************************************************/

static __IO uint32_t TimingDelay;

/*********************************************************************
*							函数定义
**********************************************************************/

void Delay(__IO uint32_t nTime);

/*********************************************************************
*							函数
**********************************************************************/

int main(void)
{
    //定义IO初始化结构体
	GPIO_InitTypeDef GPIO_InitStructure;
    
    //系统时钟:1ms滴答1次
    if (SysTick_Config(SystemCoreClock / 1000))
    { 
        while (1);
    }
  
    //设置LED的IO口
    //初始化时钟
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
    //管脚模式:输出口
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;	
    //类型:推挽模式
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;	
    //上拉下拉设置:不使能
	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);
    
    //设置按键的IO口
    //初始化时钟
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
    //管脚模式:输出口
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;	
    //类型:推挽模式
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;	
    //上拉下拉设置:不使能
	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);
        //按键检测
        if (GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13) == 0)
        {
            GPIO_ToggleBits(GPIOE,GPIO_Pin_2);
            Delay(500);
        }
    }
}

/**
  * @brief  Inserts a delay time.
  * @param  nTime: specifies the delay time length, in milliseconds.
  * @retval None
  */
void Delay(__IO uint32_t nTime)
{ 
  TimingDelay = nTime;

  while(TimingDelay != 0);
}

/**
  * @brief  Decrements the TimingDelay variable.
  * @param  None
  * @retval None
  */
void TimingDelay_Decrement(void)
{
  if (TimingDelay != 0x00)
  { 
    TimingDelay--;
  }
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/**
  * @}
  */ 

/**
  * @}
  */ 

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: