从零开始的stm32学习(二):按键控制和光敏传感器(上)

一、模块化编程

封装好驱动代码,便于管理和移植。

在工程文件下新建文件夹,重命名Hardware(硬件),存放硬件驱动。回到kail5,新建文件夹Hardware,在魔术棒中添加路径。在Hardare文件夹下添加.c,.h文件,注意修改地址。

  1. 对于.c
#include "stm32f10x.h"                  // Device header

void LED_Init(void){
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);  // Enable the clock for GPIOA peripheral
    GPIO_InitTypeDef GPIO_InitStructure;                   // Create a structure to configure GPIO pins
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;       // Configure GPIO pins as output in push-pull mode
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2; // Specify the pins to be configured (Pin 1 and Pin 2)
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;      // Set the maximum output speed to 50MHz
    GPIO_Init(GPIOA, &GPIO_InitStructure);                 // Initialize GPIOA pins with the specified configuration
    GPIO_SetBits(GPIOA, GPIO_Pin_1 | GPIO_Pin_2);          // Set the initial state of Pin 1 and Pin 2 to high (LEDs are initially off)
}

void LED1_ON(void){
    GPIO_ResetBits(GPIOA, GPIO_Pin_1);                     // Turn on LED1 by setting Pin 1 to low
}

void LED2_ON(void){
    GPIO_ResetBits(GPIOA, GPIO_Pin_2);                     // Turn on LED2 by setting Pin 2 to low
}

void LED1_OFF(void){
    GPIO_SetBits(GPIOA, GPIO_Pin_1);                     // Turn off LED1 by setting Pin 1 to high
}

void LED2_OFF(void){
    GPIO_SetBits(GPIOA, GPIO_Pin_2);                     // Turn off LED2 by setting Pin 2 to high
}

keil5可能对中文注释不兼容,所以我添加了英文注释。在.c文件中需要先添加头文件,再添加自己定义的函数。上代码实现了GPIOA 1,2引脚的初始化和高低电平的配置。

  1. 对于.h
#ifndef _LED_H
#define _LED_H

void LED_Init(void);    // LED initialization function
void LED1_ON(void);     // Turn on LED1
void LED2_ON(void);     // Turn on LED2
void LED1_OFF(void);    // Turn off LED1
void LED2_OFF(void);    // Turn off LED2

#endif

前两行是声明,如果LED没有被定义,就定义LED。
ifndef 和 endif构成括号。

二、GPIO的读取

  1. GPIO_ReadInputDataBit()(读入指定引脚)
    GPIO_ReadInputDataBit()
GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1);
  1. GPIO_ReadInputData()(读入指定IO口)
    GPIO_ReadInputData()

  2. GPIO_ReadOutputDataBit()(读入指定输出引脚状态)
    GPIO_ReadOutputDataBit()

  3. GPIO_ReadOutputData()(读入指定输出GPIO状态)
    GPIO_ReadOutputData()

前两个读取输入,后两个读取输出的。

三、封装Key文件

了解1,2之后,可以将按键的开关模块化了。

  1. Key.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"

void Key_Init(void) {
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);  // Enable clock for GPIOB
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;  // Configure GPIO pins as input mode with pull-up resistors
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_11;  // Configure GPIO pins 1 and 11
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  // Set GPIO speed to 50MHz
    GPIO_Init(GPIOB, &GPIO_InitStructure);  // Initialize GPIOB with the specified configuration
}

uint8_t Key_GetNum(void) {
    uint8_t Key_Num = 0;
    
    if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0) {  // Check if GPIO pin 1 is low
        Delay_ms(20);  // Delay for debounce
        while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0);  // Wait for GPIO pin 1 to become high again
        Delay_ms(20);  // Delay for debounce
        Key_Num = 1;  // Set Key_Num to 1
    }
    
    if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0) {  // Check if GPIO pin 11 is low
        Delay_ms(20);  // Delay for debounce
        while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0);  // Wait for GPIO pin 11 to become high again
        Delay_ms(20);  // Delay for debounce
        Key_Num = 2;  // Set Key_Num to 2
    }
    
    return Key_Num;  // Return the value of Key_Num
}

Delay_ms(20)软件按键消抖

  1. Key.h
#ifndef _KEY_H
#define _KEY_H

void Key_Init(void);                    // Initialize the key
uint8_t Key_GetNum(void);                

#endif

四、main文件

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "LED.h"
#include "Key.h"

uint8_t KeyNum;

int main(void)
{
    LED_Init();                        // Initialize the LED
    Key_Init();                        // Initialize the keys
   
    while (1)
    {
        KeyNum = Key_GetNum();         // Get the number of the pressed key
        
        if (KeyNum == 1)
        {
            LED1_ON();               
        }
        
        if (KeyNum == 2)
        {
            LED1_OFF();               
        }
    }
}


五、连接电路

![按键控制](https://img-blog.csdnimg.cn/5c39bdb517ae4d0fb5bb7283b70c9aa6.jpeg

以上,就完成了两个按键控制一个LED的工程。

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值