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();
      }
  }
}
相关推荐
张海森-16882011 分钟前
变量的作用域和生命周期
单片机
深圳市恒锐丰科技杨生1 小时前
EG3014S 80V 半桥栅极驱动芯片|低压半桥外置 MOS/IGBT 驱动方案
嵌入式硬件·硬件工程
Freak嵌入式1 小时前
MicroPython+Pico 接收空闲中断和发送空闲中断全实战
java·开发语言·stm32·单片机·嵌入式硬件
尼喃1 小时前
锂电池3.7V升压5V芯片全场景选型:耳机/音箱/USB输出三档方案
嵌入式硬件
LCG元1 小时前
STM32F103 硬件 I2C 通信全流程调试实录:从寄存器配置到总线死锁恢复
stm32·单片机·嵌入式硬件
LCG元2 小时前
STM32L4 ADC+DMA 多通道采集实战:从 CubeMX 配置到 VREFINT 电压校准
stm32·单片机·嵌入式硬件
单片机仿真设计2 小时前
【proteus仿真】基于 STM32 快递运输避障小车设计(仿真图+程序)
stm32·单片机·嵌入式硬件·proteus·毕设
lhbweilai2 小时前
工业互联网(二)——USB 设备编程(一)
单片机·嵌入式硬件
G***技3 小时前
IB3-771嵌入式主板6 TOPS真实算力怎么用:从PyTorch到RKNN的量化落地
人工智能·嵌入式硬件
国科安芯3 小时前
低轨卫星姿轨控系统中抗辐射MCU的选型依据与工程实践
单片机·嵌入式硬件·算法·架构·系统安全·低轨卫星·抗辐射