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();
      }
  }
}
相关推荐
触角010100011 小时前
OLED屏幕开发全解析:从硬件设计到物联网显示实战 | 零基础入门STM32第五十二步
驱动开发·stm32·单片机·嵌入式硬件·物联网
蓑衣客VS索尼克2 小时前
无感方波开环强拖总结
经验分享·单片机·学习
傍晚冰川4 小时前
【江协科技STM32】ADC数模转换器-学习笔记
笔记·科技·stm32·单片机·嵌入式硬件·学习
c-u-r-ry304 小时前
009---基于Verilog HDL的单比特信号边沿检测
嵌入式硬件·fpga开发
土豆198910214 小时前
简记_ MCU管脚的防静电处理
嵌入式硬件
Whappy0015 小时前
第三节:基于Winform框架的串口助手小项目---串口操作《C#编程》
linux·单片机·c#
爱吃奶酪的松鼠丶7 小时前
51单片机之蓝牙模块的使用
单片机·嵌入式硬件·51单片机
盐析大白兔8 小时前
STM32G431RBT6--(3)片上外设及其关系
stm32·单片机·嵌入式硬件
Joshua.X8 小时前
电脑总显示串口正在被占用处理方法
嵌入式硬件·串口·信息与通信·故障排查·232·485
与光同尘 大道至简9 小时前
中国嵌入式单片机就业形势分析
arm开发·python·单片机·嵌入式硬件·github·硬件工程