//********************************************************************** /*   「簡易パルス測定器」  ■測定モード   ・立上がり→立下り   ・立上がり→立下り→上がり   ・立下り→立上がり   ・立下り→立上がり→立下り  ■測定精度=0.1msec  ■測定時間=最大約59時間 */ //********************************************************************** #define LED PORTA.F2 #define SW_START PORTA.F3 #define SW1 PORTA.F4 #define SW2 PORTA.F5 #define INPUT PORTB.F0 //********************************************************************** static unsigned long cnt; void interrupt() { PIR1.CCP1IF = 0; // cnt++; LED = ~LED; } //********************************************************************** unsigned long measurement(short mode) { TMR1L = 0; TMR1H = 0; cnt = 0; // switch (mode) { case 0: //↑↓ while (INPUT == 1) //“0”になるまで待つ ; while (INPUT == 0) //立上がりをチェック ; T1CON.TMR1ON = 1; //カウント開始 while (INPUT == 1) //立下りをチェック ; T1CON.TMR1ON = 0; //カウント停止 break; case 1: //↑↓↑ while (INPUT == 1) //“0”になるまで待つ ; while (INPUT == 0) //立上がりをチェック ; T1CON.TMR1ON = 1; //カウント開始 while (INPUT == 1) //立下りをチェック ; while (INPUT == 0) //立上がりをチェック ; T1CON.TMR1ON = 0; //カウント停止 break; case 2: //↓↑ while (INPUT == 0) //“1”になるまで待つ ; while (INPUT == 1) //立下がりをチェック ; T1CON.TMR1ON = 1; //カウント開始 while (INPUT == 0) //立上りをチェック ; T1CON.TMR1ON = 0; //カウント停止 break; case 3: //↓↑↓ while (INPUT == 0) //“1”になるまで待つ ; while (INPUT == 1) //立下がりをチェック ; T1CON.TMR1ON = 1; //カウント開始 while (INPUT == 0) //立上がりをチェック ; while (INPUT == 1) //立下りをチェック ; T1CON.TMR1ON = 0; //カウント停止 break; } // return (cnt); } //********************************************************************** void SwitchONcheck() { while (Button(&PORTA, 3, 1, 0) == 0) ; while (Button(&PORTA, 3, 1, 1) == 0) ; } //********************************************************************** void main() { static char buf[16], mode; static unsigned long msec; // // OSCCON = 0b01110000; CMCON = 0b00000111; ANSEL = 0b00000000; TRISA = 0b11111011; TRISB = 0b00000001; // TIMER1の設定 PIE1.TMR1IE = 0; PIR1.TMR1IF = 0; T1CON.T1CKPS0 = 0; T1CON.T1CKPS1 = 1; T1CON.TMR1ON = 0; TMR1L = 0; TMR1H = 0; // CCPの設定 PIE1.CCP1IE = 1; PIR1.CCP1IF = 0; CCP1CON.CCP1M3 = 1; CCP1CON.CCP1M2 = 0; CCP1CON.CCP1M1 = 1; CCP1CON.CCP1M0 = 1; CCPR1L = 0x7D; // 0.1msec...(1÷20000000)*4*4*125 CCPR1H = 0x00; // //LCDの初期化 Lcd_Custom_Config(&PORTB, 7, 6, 5, 4, &PORTB, 3, 2, 1); Lcd_Custom_Cmd(LCD_CURSOR_OFF); Lcd_Custom_Cmd(LCD_CLEAR); Lcd_Custom_Out(1, 1, "PulseMeasurement"); Delay_ms(500); Lcd_Custom_Cmd(LCD_CLEAR); Lcd_Custom_Out(2, 13, "msec"); // 割り込みを許可する。 INTCON.PEIE = 1; INTCON.GIE = 1; // while (1) { if ((SW1 == 1) && (SW2 == 1)) { mode = 0; Lcd_Custom_Out(1, 11, "0->10 "); } if ((SW1 == 0) && (SW2 == 1)) { mode = 1; Lcd_Custom_Out(1, 11, "0->101"); } if ((SW1 == 1) && (SW2 == 0)) { mode = 2; Lcd_Custom_Out(1, 11, "1->01 "); } if ((SW1 == 0) && (SW2 == 0)) { mode = 3; Lcd_Custom_Out(1, 11, "1->010"); } LED = 0; Lcd_Custom_Out(1, 1, " "); if (SW_START == 1) { continue; } while (Button(&PORTA, 3, 1, 1) == 0) ; // Lcd_Custom_Out(1, 1, "start"); Lcd_Custom_Out(2, 1, " "); msec = measurement(mode); LongToStr(msec, buf); buf[12] = 0x00; buf[11] = buf[10]; buf[10] = '.'; Lcd_Custom_Out(2, 1, buf); } } //*********************************************************************