I2C Addressing

Discussion in 'Embedded Systems and Microcontrollers' started by LewisMF, Jun 22, 2015.

  1. LewisMF

    Thread Starter Member

    Nov 15, 2014
    63
    2
    Hi Everyone!

    I am planning on building a system with 1 master PIC18F45K22 and 6 slaves PIC16F1704.

    My question is, how do I determine the addresses for the slaves? I have checked the datasheet of the PIC16F1704 under the I2C slave mode operation and cant seem to find what address it has (7-bits addressing) or how to give it an address, can I give it any address I want within the range 1 - 128?, I am a bit confused.

    Is it possible to address the device by hardware, maybe with DIP switches? Is it possible to make the master scan the bus on start-up and give the slaves addresses?

    It is my first time with I2C so go easy on me :)
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,135
    1,786
    I do not know the answer for sure. Look for a register in the I2C peripheral that allows you to set or configure the address. I'll download the datasheet to see if I can find it.

    EDIT: That wasn't too hard; look at figure 28-3 on page 270. There you will find the "Mask and Match" hardware for address recognition. Read the datasheet carefully with respect to registers "SSPMSK" and "SSPADD". You have full control in firmware, no DIP switches or pre-configured addresses in hardware.
     
    Last edited: Jun 22, 2015
  3. OBW0549

    Well-Known Member

    Mar 2, 2015
    1,301
    880
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    You can and must assign the slave address to the PIC when running it as a slave.

    1-128 is an appropiate range, do check the I2C spec for reserved addresses and don't step on any other perif's you may be using.
     
  5. LewisMF

    Thread Starter Member

    Nov 15, 2014
    63
    2
    I have checked the datasheet but I still do not have very clear what addresses I can give the slaves? Can I give them any address within the range of 1-128?

    Is it possible to give it an address by hardware? This would be a very interesting solution therefore in this system the master will be the motherboard and the slaves will range from 1 to 6, and will vary depending on how big the system needs to be.

    Any suggestions?

    I would like to thank you guys for the help in advanced :)
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    You are free to give the slaves any address you wish, by any means necessary.

    You can hard code them into the hex files, have them load from EEPROM, or read some pins to set a value in a range.

    When I needed 10 slaves on my master I kept the code constant for them all. I conveniently had an option to attach an LCD display and some buttons for calibration purposes to each slave. One of the cal options was to set the slave address, which was stored in EEPROM. I used addresses in the range of 0xC0 to 0xCA AFAIR.
     
  7. LewisMF

    Thread Starter Member

    Nov 15, 2014
    63
    2
    The problem is that the slaves will be on different PCB's and the system will be increased adding these PCB's which will connect to the master via a flat ribbon cable. And because the slave module cannot work on its own the addresses will be set manually.

    You said the address on each slave can be set reading some pin's values...Could you supply a hardware/software example? I would be truly appreciate it :)
     
  8. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Ever look at a device like the 24AA512 EEPROM?
    http://ww1.microchip.com/downloads/en/DeviceDoc/21754M.pdf

    See how it uses external pins to set the I2C address? You could do something like that.

    You could change the program in each separate PCB's PIC to keep it unique.

    You could change a value in each separate PCB's PIC EEPROM to keep it unique.

    You may even devise some scheme to use some resistors to make a voltage to set the address.
     
  9. LewisMF

    Thread Starter Member

    Nov 15, 2014
    63
    2
    Okay so I get the idea now. I understand that setting the I2C address using external pins would also be possible with the PIC16F1704?

    The idea of changing it in each program would be a bit difficult therefore I would like to develop a single program that will run the same on all the boards (therefore they will all be identical and carry out the same tasks) and changing the address of each one by program memory would be very flexible for the end user.

    What I had in mind would be to use for example a 4 position DIP switch and set the address binary on each board. That way the end user would only have to set the address manually on each board and the master would scan an recognize the addresses on start-up.

    What do you think? :)
     
    DNA Robotics likes this.
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    If all you will ever need is 16 boards then that should work fine.

    Since your program needs to set the slave address your program can vary this address in any mannor your code will supply. At start up it reads the pins (switch positions) and uses that information to calculate the address.
     
  11. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,645
    759
    If the number of boards stays within reason you could assign a unique number to each one and THAT would be the ID for the PIC implanted in that board. Otherwise use a small range for each board. That could help in limitating the uncertainity at the moment of setting the ID in place.
     
  12. LewisMF

    Thread Starter Member

    Nov 15, 2014
    63
    2
    How would this be done with the PIC16F1704? What pins are used to select the I2C address?
     
  13. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Any pins you wish that can be set as an input.

    In your power on initialization you read the pins, construct an I2C address from that, and init the I2C slave module with that information.
     
  14. John P

    AAC Fanatic!

    Oct 14, 2008
    1,632
    224
    Sorry to introduce a note of doom and gloom here, but I2C is not intended for remote operation of peripherals. There's a clue in the fact that it actually stands for "Inter-Integrated Circuit". If it's sending data off one board and onto another, you need to consider the cable length, the impedance, the electrical environment and the data rate. It's sad stuff to worry about, but you really are talking about a transmission line.
     
    OBW0549 likes this.
  15. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,379
    494
    There are chips that extend i2c range.
     
  16. LewisMF

    Thread Starter Member

    Nov 15, 2014
    63
    2
    Hi John P,

    Even know the master and slaves are on different PCB's, they will be in the same metal (grounded) enclosure and will be connected via a flat ribbon cable, very close together. I'm talking about maybe <10 cm of 'transmission line' so I really don't think it will be a problem.

    If I was intending to communicate over longer distances I would of course search for an appropriate protocol designed for that purpose.

    My real confusion is with the addressing...as at startup I don't know exactly how many slaves there will be (within the range of 1 - 6) I need the master to scan the bus to identify the slaves.

    And my final problem is the slaves addresses...as I explained in previous posts I need to be able to address the slaves manually via a 4 position DIP switch. And I still cannot figure out how to do this with the PIC16F1704 :S
     
  17. John P

    AAC Fanatic!

    Oct 14, 2008
    1,632
    224
    OK, just a few inches of cable shouldn't be a problem.

    Surely you can identify which slaves you have just by addressing each one that can be potentially there in turn, and noting which ones respond?

    For setting the addresses, assuming you can spare 4 input pins, just wire the DIP switches between the pins and ground, turn on the pullups on those pins, and see if they're being pulled low via the switches. If you have a shortage of pins, you could rig up the DIP switch with resistors to create a kind of DAC, and read it in on a single pin as an analog level.
     
    atferrari likes this.
  18. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,379
    494
    1.
    10 cm will be fine. It is when you get into meter or more, than you will have problems. There are a couple of ways to make i2c to work for longer distances, but, as you noted, your distance is not great so you don't need them.

    2.
    If your slaves use pre determined addresses and you know the maximum number of slaves, all you have to do is make master check through the list of slave addresses. Master can store the list in an array or you can do manual if statements.

    3.
    The manual address input is almost like interfacing keypad to uC. Only your's is even simpler. First you have to decide what input you want. Do you want active high to be the input or active low? Then you just configure 4 pins of the PIC as inputs (or configure the whole port, probably 8 pins, as input port). If that PIC has 8 bit ports, each pin of the port is a bit. Once you decide what type of input you want, you can wire 4 pins to be your digital zero and use the other 4 pins for the input via 4 position DIP switch. I think the most sensible would be to tie pins 7, 6, 5, 4 to be digital zero and use pins 3, 2, 1, 0 for the input from the DIP switch. This way when you select 0000 on switch, you will have 0000 0000 on the PIC port (00 hex). When you select 0001 on switch, you will have 0000 0001 on the PIC port (01 hex). When you select 0010 on switch, you will have 0000 0010 on PIC port (02 hex). And so on.
     
    LewisMF likes this.
  19. korchoi

    Member

    Jun 5, 2015
    59
    6
    Interesting stuff.
    What are you building with 7 slave PIC's? an LED matrix? a multi-motor controller? 3D printer, maybe?
     
  20. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    As long as you are getting ribbon cables and connectors to lash everything together why not add a few extra lines to act as an address? Then you don't need the dip switches and you are guaranteed unique I2C addresses on every slave.
     
Loading...