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

📄 f_13_3_tmr0_swmeas.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 "delay.h"#if defined(__18CXX)#include "timers.h"  // timers header file defined by MCC18#endif#define FOSCQ 29491200#define PRESCALE 128.0#define TMR0TIC 1.0/(FOSCQ/4.0)*PRESCALEunsigned int tmr0_tics, msec;double pulse_width_float;unsigned long pulse_width;volatile unsigned char capture_flag;#if defined(HI_TECH_C)void interrupt timer_isr(void)#endif#if defined(__18CXX)#pragma interrupt timer_isrvoid timer_isr(void)#endif{  if (INT0IF) {    if (!INTEDG0) {      //seen falling edge, start timer1      TMR0ON = 1; // turn on timer      INTEDG0 = 1;  // rising edge interrupt    } else {      TMR0ON = 0; // turn off timer#if defined(HI_TECH_C)      tmr0_tics = TMR0; // read TRM0 as a 16-bit value#endif#if defined(__18CXX)      {	   union Timers timer;     //Timers union defined in timers.h of MCC18 lib         timer.bt[0] = TMR0L;  // Copy Timer0 low byte into union         timer.bt[1] = TMR0H;  // Copy Timer0 high byte into union		 tmr0_tics = timer.lt;  // copy to 16-bit integer	  }#endif      INT0IE = 0; //disable RB0 Interrupt      capture_flag = 1;    }    INT0IF = 0;  }}      void main(void){  serial_init(95,1); // 19200 in HSPLL mode, crystal = 7.3728 MHz  // init timer0  T08BIT = 0; // 16-bit mode  T0CS = 0;   // internal clock  PSA = 0;   // use prescaler = 128  T0PS2 = 1;T0PS1 = 1;T0PS0 = 0; // prescaler = 128  TRISB0= 1; // RB0 is input  IPEN = 0; PEIE = 1;  GIE = 1;  // enable interrupts   pcrlf();printf("(Timer0 version) Ready for button mashing!");pcrlf();  while(1){    capture_flag = 0;    TMR0H = 0; // clear timer0, write low byte last    TMR0L = 0;    INTEDG0 = 0; // falling edge    INT0IF = 0;  INT0IE = 1; //RB0 Interrupt    while(!capture_flag); // wait for capture    // compute time in microseconds    pulse_width_float = TMR0TIC * tmr0_tics * 1.0e6;     msec = (unsigned int)(pulse_width_float/1000.0); //milliseconds    pulse_width = (long)pulse_width_float;    //printf ("Switch pressed, timer ticks: %u, pwidth: %lu (us)",    //	    tmr0_tics,pulse_width);   // use with full HITECH compiler or MCC18    printf ("Switch pressed, timer ticks: %u, pwidth: %u (ms)",    	    tmr0_tics,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  #endifvoid HighVector (void){    _asm goto timer_isr _endasm}#pragma code#endif

⌨️ 快捷键说明

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