• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

12C and SPI

Status
Not open for further replies.

fromansr

New Member
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!!!
 

dknguyen

Well-Known Member
Most Helpful Member
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-projects.com/images/i2c-tutorial-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:

Speakerguy

Active Member
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.
 

fromansr

New Member
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:

Nigel Goodwin

Super Moderator
Most Helpful Member
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.
 

dknguyen

Well-Known Member
Most Helpful Member
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:

UTMonkey

New Member
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
 

dknguyen

Well-Known Member
Most Helpful Member
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:

UTMonkey

New Member
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
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top