//********************************************************************** void main() { unsigned int ad, threshold; unsigned char cnt; OSCCON = 0b01110000; // クロックは8Mhz CMCON0 = 0b00000111; // コンパレータは使用しない。 ANSEL = 0b00000010; // AN1を使用する。 TRISIO = 0b00000011; GPIO = 0b00000000; // OPTION_REG = 0b10000000; // WPU.F1 = 1; // while (1) { if (GPIO.F0 == 0) { TRISIO.F2 = 1; Delay_ms(50); threshold = 0; for (cnt = 0; cnt < 50; cnt++) { Delay_ms(1); threshold += Adc_Read(1); } threshold = threshold / 50; TRISIO.F2 = 0; continue; } // TRISIO.F2 = 1; Delay_ms(50); ad = 0; for (cnt = 0; cnt < 50; cnt++) { Delay_ms(1); ad += Adc_Read(1); } ad = ad / 50; TRISIO.F2 = 0; // if (ad <= threshold) { GPIO.F2 = 1; } else { GPIO.F2 = 0; } Delay_ms(1000); } } //**********************************************************************