找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 7572|回复: 0
打印 上一主题 下一主题
收起左侧

stm32一个按键控制两个led灯

[复制链接]
跳转到指定楼层
楼主
ID:358048 发表于 2018-6-24 22:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
stm32一个按键控制两个led灯

  1. /**
  2.   ******************************************************************************
  3.   * @file    GPIO/IOToggle/main.c
  4.   * @author  MCD Application Team
  5.   * @version V3.5.0
  6.   * @date    08-April-2011
  7.   * @brief   Main program body.
  8.   */

  9. /* Includes ------------------------------------------------------------------*/
  10. #include "stm32f10x.h"

  11. //#include "stm32_eval.h"

  12. /** @addtogroup STM32F10x_StdPeriph_Examples
  13.   * @{
  14.   */

  15. /** @addtogroup GPIO_IOToggle
  16.   * @{
  17.   */

  18. /* Private typedef -----------------------------------------------------------*/
  19. /* Private define ------------------------------------------------------------*/
  20. /* Private macro -------------------------------------------------------------*/
  21. /* Private variables ---------------------------------------------------------*/
  22. //GPIO_InitTypeDef GPIO_InitStructure;

  23. /* Private function prototypes -----------------------------------------------*/
  24. /* Private functions ---------------------------------------------------------*/

  25. /**
  26.   * @brief  Main program.
  27.   * @param  None
  28.   * @retval None
  29.   */
  30. //#define LED0_OFF GPIO_SetBits(GPIOC,GPIO_Pin_8)
  31. //#define LED0_ON GPIO_ResetBits(GPIOC,GPIO_Pin_8)

  32. #define LED0_ON GPIO_SetBits(GPIOC,GPIO_Pin_8)
  33. #define LED0_OFF GPIO_ResetBits(GPIOC,GPIO_Pin_8)
  34. #define LED1_ON GPIO_SetBits(GPIOC,GPIO_Pin_9)
  35. #define LED1_OFF GPIO_ResetBits(GPIOC,GPIO_Pin_9)




  36. #define S1_DOWN GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)==0
  37. #define S1_UP GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)==1

  38. void LEDKEY_GPIO_Config(void)
  39. {
  40.      GPIO_InitTypeDef GPIO_InitStructure;
  41.      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
  42.      GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9;
  43.      GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  44.      GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  45.      GPIO_Init(GPIOC,&GPIO_InitStructure);
  46.      GPIO_SetBits(GPIOC,GPIO_Pin_8|GPIO_Pin_9);
  47.   


  48.          
  49.      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  50.          GPIO_InitStructure.GPIO_Pin=GPIO_Pin_15;
  51.      GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
  52.      GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  53.      GPIO_Init(GPIOB,&GPIO_InitStructure);
  54.      //GPIO_SetBits(GPIOB,GPIO_Pin_15);
  55.   }


  56. void delay_nms(u16 time)
  57. {
  58.    u16 i=0;
  59.    while(time--)
  60.    {
  61.       i=12000;
  62.       while(i--);
  63.    }
  64. }



  65. int main(void)
  66. {
  67.    u8  kcnt=0;
  68.    SystemInit();
  69.    LEDKEY_GPIO_Config();
  70.    
  71.   while (1)
  72.   {
  73.       if(S1_DOWN)
  74.           {
  75.               delay_nms(10);
  76.                   if(S1_DOWN)
  77.                   {
  78.                      while(S1_DOWN);
  79.                      kcnt++;
  80.                      }
  81.                          if(kcnt%4==0)
  82.                          {  
  83.                                 LED0_OFF;
  84.                                 LED1_OFF;
  85.                                                
  86.                          }
  87.                          if(kcnt%4==1)
  88.                          {  
  89.                                 LED0_ON;
  90.                                 LED1_OFF;
  91.                        
  92.                          }
  93.                          if(kcnt%4==2)
  94.                          {  
  95.                                 LED0_OFF;
  96.                                 LED1_ON;
  97.                                
  98.                          }
  99.                          if(kcnt%4==3)
  100.                          {   
  101.                                 LED0_ON;
  102.                                 LED1_ON;
  103.                        
  104.                          }
  105.        
  106.                        
  107.                        
  108.        }                                  
  109.   }
  110. }
复制代码


一个按键控制两个灯.7z

510.01 KB, 下载次数: 45, 下载积分: 黑币 -5

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表