/*  Filename     PKIT5T.C
    Description  PKIT-5T RF Remote Transmitter
    Hardware     PKIT-5T
    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)
#byte OSCCON = 0x8F

#define DATA     PIN_A0
#define LED      PIN_A1
#define KEY1     PIN_A2
#define KEY2     PIN_A3
#define KEY3     PIN_A4
#define MJP      PIN_A5

/********** TXRUN FUNCTION **********/

void tx0 (void) {                 // send bit 0
    output_high (DATA);
    delay_us (250);
    output_low (DATA);
    delay_us (250);
}

void tx1 (void) {                 // send bit 1
    output_high (DATA);
    delay_us (500);
    output_low (DATA);
    delay_us (500);
}

void txbyte (char x) {            // send one byte
    char i;
    for (i=0;i<=7;i++) {
        if ((x & 1)==1) tx1 (); else tx0 ();
        x >>= 1;
    }
}

void txsend (char x) {
    char a;
    a = input_b ();
    txbyte (0);           // preamble
    txbyte (0);
    tx1 ();               // start
    txbyte (0x53);        // sila code
    txbyte (0x35);
    txbyte (a);           // address
    txbyte (x);           // command
    tx0 ();               // end
    output_low (LED);
    delay_ms (250);
    output_high (LED);
    delay_ms (250);
}

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

void start (void) {
    OSCCON = 0x60;                          // set int-osc 4 Mhz
    while ((OSCCON & 0x4)!=0x4);            // wait for clock stable
}

void main (void) {
    char f,a,b,c;
    start ();
    f = 0;
    a = 1;
    b = 1;
    c = 1;
    while (1) {
        if (input (MJP)==0) {
            if (input (KEY1)!=a) {
                a = input (KEY1);
                if (a==0) txsend (0x4); else txsend (0x7);
            }
            if (input (KEY2)!=b) {
                b = input (KEY2);
                if (b==0) txsend (0x5); else txsend (0x8);
            }
            if (input (KEY3)!=c) {
                c = input (KEY3);
                if (c==0) txsend (0x6); else txsend (0x9);
            }
        }
        else {
            if (f==0) {
                if (input (KEY1)==0) {txsend (0x1); f = 1;}
                else if (input (KEY2)==0) {txsend (0x2); f = 1;}
                else if (input (KEY3)==0) {txsend (0x3); f = 1;}
            }
            if (input (KEY1) && input (KEY2) && input (KEY3)) {f = 0; delay_ms (100);}
        }
    }
}



