/*  Filename     PKIT5R.C
    Description  PKIT-5R RF Remote Receiver
    Hardware     PKIT-5R
    Compiler     CCS PCW C Complier V3.28
    Engineer     Kriangsak B.
    Company      Sila Research Co.,Ltd. */

/********** SETUP & I/O **********/

#include <16f819.h>

//#fuses LP // Low power osc < 200 khz
//#fuses XT // Crystal osc <= 4mhz
//#fuses HS	// High speed Osc (> 4mhz)
//#fuses EC_IO	// External clock
#fuses INTRC_IO // Internal RC Osc, no CLKOUT
//#fuses INTRC	// Internal RC Osc
//#fuses RC_IO // Resistor/Capacitor Osc
//#fuses RC	// Resistor/Capacitor Osc with CLKOUT
//----
//#fuses WDT // Watch Dog Timer
#fuses NOWDT // No Watch Dog Timer
//----
#fuses PUT // Power Up Timer
//#fuses NOPUT // No Power Up Timer
//----
//#fuses MCLR // Master Clear pin enabled
#fuses NOMCLR // Master Clear pin used for I/O
//----
//#fuses BROWNOUT // Reset when brownout detected
#fuses NOBROWNOUT // No brownout reset
//----
//#fuses LVP // Low Voltage Programming on B3
#fuses NOLVP // No low voltage prgming, B3 used for I/O
//----
//#fuses CPD // Data EEPROM Code Protected
#fuses NOCPD // No EE protection
//----
//#fuses WRT // Program Memory Write Protected
#fuses NOWRT // Program memory not write protected
//----
//#fuses DEBUG // Debug mode for use with ICD
#fuses NODEBUG // No Debug mode for ICD
//----
//#fuses CCPB2 // CCP1 input/output multiplexed with RB2
//#fuses CCPB3 // CCP1 input/output multiplexed with RB3
//----
//#fuses PROTECT // Code protected from reads
#fuses NOPROTECT // Code not protected from reading

#use delay (clock=4000000)
//#use rs232 (xmit=PIN_A6,rcv=PIN_A7,baud=9600,bits=8,parity=n)
#use rs232 (xmit=PIN_A6,baud=9600,bits=8,parity=n)
#byte OSCCON = 0x8F

#define DATA     PIN_A0
#define LED      PIN_A1
#define REL1     PIN_A2
#define REL2     PIN_A3
#define REL3     PIN_A4
#define MODE     PIN_A5
#define SBIT     PIN_A7

/********** SUB FUNCTION **********/

void flash (char x) {
    while (x>0) {
        output_low (LED);
        delay_ms (250);
        output_high (LED);
        delay_ms (250);
        x--;
    }
}

void sound (char freq,int16 time) {              // Sound Generate
    unsigned char i;
    while (time>0) {
        output_low (SBIT);                       // out signal low
        for (i=1;i<=freq;i++)
            time--;
        output_high (SBIT);                      // out signal high
        for (i=1;i<=freq;i++)
            time--;
    }
}

/********** RXRUN FUNCTION **********/

int16 rxbit (void) {              // check bit cycle
    int16 x;
    x = 0;
    while (input (DATA)==1) x++;
    while (input (DATA)==0) x++;
    return (x);
}

void rxpre (void) {               // check preamble
    int16 x;
    char c;
    c = 0;
    while (c<5) {                 // check bit 0
        x = rxbit ();
        if (x>=33 && x<=37) c++; else c = 0;
    }
    while (x<53) {
        x = rxbit ();             // check bit 1
    }
}

char rxbyte (void) {              // read one byte
    char x,i;
    x = 0;
    for (i=0;i<=7;i++) {
        x >>= 1;
        if (rxbit ()>53) x |= 0x80;
    }
    return (x);
}

char rxrecv (void) {
    char i,j,a,x;
    rxpre ();
    output_low (LED);
    i = rxbyte ();
    j = rxbyte ();
    a = rxbyte ();
    x = rxbyte ();
    if (i==0x53 && j==0x35 && a==input_b ()) {   // match
        printf (":1%X%X\r",a,x);
        sound (10,6000);
        output_high (LED);
        return (x);
    }
    else {                                       // not match
        printf (":0%X%X\r",a,x);
        output_high (LED);
        return (0xff);
    }
}

/********** MAIN FUNCTION **********/

void start (void) {
    OSCCON = 0x60;                          // set int-osc 4 Mhz
    while ((OSCCON & 0x4)!=0x4);            // wait for clock stable
    delay_ms (500);
    flash (2);
    printf ("PKIT-5R ...\r");
}

void main (void) {
    char x,a,b,c;
    start ();
    a = 1;
    b = 1;
    c = 1;
    while (1) {
        x = rxrecv ();
        if (x==0x1) {
            if (input (MODE)==1) {output_low (REL1); delay_ms (500); output_high (REL1); a = 1;}
            else {if (a==0) a = 1; else a = 0; output_bit (REL1,a);}
        }    
        else if (x==0x2) {
            if (input (MODE)==1) {output_low (REL2); delay_ms (500); output_high (REL2); b = 1;}
            else {if (b==0) b = 1; else b = 0; output_bit (REL2,b);}
        }    
        else if (x==0x3) {
            if (input (MODE)==1) {output_low (REL3); delay_ms (500); output_high (REL3); c = 1;}
            else {if (c==0) c = 1; else c = 0; output_bit (REL3,c);}
        }  
        else if (x==0x4) {output_low (REL1); a = 0;}
        else if (x==0x5) {output_low (REL2); b = 0;}
        else if (x==0x6) {output_low (REL3); c = 0;}
        else if (x==0x7) {output_high (REL1); a = 1;}
        else if (x==0x8) {output_high (REL2); b = 1;}
        else if (x==0x9) {output_high (REL3); c = 1;}
    }
}



