//----------------------------------------------------------------------------- // Note // ドアの侵入検知 // PICの周波数を32Mhzに設定 //----------------------------------------------------------------------------- #include #include #include #define _XTAL_FREQ 32000000 // コンフィグレーションBIT1 設定 #pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin) #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled) #pragma config PWRTE = ON // Power-up Timer Enable (PWRT enabled) #pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is MCLR) #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) #pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled) #pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset disabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) #pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled) // コンフィグレーションBIT2 設定 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config PLLEN = ON // PLL Enable (4x PLL enabled) #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) #pragma config BORV = HI // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), high trip point selected.) #pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming) // 点灯IOの定義 #define LED_GREEN RA0 #define LED_RED RA1 #define LED_YELLOW RA2 #define CHECK_SW RA5 #define DOOR_SW RA4 // IOの状態を定義 #define SW_ON 0 #define SW_OFF 1 #define LED_ON 1 #define LED_OFF 0 int main(int argc, char** argv) { short step = 0; short now_sw = 0; short old_sw = 0; short count = 0; OSCCON = 0b01110000 ; // 内部クロック8MHz OPTION_REG = 0b00000000 ;// 7bit目が0でプルアップ抵抗が有効 ANSELA = 0b00000000 ; // すべてデジタルI/Oに割当てる TRISA = 0b00111000 ; // RA3,4,5を入力とする WPUA = 0b00110000 ; // RA4,5をプルアップ抵抗設定 PORTA = 0b00000000 ; // ピン状態初期化 for(;;) { now_sw = DOOR_SW; switch(step) { case 0: // 電源ON後 一定時間無効 LED_GREEN = LED_ON; __delay_ms(5000); old_sw = now_sw; step = 1; break; case 1: // ドア開のチェック if( (now_sw == SW_OFF) && (now_sw != old_sw) ) { __delay_ms(1000); // チャタリング防止 count++; } break; } // 状態表示 if( (step == 1) && (CHECK_SW == SW_ON) ) { if(count<2) { LED_GREEN = LED_ON; LED_RED = LED_OFF; } else { LED_GREEN = LED_OFF; LED_RED = LED_ON; } } else { LED_GREEN = LED_OFF; LED_RED = LED_OFF; } // ドアのIO状態 if( DOOR_SW == SW_ON ) LED_YELLOW = LED_ON; else LED_YELLOW = LED_OFF; old_sw = now_sw; } return (EXIT_SUCCESS); }