 /*
 * File            : newmain.c
 * Author          : Ligo George
 * Company         : electroSome
 * Project         : SPI Slave Example
 * Microcontroller : PIC 16F877A
 * Created on April 15, 2017, 5:59 PM
 */
#pragma config FOSC = INTRCIO   // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select bit (MCLR pin function is MCLR)
#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)
#pragma config BOREN = OFF      // Brown-out Reset Selection bits (BOR disabled)
#pragma config IESO = ON        // Internal External Switchover bit (Internal External Switchover mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)

#include <xc.h>
#include <pic16f690.h>
#include "spi.h"

#define _XTAL_FREQ 8000000
#define _LED_PIN RC5

void interrupt SPI_Slave_Read()
{
    unsigned char b;
    if(SSPIF == 1)
    {
 //       b = spiRead();
//        if (b == 0) {
        if (spiRead() == 0) {
            _LED_PIN = 0;
        } else {
            _LED_PIN = 1;
        }      
//        spiWrite(0);
        SSPIF = 0;
    }
}


void main()
{
    TRISA = 0;
    TRISC5 = 0; // LED
    TRISC6 = 1; // SS
    ANS8=0;  // RC6 - Digital I/O

    GIE = 1;
   PEIE = 1;
   SSPIF = 0;
   SSPIE = 1;
   ADCON1 = 0x07;

//    _LED_PIN = 1;
//    __delay_ms(100);
//    _LED_PIN = 0;
//    __delay_ms(100);
//    _LED_PIN = 1;
//    __delay_ms(100);
//    _LED_PIN = 0;
//    __delay_ms(100);
    
   spiInit(SPI_SLAVE_SS_EN, SPI_DATA_SAMPLE_MIDDLE, SPI_CLOCK_IDLE_LOW, SPI_IDLE_2_ACTIVE);
   
   while(1)
   {
       /*if(spiDataReady())
       {
           PORTD = spiRead();
           spiWrite(PORTB);
       }
       */
       __delay_ms(5);
   }
}