找回密码
 注册

QQ登录

只需一步,快速开始

搜索

STM32F103程序两个灯一亮一灭 每按一下开关 两个灯的亮灭状态翻转

[复制链接]
路漫漫 发表于 2020-6-6 23:11:29 | 显示全部楼层 |阅读模式
STM32F103设计电路,在GPIO C口,分别接有一个开关K1和两个指示灯LED1和LED2。两个灯一亮一灭,每按一下开关,两个灯的亮灭状态翻转
  1. #include"stm32f10x.h"
  2. /*pc5green,6red,10button*/
  3. void LED_Init(void)
  4. GPIO_InitTypeDef GPIO_InitStructure;
  5. {RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
  6. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_6;
  7. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

  8. GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP;//输出模式

  9. GPIO_Init(GPIOC,&GPIO_InitStructure);      
  10. }
  11. void KEY_Init(void)
  12. {
  13. //定义一个GPIO_InitTypeDef类型的结构体
  14. GPIO_InitTypeDefGPIO_InitStructure;
  15. //开启GPIO相关的外设时钟
  16. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
  17. //选择要控制的GPIO引脚
  18. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_10;
  19. //设置GPIO引脚的输入模式
  20. GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IPU;//高电平
  21. //调用库函数初始化GPIO
  22. GPIO_Init(GPIOC,&GPIO_InitStructure);
  23. }
  24. int main(void)
  25. {int i;
  26.     LED_Init();
  27.   KEY_Init();
  28. GPIO_ResetBits(GPIOC,GPIO_Pin_5);//green亮
  29.         GPIO_WriteBit(GPIOC,GPIO_Pin_6,Bit_SET);//red灭
  30.     while(1){

  31.         if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_10))
  32.             {
  33.                 GPIO_WriteBit(GPIOC,GPIO_Pin_5,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_5)));
  34.                  for(i=0;i<500000;i++) { } ;
  35.              GPIO_WriteBit(GPIOC,GPIO_Pin_6,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_6)));
  36.                     for(i=0;i<500000;i++) { };

  37.                     }
  38.             }
  39.         }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|ELEOK |网站地图

GMT+8, 2024-3-29 05:27

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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