//********************************************************************** /*   <電圧低下検出ユニット> */ //********************************************************************** #define SW GPIO.F3 //********************************************************************** void Pwm_Change_DutyEx(unsigned int duty_ratio) { CCPR1L = duty_ratio >> 2; CCP1CON.F6 = duty_ratio & 0b00000001; CCP1CON.F7 = (duty_ratio & 0b00000010) >> 1; } //********************************************************************** void main() { static double ad; static unsigned int threshold; static short cnt, mode; // TRISIO = 0b00001011; OSCCON = 0b01010000; // クロックは2Mhz CMCON0 = 0b00000111; // コンパレータは使用しない。 ANSEL = 0b00000001; // A/D変換を使用する。 ADCON0.VCFG = 1; // GPIO.F4 = 0; // Pwm_Init(1200); Pwm_Change_DutyEx((PR2 * 4) / 2); Pwm_Stop(); // 保存されている閾値の取り込み threshold = Eeprom_Read(1); threshold = threshold << 8; threshold = threshold | Eeprom_Read(0); // mode = 0; GPIO.F4 = 0; // for (cnt = 0; cnt < 5; cnt++) { Pwm_Start(); Delay_ms(100); Pwm_Stop(); Delay_ms(500); } // while (1) { //電圧を測定する。 ad = 0.0; for (cnt = 0; cnt < 100; cnt++) { ad += Adc_Read(0); } ad = (ad / 100) * 11 * 2.44140625; //閾値との比較を行う。 switch (mode) { case 0: if (ad < threshold) { mode = 1; GPIO.F4 = 1; } break; case 1: if (ad > (threshold + 100.0)) { mode = 0; GPIO.F4 = 0; } break; } //電圧が低下したらブザーを鳴らす。 if (mode == 1) { Pwm_Start(); Delay_ms(100); Pwm_Stop(); Delay_ms(500); } // if (SW == 0) { threshold = ad; // 閾値の保存 Eeprom_Write(0, (threshold & 0xFF)); Delay_ms(20); Eeprom_Write(1, ((threshold >> 8) & 0xFF)); Delay_ms(20); } } } //**********************************************************************