GD32E230按键软件消抖程序
我这里使用的消抖方式是金沙滩工作室宋老师所讲的方法,用一个定时器,定时 2ms 进一次中断,在中断扫描一次按键状态并且存储起来,连续扫描 8 次后,看看这连续 8 次的按键状态是否是一致的。8 次按键的时间是 16ms,这 16ms 内如果按键状态一直保持一致,那就可以确定现在按键处于稳定的阶段,而非处于抖动的阶段。
本程序所用的按键原理图为
main.c
/*!\file main.c\brief TIMER2 oc timebase demo for gd32e23x\version 2019-02-19, V1.0.0, firmware for GD32E23x\version 2020-12-12, V1.1.0, firmware for GD32E23x
*//*Copyright (c) 2020, GigaDevice Semiconductor Inc.Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/#include "gd32e23x.h"
#include <stdio.h>
#include "gd32e230c_eval.h"/* configure the TIMER peripheral */
void timer_config(void);
/* configure the TIMER interrupt */
void nvic_config(void);uint8_t keysta = 0xff,backup = 0xff;
uint8_t cnt = 0;/**\brief configure the TIMER interrupt\param[in] none\param[out] none\retval none*/
void nvic_config(void)
{nvic_irq_enable(TIMER0_BRK_UP_TRG_COM_IRQn, 0);
}/**\brief configure the TIMER peripheral\param[in] none\param[out] none\retval none*/
void timer_config(void)
{/* ----------------------------------------------------------------------------TIMER2 Configuration: TIMER2CLK = SystemCoreClock/(7200*9) = 1.111KHz,the period is 1s(1111/555555 = 2ms).1.0986khz /54930 5ms(period*prescaler)/systemcoreclock = you want to set time---------------------------------------------------------------------------- */timer_oc_parameter_struct timer_ocinitpara;timer_parameter_struct timer_initpara;/* enable the peripherals clock */rcu_periph_clock_enable(RCU_TIMER0);/* deinit a TIMER */timer_deinit(TIMER0);/* initialize TIMER init parameter struct */timer_struct_para_init(&timer_initpara);/* TIMER2 configuration */timer_initpara.prescaler = 7199;timer_initpara.alignedmode = TIMER_COUNTER_EDGE;timer_initpara.counterdirection = TIMER_COUNTER_UP;timer_initpara.period = 19;timer_initpara.clockdivision = TIMER_CKDIV_DIV1;timer_init(TIMER0, &timer_initpara);/* clear channel 0 interrupt bit */timer_interrupt_flag_clear(TIMER0, TIMER_INT_FLAG_UP);/* enable the TIMER interrupt */timer_interrupt_enable(TIMER0, TIMER_INT_UP);/* enable a TIMER */timer_enable(TIMER0);
}void KeyScan()
{if (keysta != backup) {if (backup == 0x00) {cnt++;if(cnt%2){gd_eval_led_on(LED1);}else{gd_eval_led_off(LED1);}}backup = keysta; }
}
/*!\brief main function\param[in] none\param[out] none\retval none
*/
int main(void)
{/* configure led GPIO */gd_eval_led_init(LED1);/* configure the TIMER peripheral */timer_config();/* configure the TIMER2 interrupt */nvic_config();while (1){KeyScan();}
}
gd32e23x_it.c
/*!\file gd32e23x_it.c\brief interrupt service routines\version 2019-02-19, V1.0.0, firmware for GD32E23x\version 2020-12-12, V1.1.0, firmware for GD32E23x
*//*Copyright (c) 2020, GigaDevice Semiconductor Inc.Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/#include "gd32e23x_it.h"
#include "gd32e230c_eval.h"extern uint8_t keysta ;
uint32_t cnt11 = 0;/*!\brief this function handles NMI exception\param[in] none\param[out] none\retval none
*/
void NMI_Handler(void)
{}/*!\brief this function handles HardFault exception\param[in] none\param[out] none\retval none
*/
void HardFault_Handler(void)
{/* if Hard Fault exception occurs, go to infinite loop */while (1){}
}/*!\brief this function handles MemManage exception\param[in] none\param[out] none\retval none
*/
void MemManage_Handler(void)
{/* if Memory Manage exception occurs, go to infinite loop */while (1){}
}/*!\brief this function handles BusFault exception\param[in] none\param[out] none\retval none
*/
void BusFault_Handler(void)
{/* if Bus Fault exception occurs, go to infinite loop */while (1){}
}/*!\brief this function handles UsageFault exception\param[in] none\param[out] none\retval none
*/
void UsageFault_Handler(void)
{/* if Usage Fault exception occurs, go to infinite loop */while (1){}
}/*!\brief this function handles SVC exception\param[in] none\param[out] none\retval none
*/
void SVC_Handler(void)
{}/*!\brief this function handles DebugMon exception\param[in] none\param[out] none\retval none
*/
void DebugMon_Handler(void)
{}/*!\brief this function handles PendSV exception\param[in] none\param[out] none\retval none
*/
void PendSV_Handler(void)
{}/*!\brief this function handles SysTick exception\param[in] none\param[out] none\retval none
*/
void SysTick_Handler(void)
{}/*** @brief This function handles TIMER0 interrupt request.* @param None* @retval None*/
void TIMER0_BRK_UP_TRG_COM_IRQHandler(void)
{static unsigned char keybuf = 0xff;if(SET == timer_interrupt_flag_get(TIMER0, TIMER_INT_FLAG_UP)){/* clear channel 0 interrupt bit */timer_interrupt_flag_clear(TIMER0, TIMER_INT_FLAG_UP);
// cnt11++;
// if(cnt11%2)
// {// gd_eval_led_on(LED1);
// }
// else
// {// gd_eval_led_off(LED1);
// }/* toggle selected led */if(RESET == gpio_input_bit_get(GPIOB,GPIO_PIN_6)){keybuf = (keybuf<<1) | 0x01;}else{keybuf = (keybuf<<1) | 0x00;}if(keybuf == 0x00){keysta = 0x00;}else if (keybuf == 0xff){keysta = 0xFF;}else{}}
}
工程如下
https://download.csdn.net/download/wyc3251/85093508
GD32E230按键软件消抖程序相关推荐
- FPGA实现按键检测消抖程序
这个是黑金的板子提供的原版按键消抖程序 `timescale 1ns / 1ps // // Company: // Engineer: // // Create Date: 2020/02/29 1 ...
- 单片机矩阵消抖延时c语言,单片机矩阵按键定时器消抖程序源码
芯片是采用的stc89c51单片机. 下面是矩阵键盘的电路图,矩阵键盘是接在p2口的. 下面是单片机部分的图,数码管显示等完整的原理图可以从http://www.51hei.com/f/ks51.pd ...
- 单片机消抖c语言程序,基于单片机定时器软件消抖C51程序研究
向兵 杨述凯 摘 要:随着电子技术的发展,单片机的应用在教学及科技开发中越来越普遍,而针对单片机的开发环境而言,大多采用C51语法进行设计.按键的使用在大多数硬件设计里相当频繁,而按键的干扰处理可以使 ...
- 单片机入门资料,按键消抖方式,按键怎么消抖
1.什么是按键消我们先来看一下按键按下去的波形图 1.按键消抖原理 我们可以看到当按键按下的那一时刻和松开的时候有类似于锯齿的形状那就是按键抖动,这个抖动不是我们人为能控制得了的,所以我们只能对进行 ...
- [51单片机]按键部分(软件消抖)
独立按键: 电路图: 独立按键需要考虑按键消抖的问题.按消键抖通常的按键所用开关为机械弹性开关,当机械触点断开.闭合时,由于机械触点的弹性作用,一个按键开关在闭合时不会马上稳定地接通,在断开时也不会一 ...
- 按键状态机消抖(上)
按键为什么要消抖呢? 按键再按下时,由于弹片时金属的,会产生振动导致当时IO口读取电平状态错误,其次如果将手指或其他导电物体接触到键盘底部的引脚也会导致读取电平状态错误.(我之前试过在while循环中 ...
- stm32 工业按键检测_STM32单片机按键消抖和FPGA按键消抖大全
写在前面: 物联网STM32入门 - 直播课程 - 创客学院www.makeru.com.cn 按键去抖:由上图可以看出理想波形与实际波形之间是有区别的,实际波形在按下和释放的瞬间都有抖动的现象,抖 ...
- stm32硬件消抖_STM32单片机按键消抖和FPGA按键消抖大全
原标题:STM32单片机按键消抖和FPGA按键消抖大全 写在前面: 按键去抖:由上图可以看出理想波形与实际波形之间是有区别的,实际波形在按下和释放的瞬间都有抖动的现象,抖动时间的长短和按键的机械特性有 ...
- 一种相对高效的按键消抖方法
按键软件消抖自我接触单片机开始就已经存在这个问题了,网上的办法无非是延时消抖和定时轮询.对于写裸机的我来说这两种方法都不可避免的会有资源浪费掉,今天突然有了灵感,想到了一种相对高效的办法来解决消抖问题 ...
- 按键消抖及原理(硬件和软件方法详解)
在设计单片机按键输入的时候,进行按键消抖是防止按键输入被CPU误读多次的必要手段. 一.按键抖动 按键接法 抖动时间的长短由按键的机械特性决定,一般为5ms-10ms.这是一个很重要的时间参数,在很多 ...
最新文章
- 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)
- 2016年 CodePen 最热门的前端代码 Top 100
- mogilefsd同步速度调优
- PTA浙大版python程序设计题目集--第2章-4 特殊a串数列求和 (20 分)
- SAP UI5 应用开发教程之六十 - SAP UI5 地图控件的一些高级用法试读版
- 用DDA Convolution和Perlin Noise来模拟水粉画笔触
- android 全局 窗口,miui12全局自由窗口app
- Magento去除前台URL中显示的index.php
- centos8平台用NetworkManager/nmcli管理网络
- windows安装多个maven_全网最容易理解的Maven安装、配置、集成演示
- c语言编写 程序 闰年,C语言计算闰年程序
- python写的ROS激光雷达扇形滤波
- C++报错 invalid operands to binary expression
- 快速“美女找茬”(辅助工具)
- 区块链与大数据,打造智能经济(读书笔记)——井底望天
- python3模拟登录_python3模拟登录有哪些情况
- Scrapy 出现DEBUG:Filtered duplicate request
- 国内云桌面架构有哪些?为什么VDI能成为主流
- 零信任网络ZTNA及SDP概念理解
- 解决RuntimeError: stack expects a non-empty Tensorlist问题