[STM32F4]

(转)STM32F4的IO设置测试

[复制链接]
664|2
手机看帖
扫描二维码
随时随地手机跟帖
renzheshengui|  楼主 | 2018-8-16 09:35 | 显示全部楼层 |阅读模式
本文博客链接: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****/


  
wakayi| | 2018-8-16 09:49 | 显示全部楼层
非常简单有效的小程序

使用特权

评论回复
renzheshengui|  楼主 | 2018-8-17 09:24 | 显示全部楼层
wakayi 发表于 2018-8-16 09:49
非常简单有效的小程序

非常感谢夸奖

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

78

主题

3808

帖子

2

粉丝