在MPLAB上删除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;
}
}
}我尝试了一个队列过程,我希望它能正常工作,完全取消开关,但它只是有时起作用,而且我想要的东西每次都能工作。
发布于 2022-11-08 08:42:19
对于最简单的卸载形式,然后创建一个循环计时器中断,每5ms左右触发一次。从内部来看,你需要这样的东西:
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。
https://stackoverflow.com/questions/74357807
复制相似问题