//********************************************************************** /*   「防犯アラーム(振動検知)」 */ //********************************************************************** #define LED GPIO.F5 #define SW GPIO.F3 //********************************************************************** void Pwm_Change_DutyEx(unsigned int duty_ratio) { CCPR1L = duty_ratio >> 2; CCP1CON.DC1B0 = duty_ratio & 0b00000001; CCP1CON.DC1B1 = (duty_ratio & 0b00000010) >> 1; } //********************************************************************** unsigned long measurement(unsigned short channel) { static unsigned int cnt; static unsigned long ad; // ad = 0; for (cnt = 0; cnt < 10; cnt++) { ad += Adc_Read(channel); } return (ad / 10); } //********************************************************************** double offset_x, offset_y, offset_z; void initKXM52() { LED = 1; // offset_z = (double)measurement(0); // offset_y = (double)measurement(1); // offset_x = (double)measurement(3); // LED = 0; } //********************************************************************** void alarm() { static short cnt; // for (cnt = 0; cnt < 10; cnt++) { LED = 1; Delay_ms(100); Pwm_Start(); LED = 0; Delay_ms(100); Pwm_Stop(); } } //********************************************************************** void main() { static double ad_z, ad_y, ad_x; static int limit; // OSCCON = 0b01110000; // クロックを8Mhzに設定する。 CMCON0 = 0b00000111; // コンパレータは使用しない。 ANSEL = 0b00011011; // A/D変換を使用する。 TRISIO = 0b00011011; // Pwm_Init(1000); // 1kHz duty=50% Pwm_Change_DutyEx((PR2 * 4) / 2); Pwm_Stop(); //感度設定 limit = 100; if (SW == 0) { while (SW == 0) { LED = 1; Delay_ms(50); LED = 0; Delay_ms(50); } limit = 10; } // Delay_ms(1000); initKXM52(); // while (1) { ad_z = measurement(0); ad_y = measurement(1); ad_x = measurement(3); // if (labs(ad_z - offset_z) > limit) { alarm(); } if (labs(ad_y - offset_y) > limit) { alarm(); } if (labs(ad_x - offset_x) > limit) { alarm(); } // while (SW == 0) { initKXM52(); Delay_ms(50); } } } //**********************************************************************