Strange I2C Behaviour on PIC and some doubts

i2cpic

I'm posting the Code here:

    //***************************************************
    void main(void)
    { TRISCbits.TRISC2=0;// configures it as Output
      OpenI2C(MASTER, SLEW_ON); // Initialize I2C module
      SSPADD = 9;               //400kHz Baud clock(9) @16MHz
                                 //100kHz Baud clock(39) @16MHz


    EEByteWrite(0xA0, 0x30, 0xA5);
    PORTCbits.RC2=1;       

}

I happen to Burn this Code that i found on I2c App note and Tried to port the Code in to PIC18f.
Well when i burned the code.The SDA and SCL are showing Pulses with 8bits seen on SCL with the help of oscilloscope.Well the Transmission is supposed to End after a Byte Write Function but it shows pulses on both pins even after getting the Byte Write Function is done.

Well as far as from what i learned the I2cBus must be Idle after Writing Bytes!. Well I cross checked whether the ACKSTAT(ACKNOWLEDGEMENT STATUS IS RECIEVING ) and found it recieving from the slave which is an eeprom chip.

Why is this Strange behavior happening on the I2C? Or is it that because of not so clarity in I2c concepts.

Best Answer

Try adding: while(1){} to the end to keep your program from resetting.

Unlike running a PC application, microcontrollers have nowhere to exit to from main(). Generally you need to have an infinite loop or the processor or code will reset and get executed over and over.

For more complete explainations, see:

Who receives the value returned by main()?

or:

What happens when an embedded program finishes?