我在 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;
}
}
}
我用队列程序尝试了它,我希望它能够工作并完全消除开关抖动,但它只是有时有效,我想要每次都有效的东西。
对于最简单的去抖形式,然后创建一个循环定时器中断,每 5 毫秒左右触发一次。从 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 的假设下进行的。
C 中消除按钮抖动的一个简单方法是等待一定的延迟,然后检查按钮是否仍然被按下。延迟越短,响应越快,但可靠性越差。尝试不同的延迟长度,看看哪种最适合您和您的手电筒。像这样的东西:
void __interrupt() ISR(void)
{
if(INTCONbits.IOCIF == 1)
{
__delay_ms(100); // Wait for button to finish bouncing
if(PORTAbits.RA5 == 0) // Check if button is still pressed
{
if(statusFlag == 1)
{
statusFlag = 0;
}
else
{
statusFlag = 1;
}
}
IOCAbits.IOCAF5 = 0; // Reset interrupt bits
INTCONbits.IOCIF = 0; // Reset interrupt bits
}
}