// CONFIG
//#pragma config FOSC = INTRCCLK // Oscillator Selection bits (INTOSC oscillator: CLKOUT function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN)
#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)
// Definitions 4MHZ internal
#define _XTAL_FREQ 4000000
#define RELAY GPIObits.GP4
#define SW GPIObits.GP5
#include <xc.h>
volatile uint8_t flag = 0;
void __interrupt() isr(void){
if (INTCONbits.INTF == 1) {
INTCONbits.INTF = 0;
//RELAY = 1;
flag = 1;
}
// GP1 is internally pulled up, if pressed it will be zero
if (INTCONbits.GPIF == 1 && GPIObits.GP1 == 0) {
INTCONbits.GPIF = 0;
flag = 0;
//SW = !SW;
}
}
void wait(uint8_t w);
void main(void) {
// OSCCAL = 0b10000000;
// Comparator register
CMCON = 0b00000111; // disable comparator
// Interrupt config reg.
// GIE,
// PEIE Peripheral int,
// T0IE,
// INTE (PORT2),
// GPIE (Change Port),
// T0IF,
// INTF (External Int Flag),
// GPIG (Port change flag)
INTCON = 0b11011000;
TRISIObits.TRISIO0 = 1; //relay reset
TRISIObits.TRISIO1 = 1; // extra
TRISIObits.TRISIO2 = 1; // emergency stop
//TRISIObits.TRISIO3 = 1; // MCLRE
TRISIObits.TRISIO4 = 0; // Relay
TRISIObits.TRISIO5 = 0; //
//GPIO = 0;
// internal weak pull ups
WPU = 0b00000111;
// IOC Interrupt On Change
IOC = 0b00000011;
// OPTION_REG reg
// GPPU' pull up enable
// INTEDG intrrupt edge
// T0CS TMR0 clock source
// T0SE TMR0 source edge
// PSA prescaler assignment
// PS<2:0> prescaler rate bits
//OPTION_REG = 0b00000000;
//OPTION_REGbits.nGPPU = 0;
while(1) {
//GPIObits.GP4 = !GPIObits.GP4;
//GPIObits.GP5 = !GPIObits.GP5;
wait(5); // wait for 5 sec
RELAY = 1; // turn on relay after 5 sec
while(flag == 0) {
__delay_ms(10);
}
while(flag == 1) {
__delay_ms(10);
}
}
return;
}
void wait(uint8_t w){
for(uint8_t c = 0; c < w; c++) {
__delay_ms(1000);
}
}