【HC32F460开发板测评】04 独立按键控制LED
本帖最后由 1nnocent 于 2021-4-20 11:15 编辑<p>首先大概看下独立按键的硬件资源:</p>
<p>EVB 配置 5 个独立按键,分别为 1 个复位按键和 4 个用户按键。复位按键作为芯片的硬件复位;<br />
用户按键则作为预留的外设连接到 MCU GPIO。通过下表中的引脚连接到 MCU:</p>
<table>
<tbody>
<tr>
<td width="200">丝印</td>
<td width="200">管脚/功能</td>
</tr>
<tr>
<td width="200">SW1</td>
<td width="200">RESET/复位按键</td>
</tr>
<tr>
<td width="200">SW2</td>
<td width="200">PD3/用户按键</td>
</tr>
<tr>
<td width="200">SW3</td>
<td width="200">PD4/用户按键</td>
</tr>
<tr>
<td width="200">SW4</td>
<td width="200">PD5/用户按键</td>
</tr>
<tr>
<td width="200">SW5</td>
<td width="200">PD6/用户按键</td>
</tr>
</tbody>
</table>
<p>从原理图可知,该独立按键使用硬件消抖的方式,按键按下时(以SW4为例),</p>
<p>电阻R121和电容C83放电进行消抖操作,</p>
<p> </p>
<p>这个实验还需要使用到LED的硬件资源:</p>
<p>EVB 配置 5 个指示灯,分别为电源指示灯和用户指示灯。电源指示灯用于显示是否正常通电,<br />
用户指示灯作为预留的外设连接到 MCU GPIO。 管脚分配请参考下表:</p>
<table>
<tbody>
<tr>
<td width="200">丝印</td>
<td width="200">管脚/功能</td>
</tr>
<tr>
<td width="200">D1</td>
<td width="200">3V3 电源指示灯</td>
</tr>
<tr>
<td width="200">D23</td>
<td width="200">PE6/红色指示灯</td>
</tr>
<tr>
<td width="200">D26</td>
<td width="200">PA7/绿色指示灯</td>
</tr>
<tr>
<td width="200">D27</td>
<td width="200">PB5/黄色指示灯</td>
</tr>
<tr>
<td width="200">D30</td>
<td width="200">PB9/蓝色指示灯</td>
</tr>
</tbody>
</table>
<p></p>
<p>以下是具体代码:</p>
<p>led.c</p>
<pre>
<code>/******************************************************************************/
/** \file wm8731.c
**
** \brief I2S codec WM8731 driver API functions
**
** - 2018-11-131.0Wangmin 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')
******************************************************************************/
/**
*******************************************************************************
** \briefInitialize LED0 for indicate recoder status
**
** \paramNone
**
** \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)
******************************************************************************/
</code></pre>
<p>led.h</p>
<pre>
<code>/******************************************************************************/
/** \file led.h
**
** A detailed description is available at
** <a href="home.php?mod=space&uid=43623" target="_blank">@link</a> led Series description @endlink
**
** - 2021-4-151.0First version for i2s Series Functions description
** Library.
**
******************************************************************************/
#ifndef __LED_H__
#define __LED_H__
/*******************************************************************************
* Include files
******************************************************************************/
/* LED0 Port/Pin definition */
#defineLED0_PORT PortE
#defineLED0_PIN Pin06
/* LED0~1 toggle definition */
#defineLED0_TOGGLE() PORT_Toggle(LED0_PORT, LED0_PIN)
/* LED0 Port/Pin definition */
#defineLED1_PORT PortA
#defineLED1_PIN Pin07
/* LED0~1 toggle definition */
#defineLED1_TOGGLE() PORT_Toggle(LED1_PORT, LED1_PIN)
/* LED0 Port/Pin definition */
#defineLED2_PORT PortB
#defineLED2_PIN Pin05
/* LED0~1 toggle definition */
#defineLED2_TOGGLE() PORT_Toggle(LED2_PORT, LED2_PIN)
/* LED0 Port/Pin definition */
#defineLED3_PORT PortB
#defineLED3_PIN Pin09
/* LED0~1 toggle definition */
#defineLED3_TOGGLE() PORT_Toggle(LED3_PORT, LED3_PIN)
void IniLedPort(void);
#endif /* __LED_H__ */
/*******************************************************************************
* End of file
******************************************************************************/
</code></pre>
<p>key.c</p>
<pre>
<code>/******************************************************************************/
/** \file key.c
**
** \brief Key functions
**
** - 2021-04-151.0First 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')
******************************************************************************/
/**
*******************************************************************************
** \briefInitialize keys
**
** \paramNone
**
** \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);
}
/**
*******************************************************************************
** \briefkeyscan
**
** \paramNone
**
** \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)
******************************************************************************/
</code></pre>
<p>key.h</p>
<pre>
<code>/******************************************************************************/
/** \file key.h
**
** A detailed description is available at
** @link key Series description @endlink
**
** - 2021-4-151.0First 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
#defineKEY2_PORT (PortD)
#defineKEY2_PIN (Pin03)
#defineKEY2 PORT_GetBit(PortD,Pin03)
/* KEY2 Port/Pin definition *///SW3
#defineKEY3_PORT PortD
#defineKEY3_PIN Pin05
#defineKEY3 PORT_GetBit(PortD,Pin05)
/* KEY1 Port/Pin definition *///SW4
#defineKEY4_PORT (PortD)
#defineKEY4_PIN (Pin04)
#defineKEY4 PORT_GetBit(PortD,Pin04)
/* KEY3 Port/Pin definition *///SW5
#defineKEY5_PORT PortD
#defineKEY5_PIN Pin06
#defineKEY5 PORT_GetBit(PortD,Pin06)
void IniKey(void);
void KeyScan(void);
#endif /* __KEY_H__ */
/*******************************************************************************
* End of file
******************************************************************************/
</code></pre>
<p>main.c</p>
<pre>
<code>/**
*******************************************************************************
* @filemain.c
* <a href="home.php?mod=space&uid=159083" target="_blank">@brief</a> 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')
******************************************************************************/
/**
* @briefMain function of template project
* @paramNone
* @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)
******************************************************************************/
</code></pre>
<p>该实验,按下一个按键对应一个灯闪烁,一共四个按键对应四个灯闪烁。</p>
<p>这里发现一个小问题,独立按键虽然使用了硬件消抖,但好像有时不是很好用,</p>
<p>刚开始该实验室按键按一下对应的灯状态取反一次,验证的时候会存在按键按完等的状态还是与初始的状态一致,</p>
<p>说明按“一次”按键灯的状态连续取反了两次,消抖不是那么干净(可以将RC增大,提高时间常数应该会有改善),</p>
<p>也有可能是个人按按键的方式问题,不知道其他小伙伴是否有遇到过这种问题。</p>
<p>下面是实验的效果:</p>
<p><iframe allowfullscreen="true" frameborder="0" height="450" src="//player.bilibili.com/player.html?bvid=1qV411J7yb&page=1" style="background:#eee;margin-bottom:10px;" width="750"></iframe><br />
板子的电源指示灯被我用胶带粘住了,灯有点亮晃眼睛<img height="29" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/face-with-cold-sweat_1f613.png" width="29" /></p>
<p></p>
<p>下面整个自动跑马灯程序,不用手按键了</p>
火辣西米秀 发表于 2021-4-20 17:39
下面整个自动跑马灯程序,不用手按键了
<p>跑马灯之前有玩过</p>
1nnocent 发表于 2021-4-21 13:58
跑马灯之前有玩过
<p>有hc32f460 kcta的led源程序吗</p>
<p> </p>
逃之之夭夭 发表于 2021-7-17 12:19
有hc32f460 kcta的led源程序吗
<p>另一个帖子有,不过是PBTE的程序</p>
1nnocent 发表于 2021-7-17 18:35
另一个帖子有,不过是PBTE的程序
<p>大佬,咱们这个hc32f460 IO口是怎么使能的,刚开始接触这,实在是没搞明白、、、、</p>
<p> </p>
逃之之夭夭 发表于 2021-7-28 10:07
大佬,咱们这个hc32f460 IO口是怎么使能的,刚开始接触这,实在是没搞明白、、、、
  ...
<p>程序下载下来看看就懂了</p>
<p> </p>
页:
[1]