【HC32F460开发板测评】04 独立按键控制LED

1nnocent   2021-4-20 11:11 楼主

首先大概看下独立按键的硬件资源:

EVB 配置 5 个独立按键,分别为 1 个复位按键和 4 个用户按键。复位按键作为芯片的硬件复位;
用户按键则作为预留的外设连接到 MCU GPIO。通过下表中的引脚连接到 MCU:

丝印 管脚/功能
SW1 RESET/复位按键
SW2 PD3/用户按键
SW3 PD4/用户按键
SW4 PD5/用户按键
SW5 PD6/用户按键

image.png 从原理图可知,该独立按键使用硬件消抖的方式,按键按下时(以SW4为例),

电阻R121和电容C83放电进行消抖操作,

 

这个实验还需要使用到LED的硬件资源:

EVB 配置 5 个指示灯,分别为电源指示灯和用户指示灯。电源指示灯用于显示是否正常通电,
用户指示灯作为预留的外设连接到 MCU GPIO。 管脚分配请参考下表:

丝印 管脚/功能
D1 3V3 电源指示灯
D23 PE6/红色指示灯
D26 PA7/绿色指示灯
D27 PB5/黄色指示灯
D30 PB9/蓝色指示灯

image.png

以下是具体代码:

led.c

/******************************************************************************/
/** \file wm8731.c
 **
 ** \brief I2S codec WM8731 driver API functions
 **
 **   - 2018-11-13  1.0  Wangmin First version for Device Driver Library
 **
 ******************************************************************************/

/*******************************************************************************
 * Include files
 ******************************************************************************/
#include "hc32_ddl.h"
#include "led.h"

/*******************************************************************************
 * Local type definitions ('typedef')
 ******************************************************************************/

/*******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/

/*******************************************************************************
 * Global variable definitions (declared in header file with 'extern')
 ******************************************************************************/

/*******************************************************************************
 * Local function prototypes ('static')
 ******************************************************************************/

/*******************************************************************************
 * Local variable definitions ('static')
 ******************************************************************************/

/*******************************************************************************
 * Function implementation - global ('extern') and local ('static')
 ******************************************************************************/
 
/**
 *******************************************************************************
 ** \brief  Initialize LED0 for indicate recoder status
 **
 ** \param  None
 **
 ** \retval None
 **
 ******************************************************************************/
void IniLedPort(void)
{
    stc_port_init_t stcPortInit;
    /*initiallize LED port*/
    MEM_ZERO_STRUCT(stcPortInit);

    stcPortInit.enPinMode = Pin_Mode_Out;
    stcPortInit.enExInt = Enable;
    stcPortInit.enPullUp = Enable;

    /* LED0 Port/Pin initialization */
    PORT_Init(LED0_PORT, LED0_PIN, &stcPortInit);
	
    /* LED1 Port/Pin initialization */
    PORT_Init(LED1_PORT, LED1_PIN, &stcPortInit);
	
    /* LED2 Port/Pin initialization */
    PORT_Init(LED2_PORT, LED2_PIN, &stcPortInit);
	
    /* LED3 Port/Pin initialization */
    PORT_Init(LED3_PORT, LED3_PIN, &stcPortInit);
}
/*******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/

led.h

/******************************************************************************/
/** \file led.h
 **
 ** A detailed description is available at
 ** @link led Series description @endlink
 **
 **   - 2021-4-15  1.0  First version for i2s Series Functions description
 **     Library.
 **
 ******************************************************************************/
#ifndef __LED_H__
#define __LED_H__

/*******************************************************************************
 * Include files
 ******************************************************************************/
 
 

/* LED0 Port/Pin definition */
#define  LED0_PORT          PortE
#define  LED0_PIN           Pin06
/* LED0~1 toggle definition */
#define  LED0_TOGGLE()      PORT_Toggle(LED0_PORT, LED0_PIN) 

/* LED0 Port/Pin definition */
#define  LED1_PORT          PortA
#define  LED1_PIN           Pin07
/* LED0~1 toggle definition */
#define  LED1_TOGGLE()      PORT_Toggle(LED1_PORT, LED1_PIN)

/* LED0 Port/Pin definition */
#define  LED2_PORT          PortB
#define  LED2_PIN           Pin05
/* LED0~1 toggle definition */
#define  LED2_TOGGLE()      PORT_Toggle(LED2_PORT, LED2_PIN)

/* LED0 Port/Pin definition */
#define  LED3_PORT          PortB
#define  LED3_PIN           Pin09
/* LED0~1 toggle definition */
#define  LED3_TOGGLE()      PORT_Toggle(LED3_PORT, LED3_PIN)



void IniLedPort(void);


#endif /* __LED_H__ */

/*******************************************************************************
 * End of file
 ******************************************************************************/





key.c

/******************************************************************************/
/** \file key.c
 **
 ** \brief Key functions
 **
 **   - 2021-04-15  1.0  First version for Key
 **
 ******************************************************************************/

/*******************************************************************************
 * Include files
 ******************************************************************************/
#include "hc32_ddl.h"
#include "key.h"
#include "led.h"

/*******************************************************************************
 * Local type definitions ('typedef')
 ******************************************************************************/

/*******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/

/*******************************************************************************
 * Global variable definitions (declared in header file with 'extern')
 ******************************************************************************/

/*******************************************************************************
 * Local function prototypes ('static')
 ******************************************************************************/

/**
 *******************************************************************************
 ** \brief  Initialize keys
 **
 ** \param  None
 **
 ** \retval None
 **
 ******************************************************************************/
void IniKey(void)
{
    stc_port_init_t stcPortInit;
    /*initiallize KEY port*/
    MEM_ZERO_STRUCT(stcPortInit);

    /* KEY0 Port/Pin initialization */
    PORT_Init(KEY2_PORT, KEY2_PIN, &stcPortInit);
    /* KEY1 Port/Pin initialization */
    PORT_Init(KEY3_PORT, KEY3_PIN, &stcPortInit);
    /* KEY2 Port/Pin initialization */
    PORT_Init(KEY4_PORT, KEY4_PIN, &stcPortInit);
    /* KEY3 Port/Pin initialization */
    PORT_Init(KEY5_PORT, KEY5_PIN, &stcPortInit);
}

/**
 *******************************************************************************
 ** \brief  keyscan
 **
 ** \param  None
 **
 ** \retval None
 **
 ******************************************************************************/
void KeyScan(void)
{
	if(KEY2 == 0)
	{
		LED0_TOGGLE();
		Ddl_Delay1ms(10);
		LED0_TOGGLE();
	}
	if(KEY3 == 0)
	{
		LED1_TOGGLE();
		Ddl_Delay1ms(10);
		LED1_TOGGLE();
	}
	if(KEY4 == 0)
	{
		LED2_TOGGLE();
		Ddl_Delay1ms(10);
		LED2_TOGGLE();
	}
	if(KEY5 == 0)
	{
		LED3_TOGGLE();
		Ddl_Delay1ms(10);
		LED3_TOGGLE();
	}
}

/*******************************************************************************
 * Local variable definitions ('static')
 ******************************************************************************/

/*******************************************************************************
 * Function implementation - global ('extern') and local ('static')
 ******************************************************************************/

/*******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/


key.h

/******************************************************************************/
/** \file key.h
 **
 ** A detailed description is available at
 ** @link key Series description @endlink
 **
 **   - 2021-4-15  1.0  First version for Key Functions description
 **     Library.
 **
 ******************************************************************************/
#ifndef __KEY_H__
#define __KEY_H__

/*******************************************************************************
 * Include files
 ******************************************************************************/
 
/*******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/
/* KEY0 Port/Pin definition *///SW2
#define  KEY2_PORT          (PortD)
#define  KEY2_PIN           (Pin03)
#define  KEY2								PORT_GetBit(PortD,Pin03)
/* KEY2 Port/Pin definition *///SW3
#define  KEY3_PORT          PortD
#define  KEY3_PIN           Pin05
#define  KEY3								PORT_GetBit(PortD,Pin05)
/* KEY1 Port/Pin definition *///SW4
#define  KEY4_PORT          (PortD)
#define  KEY4_PIN           (Pin04)
#define  KEY4								PORT_GetBit(PortD,Pin04)
/* KEY3 Port/Pin definition *///SW5
#define  KEY5_PORT          PortD
#define  KEY5_PIN           Pin06
#define  KEY5								PORT_GetBit(PortD,Pin06)
 
void IniKey(void);
void KeyScan(void);
 
#endif /* __KEY_H__ */

/*******************************************************************************
 * End of file
 ******************************************************************************/




main.c

/**
 *******************************************************************************
 * @file  main.c
 * @brief Main program template.
 @verbatim
   Change Logs:
   Date             Author          Notes
   2020-06-30       Zhangxl         First version
 @endverbatim
 *******************************************************************************
 * Copyright (C) 2016, Huada Semiconductor Co., Ltd. All rights reserved.
 *
 * This software is owned and published by:
 * Huada Semiconductor Co., Ltd. ("HDSC").
 *
 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
 *
 * This software contains source code for use with HDSC
 * components. This software is licensed by HDSC to be adapted only
 * for use in systems utilizing HDSC components. HDSC shall not be
 * responsible for misuse or illegal use of this software for devices not
 * supported herein. HDSC is providing this software "AS IS" and will
 * not be responsible for issues arising from incorrect user implementation
 * of the software.
 *
 * Disclaimer:
 * HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
 * REGARDING THE SOFTWARE (INCLUDING ANY ACCOMPANYING WRITTEN MATERIALS),
 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
 * WARRANTY OF NONINFRINGEMENT.
 * HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
 * SAVINGS OR PROFITS,
 * EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
 * FROM, THE SOFTWARE.
 *
 * This software may be replicated in part or whole for the licensed use,
 * with the restriction that this Disclaimer and Copyright notice must be
 * included with each copy of this software, whether used in part or whole,
 * at all times.
 *******************************************************************************
 */

/*******************************************************************************
 * Include files
 ******************************************************************************/
#include "hc32_ddl.h"
#include "led.h"
#include "key.h"

/*******************************************************************************
 * Local type definitions ('typedef')
 ******************************************************************************/

/*******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/

/*******************************************************************************
 * Global variable definitions (declared in header file with 'extern')
 ******************************************************************************/

/*******************************************************************************
 * Local function prototypes ('static')
 ******************************************************************************/

/*******************************************************************************
 * Local variable definitions ('static')
 ******************************************************************************/

/*******************************************************************************
 * Function implementation - global ('extern') and local ('static')
 ******************************************************************************/

/**
 * @brief  Main function of template project
 * @param  None
 * @retval int32_t return value, if needed
 */
int32_t main(void)
{
	while(1)
	{
		/* Initialize Key */
		IniKey();
		/* Initialize LED0 */
		IniLedPort();
    while(1)
    {
			KeyScan();
    }
	}
}

/*******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/

该实验,按下一个按键对应一个灯闪烁,一共四个按键对应四个灯闪烁。

这里发现一个小问题,独立按键虽然使用了硬件消抖,但好像有时不是很好用,

刚开始该实验室按键按一下对应的灯状态取反一次,验证的时候会存在按键按完等的状态还是与初始的状态一致,

说明按“一次”按键灯的状态连续取反了两次,消抖不是那么干净(可以将RC增大,提高时间常数应该会有改善),

也有可能是个人按按键的方式问题,不知道其他小伙伴是否有遇到过这种问题。

下面是实验的效果:


板子的电源指示灯被我用胶带粘住了,灯有点亮晃眼睛

hc32f460petb_template_key.rar (2.98 MB)
(下载次数: 15, 2021-4-20 11:15 上传)

本帖最后由 1nnocent 于 2021-4-20 11:15 编辑

回复评论 (6)

下面整个自动跑马灯程序,不用手按键了

点赞  2021-4-20 17:39
引用: 火辣西米秀 发表于 2021-4-20 17:39 下面整个自动跑马灯程序,不用手按键了

跑马灯之前有玩过

点赞  2021-4-21 13:58
引用: 1nnocent 发表于 2021-4-21 13:58 跑马灯之前有玩过

有hc32f460 kcta的led源程序吗

 

点赞  2021-7-17 12:19
引用: 逃之之夭夭 发表于 2021-7-17 12:19 有hc32f460 kcta的led源程序吗  

另一个帖子有,不过是PBTE的程序

点赞  2021-7-17 18:35
引用: 1nnocent 发表于 2021-7-17 18:35 另一个帖子有,不过是PBTE的程序

大佬,咱们这个hc32f460   IO口是怎么使能的,刚开始接触这,实在是没搞明白、、、、

 

点赞  2021-7-28 10:07
引用: 逃之之夭夭 发表于 2021-7-28 10:07 大佬,咱们这个hc32f460   IO口是怎么使能的,刚开始接触这,实在是没搞明白、、、、   ...

程序下载下来看看就懂了

 

点赞  2021-7-28 12:49
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复