+ Reply to Thread
Results 1 to 10 of 10

Thread: 12C and SPI

  1. #1
    fromansr Newbie
    Join Date
    Apr 2009
    Location
    Australia
    Posts
    15

    Default 12C and SPI

    I think this is a complete newbie question...but is it possible to operate an SPI IC and a 12C IC simultaneously, using a pic micro that has 1 MSSP module. Judging from the datasheet (16f887), it seems I can only use 1 mode at a time. Does this mean I need a micro that has more than 1 MSSP??

    Thanks for any help and sorry for all the questions ive been posting!!!


  2. #2
    Super Moderator Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent
    Join Date
    Nov 2003
    Location
    Derbyshire, UK
    Posts
    29,794

    Default

    You can use hardware for one, and software for the other - take your pick which is which.
    PIC programmer software, and PIC Tutorials at:
    http://www.winpicprog.co.uk

  3. #3
    dknguyen Excellent dknguyen Excellent dknguyen Excellent dknguyen Excellent dknguyen Excellent dknguyen Excellent dknguyen Excellent dknguyen Excellent dknguyen Excellent
    Join Date
    May 2006
    Location
    Alberta, Canada
    Posts
    6,029

    Default

    If you mean connect both I2C and SPI devices to the same MSSP port and have the PIC switch back and forth between I2C and SPI modes to communicate with only one set of devices on the line, then yes. Obviously, you can't send an SPI and I2C signal over the same set of wires at the same time.

    You write code that reconfigures the port back and forth between the I2C and SPI functionality when switching modes. You also need to take care that I2C and SPI messages don't get read by the wrong device. Preventing SPI devices from reading I2C messages is easy since you jsut disable all SPI devices with chip select when sending I2C messages.

    Stopping I2C devices from reading SPI messages is a bit trickier. Before enabling SPI devices to send them a message, you send an I2C stop-bit to tell all I2C devices that the I2C message is over, and to stop listening to the line until they hear a start bit (this is standard when sending I2C so the device knows the message is actually over). And while you transmit SPI messages, you have to watch the two SPI wire being shared with the I2C so that they do not accidentally imitate an I2C start/stop bit which will wake up the I2C devices and cause them to start listening in (or even worse, if the SPI message happens to imitate the right address and read/write bits, it might initiate a read sequence where the I2C device sends out data which would make for a very difficult and random bug to hunt down).

    http://www.best-microcontroller-proj...-star-stop.png

    In I2C, all SDA logic transitions only occur while SCL is low except for the stop/start bits. This means you must make sure that the SDA wire only transitions while SCL is low while sending SPI messages. Any SDA transition while SCL is high will be interpreted as a either a start or stop bit by the I2C devices.
    Last edited by dknguyen; 18th May 2009 at 11:18 PM.
    Tanaka Sensei (avatar) says: Please spell it "ridiculous" correctly! Not "rediculous". ^^

  4. #4
    speakerguy79 Excellent speakerguy79 Excellent speakerguy79 Excellent speakerguy79 Excellent speakerguy79 Excellent speakerguy79 Excellent speakerguy79 Excellent
    Join Date
    May 2007
    Location
    USA
    Posts
    1,414

    Default

    dknguyen,

    I fear your answer, while 100% correct and very thorough, might tempt someone to try something that is not for the neophyte...

    I would recommend getting a PIC that has two ports (one for SPI and one for I2C) or like Nigel said - do one in hardware and the other in software.

  5. #5
    fromansr Newbie
    Join Date
    Apr 2009
    Location
    Australia
    Posts
    15

    Default

    Thanks for the detailed responses....I don't think I can actually disable the 12C device (real time clock), so I'll try finding a pic with 2 ports. Ill go with a hardware interface if a pic like this is not readily available.
    Last edited by fromansr; 19th May 2009 at 01:57 AM.

  6. #6
    Super Moderator Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent
    Join Date
    Nov 2003
    Location
    Derbyshire, UK
    Posts
    29,794

    Default

    Quote Originally Posted by fromansr View Post
    Thanks for the detailed responses....I don't think I can actually disable the 12C device (real time clock), so I'll try finding a pic with 2 ports. Ill go with a hardware interface if a pic like this is not readily available.
    Why bother?, it's pretty easy to do I2C master in software, and there's an example of reading a clock chip in my tutorials.
    PIC programmer software, and PIC Tutorials at:
    http://www.winpicprog.co.uk

  7. #7
    dknguyen Excellent dknguyen Excellent dknguyen Excellent dknguyen Excellent dknguyen Excellent dknguyen Excellent dknguyen Excellent dknguyen Excellent dknguyen Excellent
    Join Date
    May 2006
    Location
    Alberta, Canada
    Posts
    6,029

    Default

    You aren't disabling the I2C device. It still runs doing whatever it does. You are just enabling or disabling whether or not it is paying attention to what is on the bus. Chip select for SPI also doesn't disable the device- it just tells the SPI device whether or not to pay attention to what is being sent on the bus.
    Last edited by dknguyen; 19th May 2009 at 08:13 AM.
    Tanaka Sensei (avatar) says: Please spell it "ridiculous" correctly! Not "rediculous". ^^

  8. #8
    UTMonkey Good UTMonkey Good UTMonkey Good
    Join Date
    Oct 2007
    Location
    Chesterfield, Derbyshire - UK
    Posts
    428

    Default

    This is sort of related to something I am working on. I am writing a FAT library (yes I know there are loads out there) and the PIC in question has one MSSP.

    I was sort of sure I could just patch into the I\O for the EUSART (debug to terminal) whilst disabling the SPI (for the SD Card).

    The level shifter I am using (74*125) has a pin for each line that essentially switches off the line. Is there something out there that allow a single pin to toggle an amount of IO lines off and on? or am I overcomplicating things?

    Thanks

  9. #9
    dknguyen Excellent dknguyen Excellent dknguyen Excellent dknguyen Excellent dknguyen Excellent dknguyen Excellent dknguyen Excellent dknguyen Excellent dknguyen Excellent
    Join Date
    May 2006
    Location
    Alberta, Canada
    Posts
    6,029

    Default

    I assume yuo don't mean just branching one pin from the PIC out to be a bunch of enable/disable pins so that they all go on or off?

    With a decoder, you can get n pins on the PIC to activate (or deactivate) one pin out of 2^n pins and deactivate (or activate) the rest. THese in turn can be branched out like I described above so n pins can control the state of 2^n groups of pins.
    Last edited by dknguyen; 19th May 2009 at 08:34 PM.
    Tanaka Sensei (avatar) says: Please spell it "ridiculous" correctly! Not "rediculous". ^^

  10. #10
    UTMonkey Good UTMonkey Good UTMonkey Good
    Join Date
    Oct 2007
    Location
    Chesterfield, Derbyshire - UK
    Posts
    428

    Default

    Quote Originally Posted by dknguyen View Post
    I assume yuo don't mean just branching one pin from the PIC out to be a bunch of enable/disable pins so that they all go on or off?
    Could I do that???

    Anyway I am not familiar with decoder IC's, I will take a look.

    Many thanks.

    Mark

+ Reply to Thread

Tags for this Thread