Pic32 start bit is not cleared - basic I2C setting

I am new to embedded programming and try to get my first I2C project working. I am using PIC32MX795F512L. I pretty much follow the chip specification for I2C on PIC32. The problem I am facing is the S bit, which is never cleared from the I2C1STAT register. I donโ€™t really understand if I need to do this or if it is done automatically when the Start event is completed, but now I'm trying to manually clear it. However, nothing I do seems to have an effect. If you need more information to make it easier to understand what is happening, let me know. I use PICKIT3 to get debugging information. I know that a Master interrupt occurs, the S bit is set, I exit the interrupt code and hang on the while statement checking I2C1STATbits.S.

Edit: I am editing this post to have new code instead of old. Now I am using a 20 MHz peripheral clock. Only one of the many things I tried today did not work. The delay is only 256 ms. I know super long time, but it was fast.

main()
{
//Setup I2C1CON

I2C1CONbits.SIDL = 0;   //Continue to run while in Idle
I2C1CONbits.SCLREL = 1; //Release the clock (Unsure of this)
I2C1CONbits.A10M = 0;   //Using a 7 bit slave address
I2C1CONbits.DISSLW = 1; //Slew rate control disabled because running at 100 KHZ

I2C1ADD = 0x1E;         //Slave address without read or write bit
I2C1BRG = 0x060;        //Set the BRG clock rate - Based on Page 24-19

I2C1CONbits.ON = 1;     //Turn on the I2C module

delay();

I2C1CONbits.SEN = 1;    //Initiate a start event

while(I2C1CONbits.SEN == 1);    //Wait until Start event is done

I2C1TRN = 0x3C;                 //Load the address into the Transmit register

while(I2C1STATbits.TRSTAT == 1);
while(I2C1STATbits.ACKSTAT == 0);    //Wait for a ACK from the device

I2C1TRN = 0x00;

while(I2C1STATbits.TRSTAT == 1);
while(I2C1STATbits.ACKSTAT == 0);

I2C1TRN = 0x70;

while(I2C1STATbits.TRSTAT == 1);
while(I2C1STATbits.ACKSTAT == 0);


while(1);

}

Thanks for any help.

+4
source share
2 answers

I also just start in the PIC32MZ family, setting up I2C to talk with various memory chips. I used your code and modified it to make it work correctly. Since I use the PIC32MZ family, I believe that the I2C registers should probably be the same.

I2C Configuration:

I2C1CONbits.SIDL = 0;       // Stop in Idle Mode bit                        -> Continue module operation when the device enters Idle mode
I2C1CONbits.A10M = 0;       // 10-bit Slave Address Flag bit                -> I2CxADD register is a 7-bit slave address
I2C1CONbits.DISSLW = 1;     // Slew Rate Control Disable bit                -> Slew rate control disabled for Standard Speed mode (100 kHz)
I2C1CONbits.ACKDT = 0;      // Acknowledge Data bit                         -> ~ACK is sent
I2C1BRG = 0x0F3;            // Baud Rate Generator set to provide 100KHz for SCL with 50 MHz xtal. 

, I2C Datasheet, pdf .

I2C:

// 1. Turn on the I2C module by setting the ON bit (I2CxCON<15>) to โ€˜1โ€™.
I2C1CONbits.ON = 1;         // I2C Enable bit                               -> Enables the I2C module and configures the SDAx and SCLx pins as serial port pins

//------------- WRITE begins here ------------
// 2. Assert a Start condition on SDAx and SCLx.
I2C1CONbits.PEN = 0;        // Stop Condition Enable Bit                    -> Stop Condition Idle 
I2C1CONbits.SEN = 1;        // Start Condition Enable bit                   -> Initiate Start condition on SDAx and SCLx pins; cleared by module
while(I2C1CONbits.SEN == 1);            // SEN is to be cleared when I2C Start procedure has been completed 

// 3. Load the Data on the bus
I2C1TRN = 0b10100000 ;          // Write the slave address to the transmit register for I2C WRITE 
while(I2C1STATbits.TRSTAT == 1);        // MASTER Transmit still in progress
while(I2C1STATbits.ACKSTAT == 1);       // Master should receive the ACK from Slave, which will clear the I2C1STAT<ACKSTAT> bit. 

I2C1TRN = 0xCE;                 // Register Address 
while(I2C1STATbits.TRSTAT == 1);
while(I2C1STATbits.ACKSTAT == 1);

I2C1TRN = 0xCF;                 // Register Value 
while(I2C1STATbits.TRSTAT == 1);
while(I2C1STATbits.ACKSTAT == 1);
I2C1CONbits.PEN = 1;        // Stop Condition Enable Bit                    -> Initiate Stop condition on SDAx and SCLx pins; cleared by module

//-------------- WRITE ends here -------------

, , .

+1

(! (I2C1STATbits.ACKSTAT == 0)); , . I2C1ADD PIC mc, I2C slave.If , PIC .

pdf, , PIC I2C , , PIC I2C1ADD. http://ww1.microchip.com/downloads/en/DeviceDoc/61116F.pdf

-1

All Articles