f_10_24_sw_debounce.c

来自「* Use 10 MHz crystal frequency. * Use 」· C语言 代码 · 共 76 行

C
76
字号
#include "config.h"#include "delay.h"#include "serial.c"#include "serio.c"// switch debounce with timer2#define DEBOUNCE  5volatile unsigned char button, button_debounce;#if defined(HI_TECH_C)void interrupt pic_isr(void)#endif#if defined(__18CXX)#pragma interrupt pic_isrvoid pic_isr(void)#endif{  if (INT2IF && INT2IE) {    // pushbutton detected    // disable interrupt, set semaphore    INT2IE = 0;  button = 1;  button_debounce = 0;  }  if (TMR2IF) { // debouncing timer    TMR2IF = 0;    if (!INT2IE) {      if (RB2) {        if (button_debounce != DEBOUNCE) button_debounce++;        } else  button_debounce=0;       if (button_debounce == DEBOUNCE && !button){	//button idle high ,re-enable interrupt	INT2IF=0; INT2IE=1;      }    }  }}void main(void){  serial_init(95,1); // 19200 in HSPLL mode, crystal = 7.3728 MHz  button = 0; button_debounce = 0;  // configure INT2 for falling edge interrupt input  TRISB2 = 1;  INT2IF = 0; INTEDG2 = 0;  INT2IE = 1;  RBPU = 0; // enable weak pullup on port B  // configure timer 2  // post scale of 11, prescale 16, PR=250, FOSC=29.4912 MHz   // gives interrupt interval of ~ 6 ms  TOUTPS3 = 1; TOUTPS2 = 0; TOUTPS1 = 1; TOUTPS0 = 0;  T2CKPS1 = 1;  PR2 = 250;    // enable TMR2 interrupt  IPEN = 0;  TMR2IF = 0; TMR2IE = 1;   PEIE = 1;  GIE = 1;  TMR2ON = 1 ;  pcrlf();  printf("Pushbutton with Timer2 Debounce");  pcrlf();  while(1) {    if (button) {      button=0; // consume this semaphore      printf("Push Button activated!"); pcrlf();    }  }//end while}//end main//for MCC18, place the interrupt vector goto#if defined(__18CXX)#if defined(HIGH_INTERRUPT)#pragma code HighVector=HIGH_INTERRUPT#else#pragma code HighVector=0x0008  #endifvoid HighVector (void){    _asm goto pic_isr _endasm}#pragma code#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?