/* Simple I2C slave example, in this simple state the program halt the chip until address and everything match
 * Plus transmit only one byte. See Simple I2C Master */

// PIC16F1829 Configuration Bit Settings
#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = ON    // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = ON        // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = OFF      // Disabled, see OSCCON
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = OFF         // Low-Voltage Programming Enable (Low-voltage programming enabled)
// #pragma config statements should precede project file includes.

#include <xc.h>

#define _XTAL_FREQ 32000000

#ifndef _i2cSDA
#define _i2cSDA  TRISBbits.TRISB4
#endif
#ifndef _i2cCLK
#define _i2cCLK  TRISBbits.TRISB6
#endif

void initI2C(void);
void interrupt slaveI2Csend (void);

unsigned char mumu=0;

void main(void) {
    OSCCON=0b11110000;     
    _i2cSDA=1;
    _i2cCLK=1;
    
    initI2C();
    while(1); //asta e de test sa vedem daca merge intreruperea
    return;
}

void initI2C(void){
    ANSELB=0;    
	SSP1STATbits.SMP=1;		//slew rate enabled 
	SSP1STATbits.CKE=0;		//no SMB needed  
	SSP1CON1bits.SSPM3=1;	//slave i2c, 7bit address
	SSP1CON1bits.SSPM2=1;	//slave i2c, 7bit address	
	SSP1CON1bits.SSPM1=1;	//slave i2c, 7bit address
    SSP1CON1bits.SSPM0=0;	//slave i2c, 7bit address 
    
    //Slave address chosen is E (Encoder) 0x45 (dec: 69) 0b10001010(last bit is unused)
    SSP1ADD=0b10001010;     
    
    SSP1CON1bits.SSPEN = 1; //enable I2C module
    PIE1bits.SSP1IE = 1;
    INTCONbits.PEIE = 1;
    INTCONbits.GIE = 1; //de acum intreruperile sunt active
    return;
}

void interrupt slaveI2Csend (void){ 
    if (PIR1bits.SSP1IF) {                //make sure that SSP1 address match brought us here, do no handle any other interrupt         
        PIR1bits.SSP1IF=0;
        SSP1BUF;                          //clear buffer to prepare for next transfer
        if (SSP1STATbits.R_nW){           //master wants to read
                mumu=mumu+1u;
           		SSP1BUF = mumu;            //load data               
                }    
        } 
    SSP1CON1bits.CKP=1;       //release clock to transmit data to master

}