Source code MPLAB X IDE & XC8 compiler


/*
*  PIR sensor
*  12F675 PIC
* Created on August 12, 2020, 11:43 AM
*/

#pragma config FOSC = INTRCIO   // Oscillator Selection bits (INTOSC oscillator: I/O function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF       // Power-Up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF    // GP3/MCLR pin function select (GP3/MCLR pin function is digital I/O, MCLR internally tied to VDD)
#pragma config BOREN = OFF      // Brown-out Detect Enable bit (BOD disabled)
#pragma config CP = OFF         // Code Protection bit (Program Memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)

#include <xc.h>
#define _XTAL_FREQ 4000000

void delay(int sec) {
    for (int i= 0; i < sec; i++) {
        __delay_ms(1000);
    }
}

void main(void) {
   
    
    TRISIO = 0b00101000; // all output except GP3, and GP5
    ANSEL =  0b00000000;  // Set ports as digital I/O, not analog input
    CMCON =  0b00000111;  //comparators CM2:CM0 off
    VRCON = 0x00;   // Shut off the Voltage Reference
    ADCON0 = 0x00;  // Shut off the A/D Converter
    GPIO  =  0b00000001;   // Make all pins 0 (zero) except GP0 = warm-up LED
    
    delay(180);  // delay 3 minutes, no less
    GP0 = 0;  // warm-up led off 
    while (1) {            // forever
        if (GP5 == 1) {   // motion detected          
            GP2 = 1;    // relay high;  
            __delay_ms(400);  // 400 milliseconds
            GP2 = 0;
            GP0 = 1;
            delay(30); // delay 60 seconds
            GP0 = 0;
        }            
    } // while()
}  // main()