I2C and SPI on same PIC

Started by Rodrigo January 16, 2009
Hi...

I have 2 sensor which I need to read with a 18F452 PIC, but one is I2C
and the other is SPI. Taking a look at datasheet I saw that the pins
used for both protocols are the same pins. Is there anything I could
do to read them both?

Thanks in advance...
Rodrigo Basniak
Hi

You probably have already known it but there are software implementation
routines of both protocols. In that case, u can use any digital I/O pins of
the processor to communicate with those devices.

Most high level languages already have inline software procedures and lots of
external ones can be found on the net.

Isik

On Fri, 16 Jan 2009 10:11:12 -0000, Rodrigo wrote
> Hi...
>
> I have 2 sensor which I need to read with a 18F452 PIC, but one is
> I2C and the other is SPI. Taking a look at datasheet I saw that the pins
> used for both protocols are the same pins. Is there anything I could
> do to read them both?
>
> Thanks in advance...
> Rodrigo Basniak
>
>
>
> to unsubscribe, go to http://www.yahoogroups.com and follow the
> instructions
Nope. That's an either/or thing. The two buses are quite different
electrically. I2C requires pullups while SPI generally does not.
That's just one difference.

They make parts with more than one of those interfaces on them. You
could also add an external I/O port that speaks one of those
protocols.

Mike Harpe, N4PLE
Sellersburg, IN USA

On Fri, Jan 16, 2009 at 5:11 AM, Rodrigo
wrote:
> Hi...
>
> I have 2 sensor which I need to read with a 18F452 PIC, but one is I2C
> and the other is SPI. Taking a look at datasheet I saw that the pins
> used for both protocols are the same pins. Is there anything I could
> do to read them both?
>
> Thanks in advance...
> Rodrigo Basniak
>
>
>
Sure,

Bit bang the SPI, it isn't hard. Since it is a synchronous hardware
protocol the clock rate, and to some extent, the precision of the clock
isn't all that critical. SPI is very fast and you can bang the bits in
a very short time.

DLC

Rodrigo wrote:
> Hi...
>
> I have 2 sensor which I need to read with a 18F452 PIC, but one is I2C
> and the other is SPI. Taking a look at datasheet I saw that the pins
> used for both protocols are the same pins. Is there anything I could
> do to read them both?
>
> Thanks in advance...
> Rodrigo Basniak
>
>
>
Yes, I just find out those software routines on MikroC to both
protocols. But to use those, since they're time dependant, I have to
disable the interrupts and I may loose time on the main program to
take these readings. I just don't know if this will really turn on a
big issue or not.

I would like to take reading from a bunch of sensors I'll intall in a
RC helicopter: acelerometers, gyros, pressure, compass, gps, etc. I
just thought on adding a second and smaller pic to take care of gps,
pressure and compass and then this one send all already the processed
data to the main PIC, unloading the processing of the main one. These
3 have lower sampling rate, arou 2-4Hz, while the sensors on the main
pic will be working at around 50-100Hz.

GPS is serial, compass is I2C and pressure is SPI. I could implement
I2C and SPI via softeare on this pic, and then send via SPI or I2C
(with the hardware modules) to the main pic. Any thoughts? Which one
will be better to communicate both pics, spi or i2c?

I'm kinda new to the pic world, I'm a mechanical engineering student,
so all this stuff is not my specialty, but I'm really enjoying to
learn all these things all by myself =) I hope I can help here someday
too =)

Thanks a lot,
Rodrigo Basniak

--- In p..., "Rodrigo"
wrote:
>
> Hi...
>
> I have 2 sensor which I need to read with a 18F452 PIC, but one is I2C
> and the other is SPI. Taking a look at datasheet I saw that the pins
> used for both protocols are the same pins. Is there anything I could
> do to read them both?
>
> Thanks in advance...
> Rodrigo Basniak
>

Rodrigo wrote:
> Yes, I just find out those software routines on MikroC to both
> protocols. But to use those, since they're time dependant, I have to
> disable the interrupts and I may loose time on the main program to
> take these readings. I just don't know if this will really turn on a
> big issue or not.

Try it and see - A bit banged SPI will frequently not need you to
disable IRQ's because most devices can handle a small clock stretch or two.

DLC

> I would like to take reading from a bunch of sensors I'll intall in a
> RC helicopter: acelerometers, gyros, pressure, compass, gps, etc. I
> just thought on adding a second and smaller pic to take care of gps,
> pressure and compass and then this one send all already the processed
> data to the main PIC, unloading the processing of the main one. These
> 3 have lower sampling rate, arou 2-4Hz, while the sensors on the main
> pic will be working at around 50-100Hz.
>
> GPS is serial, compass is I2C and pressure is SPI. I could implement
> I2C and SPI via softeare on this pic, and then send via SPI or I2C
> (with the hardware modules) to the main pic. Any thoughts? Which one
> will be better to communicate both pics, spi or i2c?
>
> I'm kinda new to the pic world, I'm a mechanical engineering student,
> so all this stuff is not my specialty, but I'm really enjoying to
> learn all these things all by myself =) I hope I can help here someday
> too =)
>
> Thanks a lot,
> Rodrigo Basniak
>
> --- In p..., "Rodrigo"
> wrote:
>> Hi...
>>
>> I have 2 sensor which I need to read with a 18F452 PIC, but one is I2C
>> and the other is SPI. Taking a look at datasheet I saw that the pins
>> used for both protocols are the same pins. Is there anything I could
>> do to read them both?
>>
>> Thanks in advance...
>> Rodrigo Basniak
>>
>
>
I have an easy routine to bit-bang SPI, no SPI port needed. I think I
got it from wikipedia. I used the I2C port to do I2C since it seemed
more complicated. PIC18F66J50 & family has both & USB, nice MCU, about
$3.30 for 1k.

http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus

or here:

unsigned char SPIBitBang8BitsMode0(unsigned char byte)
{
unsigned bit;

for (bit = 0; bit < 8; bit++) {
/* write MOSI on trailing edge of previous clock */
if (byte & 0x80)
SETMOSI();
else
CLRMOSI();
byte <<= 1;

/* half a clock cycle before leading/rising edge */
SPIDELAY(SPISPEED/2);
SETCLK();

/* half a clock cycle before trailing/falling edge */
SPIDELAY(SPISPEED/2);

/* read MISO on trailing edge */
byte |= READMISO();
CLRCLK();
}

return byte;
}

--- In p..., "Rodrigo"
wrote:
>
> Hi...
>
> I have 2 sensor which I need to read with a 18F452 PIC, but one is I2C
> and the other is SPI. Taking a look at datasheet I saw that the pins
> used for both protocols are the same pins. Is there anything I could
> do to read them both?
>
> Thanks in advance...
> Rodrigo Basniak
>

use the hardware for one, and implement the other with software.i did it
with 16F, used the SPI hardware port and implemented I2C in software.worked
well.