PIC24F04 as I2C slave - Address issue - Page 1
Forum section: electronics / general

author: Flanbix
author_rank: Contributor
timestamp: October 20, 2014, 09:08:44 am
content: Hi,I have a PIC24F04KA200 that I use as a I2C slave to drive a strip of WS2812.I have the program running on the slave working as expect, except that it doesn't matter which address I send first to the I2C, the slave will always process it. For example, my master send 0x1F or 0x00 as the address while the slave is set for 0x1E and it still works.I thought there was some hardware address matching inside the PIC though (Figure 3 on AN734) which check the incoming address with the register SSPADD (In my case I2C1ADD).I2C configuration of the slave (MPLABX XC16)Code:CloseI2C1();OpenI2C1((I2C_ON | I2C_7BIT_ADD | I2C_CLK_REL | I2C_STR_EN | I2C_IPMI_DIS), 0); //Configure I2C1ConfigIntI2C1( SI2C_INT_ON | SI2C_INT_ON );I2C1ADD = 0x1E; //I2C address set to 0x1EI2C1MSK = 0x00;Not really an issue for now but I would like to understand why...

author: dannyf
author_rank: Super Contributor
timestamp: October 20, 2014, 10:30:30 am
content: Yes, it is done via the IPMI bit and MSK register - under your set-up, it should not respond to any address other than that of your slave address.You may step throug hthe code and track the right registers to see if plib has set them up correctly or there is an errata for your device.

author: Flanbix
author_rank: Contributor
timestamp: October 20, 2014, 12:22:15 pm
content: I tried with I2C1CONbits.IPMIEN set to 0 or 1 but still have the same behavior.I will probably switch to the PIC24F08KL200 as I need more memory and also have the capacity to debug (This PIC24F cannot be debug without a special header from Microchip).

author: dannyf
author_rank: Super Contributor
timestamp: October 20, 2014, 09:53:07 pm
content: You could still debug via uart, pins, or LCD.the issue could be plib or hardware.

author: 22swg
author_rank: Frequent Contributor
timestamp: October 21, 2014, 08:23:18 am
content: You really need to debug the slave Pickit 3 ? , to confirm you are setting the10 bitslave address in I2C1ADD etc, address 0x00 is also a general call address , 0x1F could be a read for your 0x1E address, how is I2C master using the 7 bits of address, some code shifts address < 1 and or's the read / write bit, I have worked on PIC24F32KA302 I2C slave code but not used a library , search of forums suggest its not straight forward.

author: AndyC_772
author_rank: Super Contributor
timestamp: October 21, 2014, 08:48:26 am
content: The only real problem I've had using I2C on a PIC is speed of response. If the master doesn't support clock stretching - and a good few implementations don't - then you don't have a lot of time between receiving the device or register address, and having to make the first data bit available.This is the one and only time I've ever had an issue with the free Microchip compiler (XC8) in terms of code performance. The I2C handler only ran fast enough with full optimisation for speed turned on.

author: mikerj
author_rank: Super Contributor
timestamp: October 21, 2014, 12:04:56 pm
content: Are you correctly checking the values in the I2C1STAT register within your interrupt handler? e.g. are you testing the bus collision bit to ensure this hasn't caused an interrupt?

author: Flanbix
author_rank: Contributor
timestamp: October 21, 2014, 01:27:03 pm
content: I think the problem may be from the library.I tried to set the register manually to match what my OpenI2C1 function was supposed to do (Especially the IPMI register bit):Code://I2CCloseI2C1();OpenI2C1((I2C_ON | I2C_7BIT_ADD | I2C_CLK_REL | I2C_STR_EN | I2C_IPMI_DIS), 0); //Configure I2C1ConfigIntI2C1( SI2C_INT_ON | SI2C_INT_ON );I2C1ADD = 0x1F; //I2C address set to 0x1FI2C1MSK = 0x00;I2C1CONbits.IPMIEN = 0;I2C1CONbits.GCEN = 0;I2C1CONbits.A10M = 0;Now I can see a NACK instead of a ACK after the address is sent when I set the slave address to 0x18 . And it is sending a ACK only when the address of the slave is 0x1F.I know that the library functions are not the be trusted but still, I am surprise to have this kind of error.Thanks for the help.

author: dannyf
author_rank: Super Contributor
timestamp: October 22, 2014, 01:16:28 am
content: I guess it is the first mover's disadvantage: their hardware is too disparate that writing high quality / consistent software to cover all devices is tough.

author: AndyC_772
author_rank: Super Contributor
timestamp: October 22, 2014, 09:32:14 am
content: I'm not at all surprised, and it's one reason I just don't use manufacturers' libraries for simple, low level functions like this.Reading the data sheet (including the errata, *especially* for PICs) and setting register values yourself is the only way to really know how the hardware is configured, and therefore, how it will behave.Use a library, and you still need to know how the hardware is configured, but you also need to know how to make the library actually set up that configuration.

author: dannyf
author_rank: Super Contributor
timestamp: October 22, 2014, 10:31:55 am
content: The source code seems to set up the bits correctly. The issue here might be a "OR" style config word or "AND" style config word.