3-4STM32C8T6按键控制LED开与关

实物接线如下:


为了代码的简洁性,这里需要对LED与KEY进行封装如下:

c 复制代码
#include "stm32f10x.h"                  // Device header

void LED_Init(void)
{
  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1 | GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOA,&GPIO_InitStructure );
    
    GPIO_SetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2);
    
}

void LED1_ON()
{
   GPIO_ResetBits(GPIOA,GPIO_Pin_1);
}
void LED1_OFF()
{
  GPIO_SetBits(GPIOA,GPIO_Pin_1);
}
void LED1_Turn()
{
  if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_1)==0)
   GPIO_SetBits(GPIOA,GPIO_Pin_1);   
  else
   GPIO_ResetBits(GPIOA,GPIO_Pin_1);
}
void LED2_ON()
{
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
}

void LED2_OFF()
{
  GPIO_SetBits(GPIOA,GPIO_Pin_2);
}
void LED2_Turn()
{
  if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_2)==0)
   GPIO_SetBits(GPIOA,GPIO_Pin_2);   
  else
   GPIO_ResetBits(GPIOA,GPIO_Pin_2);
}
c 复制代码
#ifndef __LED_H
#define __LED_H
void LED_Init(void);
void LED1_ON(void);
void LED1_OFF(void);
void LED2_ON(void);
void LED2_OFF(void);
void LED1_Turn(void);
void LED2_Turn(void);

#endif
c 复制代码
#include "stm32f10x.h"                  // Device header
#include "delay.h"
void Key_Init()
{
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  GPIO_InitTypeDef GPIO_Structure;
  GPIO_Structure.GPIO_Mode=GPIO_Mode_IPU;
  GPIO_Structure.GPIO_Pin=GPIO_Pin_1 | GPIO_Pin_11;
  GPIO_Structure.GPIO_Speed=GPIO_Speed_50MHz; 
  GPIO_Init(GPIOB,&GPIO_Structure);    
}
uint8_t Key_GetNum()
{
  
  uint8_t  KeyNum=0;
   if( GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0)
   {
        Delay_ms(20);
        while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0);
        Delay_ms(20);
        KeyNum=1;
   }
      if( GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0)
   {
        Delay_ms(20);
        while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0);
        Delay_ms(20);
       KeyNum=2;
   }
    
    return KeyNum;

}
c 复制代码
#ifndef __KEY_H
#define __KEY_H
void Key_Init(void);
uint8_t Key_GetNum(void);

#endif
c 复制代码
#include "stm32f10x.h"                  // Device header
#include "delay.h"
#include "LED.h"
#include "Key.h"
uint8_t KeyNum;
int main(void)
{
  LED_Init(); 
  Key_Init();
  while(1)
  {
      KeyNum=Key_GetNum();
      if(KeyNum==1)
      {
       LED1_Turn();
      }
         if(KeyNum==2)
      {
       LED2_Turn();
      }
  }
}
相关推荐
李永奉5 小时前
中科蓝讯SDK开发-BT893x 面条耳机连接vivo手机后连接APP端,最后手机端设置页面手动断开蓝牙连接,此时耳机端假断开
c语言·开发语言·嵌入式硬件·物联网·智能手机·电脑
大爱编程♡5 小时前
STM32-DMA
stm32·单片机·嵌入式硬件
云泽8086 小时前
STM32 USART 详解(三):硬件架构、内部寄存器与物理层收发原理
stm32·嵌入式硬件·硬件架构
天空'之城6 小时前
单片机基础核心知识点汇总(三十三)
数据库·单片机·嵌入式硬件
数字新视界8 小时前
机房动环监控的智能化管理与安全提升探索
嵌入式硬件·物联网·数据中心·数据中心基础设施管理·dcim管理系统
@嵌入式扫地僧9 小时前
TFLite Micro STM32/ESP32 开箱即用推理骨架 + INT8 量化脚本实战:零调试跑通端侧 AI 推理完整步骤
人工智能·嵌入式硬件·tinyml·端侧ai·tflite micro
自小吃多9 小时前
高云FPGA ModelSim中的仿真
笔记·嵌入式硬件·fpga开发
单片机仿真设计9 小时前
基于 STM32 的家庭环境监测与火灾报警灭火系统(Proteus 仿真)
stm32·单片机·嵌入式硬件·proteus·毕设
深圳市恒锐丰科技杨生9 小时前
EG4321 锂电池 CC‑CV 充电管理控制器|屹晶 EGmicro
嵌入式硬件·硬件工程
辰域电子10 小时前
STM32项目开源:智能万年历/智能闹钟设计(代码+原理图+仿真)
stm32·单片机·嵌入式硬件·开源·proteus