Software SPI Interface

I have been trying to interface a sensor that uses an SPI interface to the RC. I know the actual SPI bus on the PICs is used by IFI - this is how the two PICs communicate. However, C18 has a set of “Software SPI” functions that use digital input pins to emulate SPI (C18 Library Manual, p.121).

I looked at the default values for the SPI pins in the manual. They appear to correspond to digital inputs 1, 2, 5, and 6 on the RC. I connected the sensor to these pins, included the sw_spi.h header in the project, and tried to talk to the sensor.

For some reason I cannot get the data out of the sensor. I either get all 1, or all 0 in a byte :slight_smile: No real data.

I am suspecting a conflict with the default FIRST code. The function that initializes the SPI, OpenSWSPI, is supposed to set up the pins correctly. Could it be that the FIRST library is conflicting with this? I tried to remove all lines that referenced the pins I am using from the default code. Didnt help.

Also for some reason if I change the pin assignment in sw_spi.h in the mcc18 folder the program does not build properly. Do I have to recompile something or include any other files in my project?

Did anyone use the software SPI functions? Or the software I2C which should be close to what I am doing?

I have no idea what SPI is, but I’ll try to give you some general pointers passed on your message.

Is there any docs on using SPI and how to connect a SPI sensor?
Is the hardware wired for this?
Did you check for semi-colons and the like? One of my probs
Did you wire the sensor wrong?
Is the sensor bad?
Where is the Actual code for the functions? if it is in a lib, I wouldn’t mess with it at this time (get it to work first, then check Editing Main.c for advanced stuff)
Could it be that it’s working, but not as you expected?
Are aliens skrewing around with it wile your not looking? :slight_smile:

SPI? I haven’t the faintest either. Could you clarify that for both of us?

Or the gremlins that hide under carpet. They always get us. :slight_smile:

Where? Google? What does it stand for?


I went to Google. You know how many things SPI stands for?!?!?!?!?!?!?


Is SPI a company, or a format? Or both?

[edit]Think I have it. notitle

Well, it ain’t ours! They’re under lock and key, I think… :wink:

serial peripheral interface. Don’t know how to interface it though.

For those who are wondering what SPI is:

Sorry, I don’t have any experience with SPI on a PIC.

Thank you for conferming my find. I just read through it, And it sounds pretty nifty. Tell us how you figure it out.

I have another question. Which PIC controls the digital IO pins on the robot controller - master or the user PIC? In other words, is it possible to update data on those pins without calling Putdata? Or does it only update the state of the digital outouts when putdata is called?

The Digital I/O pins are all controlled by the User Controller (Putdata() is not required.)

If you want some help your going to have to post some code, what sensor your are using and to what pins it is wired.

I have never used the c compiler SPI functions, but I have done firmware Serial Port Interface in assembly on a pic16 series processor. I used it to connect to a digital pot. Depending on what you are doing, my advice is to simply write the software explicitly. SPI is a very simple protocol. The meat of spi is between clock and data pin. Check whatever you are connecting to, if it reads the data pin when your clock pin equals 1, the rise of the clock or cycle, then set what the next bit is when you toggle the clock to 0. Toggle the clock pin 16 times, 8 toggles to value 0 and 8 to value 1, to transmit a byte.

Make a function to do a byte at a time. It shouldn’t take more than a half hour to set this up. Most intermediate programmers can do it in several minutes for simpler SPI interfaces.

There is normally a select pin to indicate when the device is reading. Bring that low, set it to zero, for the duration of the communication. Assuming I didn’t forget anything critical, it’s been a while, that should all there is to most SPI communications.

Thats exactly what I am planning to do now - write my own functions :slight_smile: It would have been nice to get the pre-written ones working (they should be doing the same thing anyway), but writing my own should not be hard. I’ll give it a try in a couple of days (as soon as I finish some school assignments :))and see how it works out.