>-----Original Message----- >From: Ted Larson [mailto:ted@LARSONLAND.COM] >Sent: 15 April 2004 16:10 >To: PICLIST@MITVMA.MIT.EDU >Subject: [PIC:] I2C & SPI on the same PIC... > > >All this talk about SPI devices, has me thinking about an >interesting problem I have, that perhaps someone can suggest >an easy solution for. > >I am not too familiar with SPI, I am just learning about it, >however, I am pretty familiar with I2C at this point. I have >a circuit that has a PIC18F252 as an I2C master, and it talks >to several I2C slave devices. I have recently been presented >with an SPI device that I need to interface to the same I2C >master PIC. The SSP hardware on the PIC is pretty much I2C or >SPI, and not both. Short of switching everything to SPI, what >are my options? Is there some kind of I2C to SPI converter >out there? I have poked around, and there doesn't appear to >be a PIC with more than one SSP on it...perhaps there is one I >am missing? > >Thanks, > >- Ted SPI is an order of magnitude more simple than I2C. Think of a shift register that has a chip select signal and that's about as complex as it gets. The good news is that SPI is also much easier than I2C to bit bash, so unless you need very high speed, you can keep the MSSP for I2C master and bit bash the SPI: Set CS (chip select) low Set SDO (data out) according to bit (most significant bit first) Set SCK (clock) high Read SDI (data in) Set SCK low In assembly this boils down to relatively few instructions, so it's often practical to code this inline for all 8 bits rather than use a loop for optimal speed. #define CS LATB,0 #define SDO LATB,1 #define SDI PORTB,2 #define SCK LATB,3 clrf DataIn bsf CS ; chip select low ; clock in/out bit 7 btfss DataOut,7 bcf SDO ; data out low btfsc DataOut,7 bcf SDO ; data out high bsf SCK ; clock high btfsc SDI ; if input high bsf DataIn, 7 ; set bit in DataIn bcf SCK ; clock low ; clock in/out bit 6 btfss DataOut,6 bcf SDO ; data out low btfsc DataOut,6 bcf SDO ; data out high bsf SCK ; clock high btfsc SDI ; if input high bsf DataIn, 6 ; set bit in DataIn bcf SCK ; clock low ...etc.... This emulates the operation of the MSSP, where 8 bits are read in at the same time that 8 bits are transmitted (i.e. to read only, you would perform a dummy write). If you only ever need to read or write separately you can make a separate read and write function that would execute more quickly. There was a recent discussion on the faster method of clocking out 8 bits in the manner, regarding some benchmakrs for the Maxim "MAXQ" microcontroller. You should be able to find this in the archive. Regards Mike ======================================================================= This e-mail is intended for the person it is addressed to only. The information contained in it may be confidential and/or protected by law. If you are not the intended recipient of this message, you must not make any use of this information, or copy or show it to any person. Please contact us immediately to tell us that you have received this e-mail, and return the original to us. Any use, forwarding, printing or copying of this message is strictly prohibited. No part of this message can be considered a request for goods or services. ======================================================================= Any questions about Bookham's E-Mail service should be directed to postmaster@bookham.com. -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu