⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swdetov.c

📁 * Use 10 MHz crystal frequency. * Use Timer0 for ten millisecond looptime. * Blink "Alive" LED e
💻 C
字号:

#include "config.h"
#include "serial.c"
#include "serio.c"
#include "i2cmsu.c"


// allows overflow of timer1 during capture


/* measures pulse width with overflow allowed on Timer 1 */

/* use Capture mode to measure the pulse width of a switch */



volatile unsigned int last_capture;
volatile unsigned int this_capture;
volatile unsigned long delta;  // this must be long
volatile unsigned char tmr1_ov;  // timer 1 overflow cnt
volatile unsigned char capture_flag;

#if defined(HI_TECH_C)
void interrupt timer_isr(void)
#endif
#if defined(__18CXX)
#pragma interrupt timer_isr
void timer_isr(void)
#endif
{
  if (TMR1IF) {
    tmr1_ov++;  // increment timer1 overflow
    TMR1IF = 0;
  }
  if (CCP1IF) {
    this_capture = CCPR1; // get 16-bit capture value
    if (!(CCP1CON & 0x01)) { // test LSbit of CCP1con
      //falling edge
      last_capture = this_capture;
      tmr1_ov = 0;  // clear timer 1 overflow count
      CCP1CON = 0x0; // turnoff before changing
      CCP1CON = 0x5; // capture every rising edge 
    } else {
      if (!tmr1_ov) { 
	// no overflow at all
	delta = this_capture - last_capture ;
      }
      else {
	delta = tmr1_ov - 1;
	delta = (delta << 16);
	last_capture = 0 - last_capture;  // time to overflow
	delta = delta + last_capture;
	delta = delta + this_capture;
      }
      /* disable timer1 interrupt */
      TMR1ON = 0; TMR1IE = 0; TMR1IF = 0;   
      capture_flag = 1;
    }
    CCP1IF = 0;   // 	CCP1IF = 0; clear capture interrupt flag
  }
}

#define FOSCQ 29491200
#define PRESCALE 2.0
#define TMR1TIC 1.0/(FOSCQ/4.0)*PRESCALE
double pulse_width_float;
unsigned long pulse_width;
unsigned int msec;
int *ptr;

void main(void){

  tmr1_ov = 0;
  serial_init(95,1); // 19200 in HSPLL mode, crystal = 7.3728 MHz 
  // initialize timer 1
  T1CKPS1 = 0;  T1CKPS0 = 1; // prescale by 2
  T1OSCEN = 0;  // disable the oscillator 
  T1SYNC = 0; TMR1CS = 0;  //use internal clock FOSC/4
  bitset(TRISC,2); // set CCP1 as input 
  // enable interrupts 
  IPEN = 0;  PEIE = 1;   GIE = 1;  
  ptr = (int *) &delta;  //for printf of long type
  pcrlf();printf("(Timer1 version) Ready for button mashing!");pcrlf();
  while(1) {
    // configure capture
    CCP1IE = 0;   // disable when changing modes
    CCP1CON = 0x0; // turnoff before changing
    CCP1CON = 0x4; // capture every falling edge 
    CCP1IF = 0; // clear CCP1IF interupt flag 
    CCP1IE = 1;   // enable capture interrupt 
    TMR1IF = 0;   // clear timer 1 interrupt flag
    TMR1IE = 1;   // allow timer 1 interrupts
    TMR1ON = 1;   // enable timer 1
    // wait for falling edge
    capture_flag = 0;
    while(!capture_flag);
    pulse_width_float = (delta * TMR1TIC)*1.0e6; //microseconds
    pulse_width = (long) pulse_width_float;
    msec = (unsigned int) (pulse_width_float/1000.0); //milliseconds
    //  printf ("Switch pressed, timer ticks:%lu , pwidth: %lu (us)", 
    //    delta, pulse_width);  // use with full compiler
    printf ("Switch pressed, timer ticks:0x%04x%04x, pwidth: %u (ms)", 
        *(ptr+1), *ptr,msec);  // use with demo compiler
    pcrlf();
  }
}

//for MCC18, place the interrupt vector goto
#if defined(__18CXX)
#if defined(HIGH_INTERRUPT)
#pragma code HighVector=HIGH_INTERRUPT
#else
#pragma code HighVector=0x0008  
#endif
void HighVector (void)
{
    _asm goto timer_isr _endasm
}
#pragma code
#endif

⌨️ 快捷键说明

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