SPI Basics

piclt

Member
I have PICAXE 18X and want to talk to a device using SPI 4 wire. I tried the spiin/spiout commands and gave me syntax error with this line. I then used the Shiftout_LSB routines in the manual, copied are they are in manual and PICAXE wired to suit but cannot get it to work. The prog downloads to PICAXE and prog seems to run but device not responding to SPI. I have downloaded the latest editor 5.1.4 and when I download to PICAXE tells me Firmware 8.6
Does the spiin/spiout only work on PICAXEs with dedicated SPI port?. Also the hspiout commands.

With the shiftout_LSB how is it possible to receive the shiftin_LSB data from the device at the same time? I was wanting to read the status of the device to see if it was receiving the shiftout_LSB command.

Also the manual routines seem to set the sdata bit then pulsout the sclk which would give a rising and falling clock edge and so clock in MOSI and clock out MISO ???. I would be grateful if someone could clarify what is meant to be happening, thanks
 

Tom2000

Senior Member
Peter Anderson published an app note showing how to manage a full-duplex SPI device. You can find it here: <A href='http://www.phanderson.com/picaxe/spi.html' Target=_Blank>External Web Link</a>

Tom
 

piclt

Member
Thanks for reply. I had read through these but decided to use the routines in the PICAXE manual as they gave options for LSB or MSB first and Pre Clock and Post Clock.
My device says it wants to receive data on MOSI pin clocked in on rising edge of SCLK and data is clocked out of MISO pin on the falling edge of SCLK with LSB first. The opcodes are 8 bit bytes and consist a command byte followed by one or more data bytes. I used the Shift0ut_LSBFirst routine.
The PICAXE prog seems to be running but I dont't know how the actual bits are going out to the device. I am confused with the Pulseout in the routines as this gives a rising and falling so a clock in and clock out ??. Does SPI not care about baud rate or SCLK speed??. Can I slow the whole thing down with pauses until I can actually see it happening?? and would it still work ??
 

demonicpicaxeguy

Senior Member
depending on how the spi bit banging routine was written and depending on the device used

when the data is sampled on say the rising clock edge it won't really matter where in the cycle it falls as long as it falls at some point,
some code uses high and low's some use pulseout's

pulseout is more convenient and makes the code a little cleaner to look at

the best way to see how it works is to stick in a few wait statements and wireup a couple of LEDs

Edited by - demonicpicaxeguy on 05/08/2007 13:53:57
 

piclt

Member
yes but......... If I shiftout of the 18X an 8 bit command to switch the device on, this would be shifted in to the device, I assume the device knows it has got 8 bits in the buffer and acts on it ??. This uses shiftout routine only. If I want to read the status of a register in the device I assume I need to shiftout from 18x a command to tell the device to send the data on its SPI output eg MISO. Then to get it I assume I would need 18X running a Shiftin routine at the same time ?? How ??. this is the bit I cannot seem to grasp ??.
 

Tom2000

Senior Member
To operate one of these devices full duplex, first refer to the data sheet for the device. Look at the timing diagram. Pay particular attention to the clock edges that are used when sending data from the device, and which edge is used to indicate that the device is clocking data out to the Picaxe.

The strategy is then to set the MOSI line to the desired data bit, send a transition on your clock lead, then read the incoming bit from the MISO line.

You need to understand how the device handles the clock transitions and write your bit banging code, or thoroughly understand the built-in routines, to make your read and write operations compatible with the device.

As I said, understanding and duplicating the timing diagram in the device's data sheet is the key. Until you do, you're shooting blind.

And whether or not you can slow the device down enough to watch the data interchange depends entirely on the device. If it's fully static, you should be able to slow it down as much as you like. Again, the data sheet is your guide.

Good luck!

Tom
 

Tom2000

Senior Member
Let me clarify a bit. (I was watching the Formula 1 race when I wrote that, so my attention was divided. Lewis Hamilton won another!)

Let's say that the device reads data from the MOSI line on positive-going clock transitions, and has the data available on the MISO line at the negative-going clock transition.

So, to both read and write the device, you'd follow this procedure:

1. Place a data bit on the MOSI line.

2. Send a clock pulse: low-to-high-to-low.

3. Read the data bit from the MISO input.

Repeat as necessary.

(Of course, you'd have to set the CS line to the proper condition before you begin reading/writing, and reset the CS when you finish. Depending upon the device, you might have to either lower or raise the CS line while you're transferring data.)

There. I hope that's less confusing than my previous entry.

Tom
 

piclt

Member
Thanks for your help, I got it working for shift out. At least I can talk to it now. I don't actually need to read the status registers of the device etc as long as it works. I only wanted to try that because I couldn't get it to work.
It was good advice to tell me to read the device manual &quot;carefully&quot;..... RTFM....! ! !
I still used the routines in the PICAXE manual.

It turned out I needed to start with:-
slave select (SS) = High
SCLK = High
MOSI = Low

Then set SS =low
Then set data byte to be sent
Gosub Shiftout_LSB
The pulsout here will now be doing High-Low-High
Return and get next byte to send
Gosub again etc
Then SS = High after last byte sent
Repeat again for next command etc
Start next command with SS = Low

I tried it with wait = 1 second after each bit sent and it still worked.
also works now with no pauses and pulsout = 1
...........................
 
Top