找回密码
 立即注册

QQ登录

只需一步,快速开始

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

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

[复制链接]
跳转到指定楼层
楼主
ID:770730 发表于 2020-6-5 21:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
STM32F103设计电路,在GPIO C口,分别接有一个开关K1和两个指示灯LED1和LED2。两个灯一亮一灭,每按一下开关,两个灯的亮灭状态翻转


#include"stm32f10x.h"
/*pc5green,6red,10button*/
void LED_Init(void)
GPIO_InitTypeDef GPIO_InitStructure;
{RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
   
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP;//输出模式
   
GPIO_Init(GPIOC,&GPIO_InitStructure);      
}
void KEY_Init(void)
{
//定义一个GPIO_InitTypeDef类型的结构体
GPIO_InitTypeDefGPIO_InitStructure;
//开启GPIO相关的外设时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
//选择要控制的GPIO引脚
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_10;
//设置GPIO引脚的输入模式
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IPU;//高电平
//调用库函数初始化GPIO
GPIO_Init(GPIOC,&GPIO_InitStructure);
}
int main(void)
{int i;
    LED_Init();
  KEY_Init();
GPIO_ResetBits(GPIOC,GPIO_Pin_5);//green亮
        GPIO_WriteBit(GPIOC,GPIO_Pin_6,Bit_SET);//red灭
    while(1){
        
        if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_10))
            {
                GPIO_WriteBit(GPIOC,GPIO_Pin_5,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_5)));
                 for(i=0;i<500000;i++) { } ;
             GPIO_WriteBit(GPIOC,GPIO_Pin_6,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_6)));
                    for(i=0;i<500000;i++) { };
               
                    }
            }
        }
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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