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();
      }
  }
}
相关推荐
fengfuyao9856 小时前
STM32如何定位HardFault错误,一种实用方法
stm32·单片机·嵌入式硬件
爱学习的颖颖7 小时前
EXTI外部中断的执行逻辑|以对射式红外传感器计次为例
单片机·嵌入式硬件·exti中断
keer_zu8 小时前
STM32L051 RTC闹钟配置详解
stm32·嵌入式硬件
AI精钢8 小时前
H20芯片与中国的科技自立:一场隐形的博弈
人工智能·科技·stm32·单片机·物联网
etcix11 小时前
implement copy file content to clipboard on Windows
windows·stm32·单片机
谱写秋天11 小时前
在STM32F103上进行FreeRTOS移植和配置(STM32CubeIDE)
c语言·stm32·单片机·freertos
globbo15 小时前
【嵌入式STM32】I2C总结
单片机·嵌入式硬件
玖別ԅ(¯﹃¯ԅ)16 小时前
SysTick寄存器(嘀嗒定时器实现延时)
stm32·单片机·嵌入式硬件
limitless_peter16 小时前
集成运算放大器(反向比例,同相比例)
嵌入式硬件·硬件工程
Blossom.11817 小时前
把 AI 推理塞进「 8 位 MCU 」——0.5 KB RAM 跑通关键词唤醒的魔幻之旅
人工智能·笔记·单片机·嵌入式硬件·深度学习·机器学习·搜索引擎