#include "18F2525.h" #fuses HS,NOWDT,NOPROTECT,NOLVP,INTRC #use delay(clock=8000000) #include "M:\PIC\Photogate timer with LCD\flex_lcd_photogate_final.c" //========================== int1 button, timer_en; //button used for ready button polling, timer_en used for ext interrupt first run, second run int1 donebit = 0; //donebit used to end mid-sampling loop long count, time, rem, divtest; //count is count of timer overflows, time is ms, rem is us, divtest is for adjustment #int_timer0 void timer_isr() { count++; // waste = 0; set_timer0(108); } #int_ext void ext_isr() { timer_en = !timer_en; if(timer_en == 0) { enable_interrupts(int_timer0); ext_int_edge(H_TO_L); donebit = 0; } else if(timer_en == 1) { disable_interrupts(int_timer0); donebit = 1; } } readyprep() { count = 0; //reset count timer_en = 1; lcd_putc("\fReady\b"); // LCD ready indicator ext_int_edge(L_TO_H); enable_interrupts(int_ext); enable_interrupts(global); return 0; } math() { time = (count / 10); //convert to milliseconds rem = count % 10; //modulus for microseconds rem *= 50; // convert to microseconds /* divtest = time % 2; if(rem == 400, divtest == 1) { time = time + 1; //possibly necessary compensation value rem = rem - 400; //possibly necessary compensation value } if(rem == 50) { rem = rem - 50; } */ return 0; } countroutine() { readyprep(); //prepares screen and vars while(donebit == 0) //mid-sample waiting area { delay_us(3); } disable_interrupts(global); disable_interrupts(int_timer0); math(); //computes count into ms, us /* if(count != 0) { readybit = 1; } */ donebit = 0; printf(LCD_putc,"\f%lu.%03lu ms \b", time, rem); //%lu for long ints delay_ms(100); return 0; } void main() { setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1|RTCC_8_BIT); //setup timer disable_interrupts(int_timer0); disable_interrupts(global); lcd_init(); // Always call this first. delay_ms(100); lcd_putc("\fFilm Shutter\nSpeed Timer\b"); // Startup message //**** Main While Loop********** while(1) { button = input(pin_A0); if(button == 0) { countroutine(); } // go to countroutine if ready button was pressed if(button == 1) { delay_us(3); } //maintains value until ready button pressed } }