如何在c中对开关进行反跳?

ykejflvf  于 2022-12-03  发布在  其他
关注(0)|答案(1)|浏览(134)

我在MPLAB X IDE的C语言中遇到了一个开关的问题。我想做一个像手电筒一样的东西,一个开关。有人能帮助我吗?抱歉我的英语不好。

#define _XTAL_FREQ 4000000
#include <xc.h>
#include "pic16f1455.h"

/************************************************************
* Interrups/debouncing
*************************************************************/
/** Configuration ********************************************************/
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.


// CONFIG1
#pragma config FOSC = INTOSC    // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = OFF       // Internal/External Switchover Mode (Internal/External Switchover Mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)


// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config CPUDIV = NOCLKDIV// CPU System Clock Selection Bit (NO CPU system divide)
#pragma config USBLSCLK = 48MHz // USB Low SPeed Clock Selection bit (System clock expects 48 MHz, FS/LS USB CLKENs divide-by is set to 8.)
#pragma config PLLMULT = 3x     // PLL Multipler Selection Bit (3x Output Frequency Selected)
#pragma config PLLEN = ENABLED  // PLL Enable Bit (3x or 4x PLL Enabled)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LPBOR = OFF      // Low-Power Brown Out Reset (Low-Power BOR is disabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)


char statusFlag = 0;


void main(void)
{
    TRISA = 0b00110000;            
    TRISC = 0b00000000;
    ANSELA = 0b00000000;
    ANSELC = 0b00000000;
    IOCAN = 0b00100000;
    INTCONbits.GIE = 1;           
    INTCONbits.IOCIE = 1;  

    
   PORTCbits.RC3 = 0;
    
    while(1)
    {
        if(statusFlag == 1)
        {
            if(PORTCbits.RC3 == 0)
            {
                PORTCbits.RC3 = 1;
            }
            else
            {
                PORTCbits.RC3 = 0;
            }
            statusFlag = 0;
        }
    }
}

void __interrupt() ISR(void)
{
    if(INTCONbits.IOCIF == 1)                       
    {
        INTCONbits.IOCIF = 0;
        if(IOCAF5 == 1)
        {
            IOCAF5 = 0;
            statusFlag = 1;
        }

    }
}

我尝试了它与一个队列过程,我期望它的工作和debounce开关完全,但它只工作有时,我想要的东西,每次工作。

ut6juiuv

ut6juiuv1#

对于最简单的去抖动形式,则创建一个周期性定时器中断,大约每5ms触发一次。在该ISR内部,您需要类似以下的内容:

void timer_isr (void)
{
  uint8_t tmp;
  static uint8_t prev;
  tmp = gpio_port;

  if(tmp == prev)
  {
    debounced = tmp;
  }
  prev = tmp;
}

其中,debounced在文件范围内声明为static volatile uint8_t debounced=0;。这都是在开关按下时处于高电平有效= 1的假设下进行的。

相关问题