Exemplo n.º 1
0
/*
 * MAIN
 */
int main(int argc, char** argv) {

    // RA1 as DO
    LATA = 0;
    ADCON1 = 0x06;
    TRISA1 = 0;
    LATA1 = 1;

    // I2C configuration
    i2c_setup();

    unsigned char w;
    for(w=0;w<20;w++)
        I2C_Recv[w]=0;

    unsigned int temps = 25;
    unsigned char temp, length=0;
    while(1){
        LATA1 = ~LATA1;
        Delay10KTCYx(temps);
        check_interruptions();
        if (DataRdyI2C()==1) {
            temps += 25;
            temp = ReadI2C();
            //********************* Data reception from master by slave *********************
            do {
                while(DataRdyI2C()==0);        // WAIT UNTILL THE DATA IS TRANSMITTED FROM master
                I2C_Recv[length++]=getcI2C();  // save byte received
            }
            while(length!=20);
        }
    }

    //******************** write sequence from slave *******************************
    while(SSPSTATbits.S!=1); //wait untill STOP CONDITION

    //********************* Read the address sent by master from buffer **************
    while(DataRdyI2C()==0); //WAIT UNTILL THE DATA IS TRANSMITTED FROM master
    temp = ReadI2C();

    //********************* Slave transmission ************************************
    if(SSPSTAT & 0x04) //check if master is ready for reception
        while(putsI2C(I2C_Send));			// send the data to master

    //-------------TERMINATE COMMUNICATION FROM MASTER SIDE---------------
    CloseI2C(); //close I2C module
    
    while(1) {
        LATA1 = ~LATA1;
        Delay10KTCYx(100);
    }; //End of program


    return (EXIT_SUCCESS);
}
Exemplo n.º 2
0
void main(void)
{
    /* Configure the oscillator for the device */
    ConfigureOscillator();

    /* Initialize I/O and Peripherals for application */
    InitApp();
    
    // i2c conf
    // /* // i2c slave
    TRISBbits.TRISB3 = 1;
    TRISBbits.TRISB4 = 1; //Statement to configure the DATA pin as an input.
    OpenI2C(SLAVE_7, SLEW_OFF);
    SSPADD = 0xB0;
   //  */
    /* //i2c master
        TRISBbits.TRISB3 = 1;
        TRISBbits.TRISB4 = 1; //Statement to configure the DATA pin as an input.
        OpenI2C( MASTER, SLEW_OFF);
        SSPADD = 0x27; //SSPADD Baud Register used to calculate I2C clock speed in MASTER mode (in this case 100Khz)
     */

    unsigned char addr;
    unsigned char data;

    int delayVal = 5;
    LEDPin = 1;//Set LED Pin

    while(1)
    {
     //   /* //i2c slave
        if(DataRdyI2C() == 1)
        {
            addr = ReadI2C();
            while(DataRdyI2C() == 0);
            data = ReadI2C();
        }
    //*/
        /* //i2c master
            StartI2C();
            IdleI2C();
            putcI2C( 0xB0 ); //send address
            IdleI2C();
            putcI2C( 0x11 ); //send data
            IdleI2C();
            StopI2C();

         */

        LEDPin = ~LEDPin;//Toggle LED Pin
        Delay10KTCYx(delayVal);//Delay 250K cycles (1 second at 1MHz since each instruction takes 4 cycles)
    }

}