找回密码
立即注册
搜索
热搜: 活动 交友 discuz
发新帖

143

积分

0

好友

12

主题
发表于 2023-9-24 21:56:42 | 查看: 429| 回复: 2 IP:中国广东省 中国电信
/******************************************************************************
* Copyright (C) 2016, Xiaohua Semiconductor Co.,Ltd All rights reserved.
*
* This software is owned and published by:
* Xiaohua Semiconductor Co.,Ltd ("XHSC").
*
* 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 XHSC
* components. This software is licensed by XHSC to be adapted only
* for use in systems utilizing XHSC components. XHSC shall not be
* responsible for misuse or illegal use of this software for devices not
* supported herein. XHSC is providing this software "AS IS" and will
* not be responsible for issues arising from incorrect user implementation
* of the software.
*
* Disclaimer:
* XHSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
* REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING 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.
* XHSC 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.
*/
/******************************************************************************/
/** \file main.c
**
** A detailed description is available at
** @link Sample Group Some description @endlink
**
**   - 2016-02-16  1.0  XYZ First version for Device Driver Library of Module.
**
******************************************************************************/

/******************************************************************************
* Include files
******************************************************************************/
#include "gpio.h"

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

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

/******************************************************************************
* Local type definitions ('typedef')
******************************************************************************/
typedef struct {
    uint8_t u8Port;
    uint8_t u8Pin;
}stc_gpio_list_t;
/******************************************************************************
* Local function prototypes ('static')
******************************************************************************/

/******************************************************************************
* Local variable definitions ('static')                                      *
******************************************************************************/
const stc_gpio_list_t gGpiolist[] =
{
    /*{ 0, 0 }, */{ 0, 1 }, { 0, 2 }, /*{ 0, 3 },*/
    { 1, 4 }, { 1, 5 },
    { 2, 3 }, { 2, 4 }, { 2, 5 }, { 2, 6 },
    { 2, 7 },
    { 3, 1 },
    { 3, 2 }, { 3, 3 }, { 3, 4 }, { 3, 5 }, { 3, 6 },


};


/******************************************************************************
* Local pre-processor symbols/macros ('#define')
******************************************************************************/
#define TEST_PORT       (1)
#define TEST_PIN        (5)

#define SK_SW2_INIT()       Gpio_InitIO(3, 3, GpioDirIn)
#define SK_SW2_GET()        Gpio_GetIO(3,3)

#define NextStep()          { while (TRUE == Gpio_GetIO(3,3));\
                                delay1ms(200);\
                            }

#define SK_LED_INIT()       Gpio_InitIO(1, 5, GpioDirOut)
#define SK_LED_SET(x)       Gpio_SetIO(1,5,(x))

/*****************************************************************************
* Function implementation - global ('extern') and local ('static')
******************************************************************************/
/**
* \brief   Gpio_IRQHandler
*          any interrput blinking a led.
*  
* \param   [in]  u8Param            port index
* \retval  void          
*/


void Gpio_IRQHandler(uint8_t u8Param)
{
    *((uint32_t *)((uint32_t)&M0P_GPIO->P3ICLR + u8Param * 0x40)) = 0;

    SK_LED_SET(0);
    delay1ms(500);
    SK_LED_SET(1);
    delay1ms(500);

}


/**
******************************************************************************
** \brief  Main function of project
**
** \return uint32_t return value, if needed
**
** This sample
**
******************************************************************************/

int32_t main(void)
{   
    //SW2 控制程序继续运行
    SK_SW2_INIT();
    NextStep();

    //GPIO初始化,配置P15为输出,外接LED
    Gpio_InitIO(TEST_PORT, TEST_PIN, GpioDirOut);
    Gpio_SetIO(TEST_PORT, TEST_PIN, TRUE);

    //初始化外部IO P33
    Gpio_InitIOExt(3, 3, GpioDirIn, TRUE, FALSE, FALSE, 0);

    //开启GPIO中断
    Gpio_ClearIrq(3, 3);
    //Gpio_EnableIrq(3, 3, GpioIrqRising);
          Gpio_EnableIrq(3, 3, GpioIrqFalling);
    EnableNvic(PORT3_IRQn, DDL_IRQ_LEVEL_DEFAULT, TRUE);

    while (1);
}

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

这个是官方GPIO中断例程,按键触发中断后,会一直不断的进入中断,不知道怎样改,使每次触发中断后,只产生一次中断。程序中设置的是下降沿中断,照理应该只产生一次中断。

发表于 2023-9-24 21:57:26 IP:中国广东省 中国电信
可以在中断处理函数中加入一个变量来判断是否已经进入过中断,如果已经进入过中断,则不再执行中断处理函数代码,等待下一次触发中断再执行中断处理函数。例如可以在中断处理函数中添加一个 static 变量,初始值为 0,在进入中断处理函数时将变量设置为 1,再次进入中断处理函数时,判断变量是否为 1,如果是则直接返回,不执行中断处理函数代码,如果不是则执行中断处理函数代码,并将变量设置为 1。
发表于 2023-9-24 22:05:15 IP:中国广东省 中国电信

我觉得你的问题可能是由于按键的抖动导致的。按键的抖动是指按键在按下或松开的过程中,会产生一些不稳定的信号,这些信号可能被误认为是有效的中断触发条件。为了避免这种情况,你需要对按键的信号进行消抖处理,也就是在检测到中断后,延时一段时间,再次确认信号是否稳定,如果是,则执行中断服务程序,否则忽略该中断。

有两种方法可以实现消抖处理,一种是硬件消抖,一种是软件消抖。

硬件消抖是在按键的电路上加入一些元件,如电阻、电容、施密特触发器等,来过滤掉不稳定的信号。

软件消抖是在中断服务程序中加入一些代码,来判断信号是否稳定。

*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

手机版|小黑屋|国产电子社区 ( 沪ICP备2023018578号-1|

苏公网安备 32011102010465号


)|网站地图

GMT+8, 2024-4-19 20:01 , Processed in 0.064114 second(s), 24 queries , MemCached On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表