首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何取消c中的开关?

如何取消c中的开关?
EN

Stack Overflow用户
提问于 2022-11-08 08:36:48
回答 1查看 102关注 0票数 0

在MPLAB上删除c中的一个开关时,我有问题。我想做一些像手电筒一样的东西,只要一个开关就可以打开和关闭。有人能帮我吗?对不起我的英语不好。

代码语言:javascript
运行
复制
#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;
        }

    }
}

我尝试了一个队列过程,我希望它能正常工作,完全取消开关,但它只是有时起作用,而且我想要的东西每次都能工作。

EN

回答 1

Stack Overflow用户

发布于 2022-11-08 08:42:19

对于最简单的卸载形式,然后创建一个循环计时器中断,每5ms左右触发一次。从内部来看,你需要这样的东西:

代码语言:javascript
运行
复制
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。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74357807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档