//********************************************************************** /*   「簡易ストップウォッチ」  <DeviceFlags>   _IESO_OFF   _FNOSC_PRI   _FCKSM_CSDCMD   _OSCIOFNC_OFF   _POSCMOD_EC   _JTAGEN_OFF   _GCP_OFF   _GWRP_OFF   _BKBUG_OFF   _COE_OFF   _ICS_PGx1   _FWDTEN_OFF */ //********************************************************************** #define LED PORTB.F6 #define START_SW PORTB.F8 #define STOP_SW PORTB.F7 //********************************************************************** static unsigned long cnt; static unsigned char flag; void Timer1Int() org 0x1A { IFS0bits.T1IF = 0; if (flag == 1) { cnt++; } } void External0Int() org 0x14 { IFS0bits.INT0IF = 0; flag = 2; // count stop! LED = 1; // LED off! } //********************************************************************** void main() { char buf[20]; // ADPCFG = 0xFFFF; // 全ピンをデジタルで使用する。 TRISA = 0b11111111; TRISB = 0b1111111110111111; CNPU1bits.CN7PUE = 1; // pull-up CNPU1bits.CN8PUE = 1; // pull-up // timer1(16bits) 1msecのタイマー割り込みに使う。 IPC0bits.T1IP0 = 1; IFS0bits.T1IF = 0; IEC0bits.T1IE = 1; T1CONbits.TCKPS0 = 0; T1CONbits.TCKPS1 = 0; T1CON.F15 = 0; PR1 = 16000; // 0.001/(1/(32MHz/2)) TMR1 = 0; // 外部割込み(External Interrupt Input)をカウントの停止に使う。 IPC0bits.INT0IP0 =1; IFS0bits.INT0IF = 0; IEC0bits.INT0IE = 1; INTCON2bits.INT0EP = 1; // 立下りで割り込み // Lcd_Custom_Config(&PORTB, 9, 10, 11, 12, &PORTB, 15, 14, 13); Lcd_Custom_Cmd(LCD_CURSOR_OFF); Lcd_Custom_Out(1, 1, "StopWatch"); Delay_ms(500); Lcd_Custom_Cmd(LCD_CLEAR); // T1CON.F15 = 1; // TIMER1 start! LED = 1; // LED off! // flag = 0; cnt = 0; // while(1) { if ((flag != 1) && (START_SW == 0)) { cnt = 0; flag = 1; // count start! LED = 0; // LED on! Lcd_Custom_Out(1, 1, "Start!"); } if (flag == 2) { Lcd_Custom_Out(1, 1, "Stop! "); flag = 0; } // LongWordToStr(cnt, buf); Lcd_Custom_Out(2, 1, buf); Lcd_Custom_Out(2, 11, "msec"); } } //**********************************************************************