CH32V307VCT6单片机-day01-LED
本文最后更新于275天前,其中的信息可能已经有所发展或是发生改变。

1. LED头文件和源文件

led.h

/*
 * led.h
 *
 *  Created on: 2023年7月27日
 *      Author: Mondayice
 */

#ifndef HARDWARE_LED_LED_H_
#define HARDWARE_LED_LED_H_

#include "ch32v30x_conf.h"
void LED_Init(void);

#endif /* HARDWARE_LED_LED_H_ */

led.c

/*
 * led.c
 *
 *  Created on: 2023年7月27日
 *      Author: Mondayice
 */


#include "led.h"

/*
 * led初始化函数
 */
void LED_Init(void){
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; // 端口号
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;// 速度
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;// 推挽输出
    GPIO_Init(GPIOA, &GPIO_InitStructure);

//    GPIO_SetBits(GPIOA, GPIO_Pin_0);
    GPIO_SetBits(GPIOA, GPIO_Pin_0|GPIO_Pin_1);
}

初始化GPIO的PA0和PA1脚,设置速度为50Mhz,推挽输出。

2.key头文件和源文件

key.h

/*
 * key.h
 *
 *  Created on: 2023年7月27日
 *      Author: Mondayice
 */

#ifndef HARDWARE_KEY_KEY_H_
#define HARDWARE_KEY_KEY_H_

#include "ch32v30x_conf.h"
#define KEY GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_2)
#define KEY_PRESS 1

void Key_Init(void);
u8 KEY_Scan(u8 mode);


#endif /* HARDWARE_KEY_KEY_H_ */

key.c

/*
 * key.c
 *
 *  Created on: 2023年7月27日
 *      Author: Mondayice
 */


#include "ch32v30x_conf.h"
#include "key.h"

void Key_Init(void){
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // 端口号
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;// 速度
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;// 上拉输出
    GPIO_Init(GPIOA, &GPIO_InitStructure);
}
u8 KEY_Scan(u8 mode){
    static u8 key_up = 1;
    if(mode){
        key_up = 0;
    }
    if(key_up&&(KEY == 0)){
        Delay_Ms(5);
        key_up = 0;
        if(KEY == 0){
            return KEY_PRESS;
        }

    }else if(KEY == 1){
        key_up = 1;
    }
    return 0;
}

这段代码是一个按键扫描函数,用于检测按键是否按下。以下是对每个部分的功能解释:

KEY_Scan(u8 mode)

这是一个函数定义,接受一个mode参数作为输入。

static u8 key_up = 1;

key_up是一个静态变量,用于保存按键的状态。初始值设置为1,表示按键松开状态。

if(mode){
    key_up = 0;
}

如果mode参数不为0(即为非零值),将key_up设置为0。这个部分用于在需要连续检测按键时,避免错误地认为按键已经松开。

if(key_up&&(KEY == 0)){
    Delay_Ms(5);
    key_up = 0;
    if(KEY == 0){
        return KEY_PRESS;
    }
} else if(KEY == 1){
    key_up = 1;
}

在这个条件语句中,首先判断key_up为1并且KEY引脚为低电平(按键按下)的情况。如果满足条件,进行一个短暂的延时(通过调用Delay_Ms()函数),然后再次检查KEY引脚的状态。如果KEY仍然为低电平,说明按键确实被按下,返回KEY_PRESS(按键按下)。

如果不满足上述条件,进入else if语句块,判断KEY引脚是否为高电平(按键松开)。如果是,则将key_up设置为1,表示按键已经松开。

最后,如果以上条件都不满足,函数返回0,表示没有检测到按键按下。

main.c

/********************************** (C) COPYRIGHT *******************************
* File Name          : main.c
* Author             : WCH
* Version            : V1.0.0
* Date               : 2021/06/06
* Description        : Main program body.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for 
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/

/*
 *@Note
 USART Print debugging routine:
 USART1_Tx(PA9).
 This example demonstrates using USART1(PA9) as a print debug port output.

*/

#include "debug.h"
#include "led.h"
#include "key.h"

/* Global typedef */

/* Global define */

/* Global Variable */


/*********************************************************************
 * @fn      main
 *
 * @brief   Main program.
 *
 * @return  none
 */
int main(void)
{
    static u8 j = 1;
    static u8 i = 1;
    static u8 k = 0;
    u8 t = 0;
    /**************
     * 串口打印
     *
     */
    USART_Printf_Init(115200);
        Delay_Init();
        LED_Init();
        Key_Init();
        printf("SystemClk:%d\r\n",SystemCoreClock);

    while(1)
    {
        Delay_Ms(250);
        t = KEY_Scan(0);
        if(t){
            GPIO_WriteBit(GPIOA, GPIO_Pin_0|GPIO_Pin_1, 1);
            k=(k+1)%4;
            printf("k=%d\r\n",k);
        }
        switch(k){
            case 0:
                GPIO_WriteBit(GPIOA, GPIO_Pin_1, (j == 0)?(j = Bit_SET):(j = Bit_RESET));
                Delay_Ms(250);
                GPIO_WriteBit(GPIOA, GPIO_Pin_0, (i == 0)?(i = Bit_SET):(i = Bit_RESET));
                break;
            case 1:
                GPIO_WriteBit(GPIOA, GPIO_Pin_0, (i == 0)?(i = Bit_SET):(i = Bit_RESET));
                break;
            case 2:
                GPIO_WriteBit(GPIOA, GPIO_Pin_1, (j == 0)?(j = Bit_SET):(j = Bit_RESET));
                break;
            case 3:
                GPIO_WriteBit(GPIOA, GPIO_Pin_0, (i == 0)?(i = Bit_SET):(i = Bit_RESET));
                GPIO_WriteBit(GPIOA, GPIO_Pin_1, (j == 0)?(j = Bit_SET):(j = Bit_RESET));
                break;
            default:
                   Delay_Ms(10);
        };
    }
}

点击数:4

    暂无评论

    发送评论 编辑评论

    
    				
    |´・ω・)ノ
    ヾ(≧∇≦*)ゝ
    (☆ω☆)
    (╯‵□′)╯︵┴─┴
     ̄﹃ ̄
    (/ω\)
    ∠( ᐛ 」∠)_
    (๑•̀ㅁ•́ฅ)
    →_→
    ୧(๑•̀⌄•́๑)૭
    ٩(ˊᗜˋ*)و
    (ノ°ο°)ノ
    (´இ皿இ`)
    ⌇●﹏●⌇
    (ฅ´ω`ฅ)
    (╯°A°)╯︵○○○
    φ( ̄∇ ̄o)
    ヾ(´・ ・`。)ノ"
    ( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
    (ó﹏ò。)
    Σ(っ °Д °;)っ
    ( ,,´・ω・)ノ"(´っω・`。)
    ╮(╯▽╰)╭
    o(*////▽////*)q
    >﹏<
    ( ๑´•ω•) "(ㆆᴗㆆ)
    😂
    😀
    😅
    😊
    🙂
    🙃
    😌
    😍
    😘
    😜
    😝
    😏
    😒
    🙄
    😳
    😡
    😔
    😫
    😱
    😭
    💩
    👻
    🙌
    🖕
    👍
    👫
    👬
    👭
    🌚
    🌝
    🙈
    💊
    😶
    🙏
    🍦
    🍉
    😣
    Source: github.com/k4yt3x/flowerhd
    颜文字
    Emoji
    小恐龙
    花!
    上一篇
    下一篇