|
|
View previous topic :: View next topic |
Author |
Message |
custom_elect
Joined: 15 Dec 2007 Posts: 13
|
#USE SPI(...) - Confusion |
Posted: Fri Dec 28, 2007 6:28 am |
|
|
Im a bit confused about the use of this directive, as you seem to be able to use SPI without it. (For example, most of the SPI samples Ive seen here dont use it).
Am I right in thinking that if you want a software SPI emulation then you "#Use SPI", but if you want to use the hardware you use "Setup_spi(...)"?
If you do #USE SPI and specify the "SPI1" option, does it only specify the I/O to be used, or does it force the use of the first hardware MSSP?
Also if you #USE SPI and specify the "FORCE_HW" option together with "BITS=16" (as per the 2nd example given in the help), does it generate multiple 8 bit transfers through the hardware?
Any help greatly appreciated. |
|
|
drh
Joined: 12 Jul 2004 Posts: 192 Location: Hemet, California USA
|
|
Posted: Fri Dec 28, 2007 9:08 am |
|
|
If you want to use the hardware SPI with the SPI library functions, you put FORCE_HW in the #use SPI directive. From page 113 of the reference manual. _________________ David |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Dec 28, 2007 1:19 pm |
|
|
Quote: |
If you do #USE SPI and specify the "SPI1" option, does it only specify
the I/O to be used, or does it force the use of the first hardware MSSP? |
You can find the answers from the .LST file. Make a small test program
and compile it. Example:
Code: |
#include <18F452.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use spi(SPI1, MASTER, BAUD=1000000, BITS=16, ENABLE=PIN_C0)
//==================================
void main()
{
spi_xfer(0x1234);
while(1);
}
|
Here's the part of the .LST file for the spi_xfer() function.
The compiler puts the 0x1234 data into two temporary RAM
locations and then it jumps to the #use spi library code.
Code: |
... spi_xfer(0x1234);
004E: MOVLW 12 // MSB of data
0050: MOVWF 07
0052: MOVLW 34 // LSB of data
0054: MOVWF 06
0056: MOVLW 10
0058: MOVWF 08
005A: BRA 0004 // Jump to SPI library code.
|
Here's the SPI library code:
Code: |
...#use spi(SPI1, MASTER, BAUD=1000000, BITS=16, ENABLE=PIN_C0)
0004: BCF F8B.0 // Set pin C0 = 0 (SPI Chip Select)
0006: MOVF FC9,W // Read SSPBUF to clear the BF flag
0008: MOVFF 06,FC9 // Write the LSB to the SSPBUF register
000C: BTFSS FC7.0 // Wait in a loop until BF goes high.
000E: BRA 000C // When it does, the byte has been sent.
0010: MOVFF FC9,01 // Read SSPBUF and put it in a temp var.
0014: MOVF FC9,W // Read SSPBUF to clear the BF flag.
0016: MOVFF 07,FC9 // Write the MSB to SSPBUF
001A: BTFSS FC7.0 // Wait in a loop until the MSB is sent.
001C: BRA 001A
001E: MOVFF FC9,02 // Read SSPBUF and put it in a temp var
0022: BSF F8B.0 // Set pin C0 = 1 (SPI chip select)
0024: GOTO 005C (RETURN)
|
Based on this, we can now answer your two questions.
Quote: |
If you do #USE SPI and specify the "SPI1" option, does it only specify
the I/O to be used, or does it force the use of the first hardware MSSP? |
It forces hardware SPI on the SPI pins.
Quote: |
Also if you #USE SPI and specify the "FORCE_HW" option together
with "BITS=16" (as per the 2nd example given in the help), does it
generate multiple 8 bit transfers through the hardware? |
Yes, it does two hardware transfers (one byte for each one).
The \CS pin is held low continuously until both transfers are done.
Then it's raised high again.
I compiled the program with vs. 4.064.
Note that you must use the spi_xfer() function to use the features
specified with the #use spi() statement. Don't use spi_write(). |
|
|
Guest
|
|
Posted: Fri Jan 04, 2008 9:48 pm |
|
|
Thanks so much for a very comprehensive reply.
I am still having trouble moving from the software SPI to hardware, and wondered if you could shed any light on the matter.
If I build and run your example (Im using 16F877A.h and PCM v4.063)
I dont get much of any sense on the clock and data lines. I only see two groups of 4 clocks. Though the clocks are the correct width, and the transfer happens nice and quickly (<20uS).
If I use a software SPI instead as follows:
#use spi (MASTER, CLOCK_HIGH=1, CLOCK_LOW=1, DI=PIN_C4, DO=PIN_C5, CLK=PIN_C3, MODE=0, BITS=16, IDLE=0, ENABLE_DELAY=1, MSB_FIRST)
I get a nice 16 bit clock pattern with the correct data around it, but its obviously slower at around 300uS.
I have a working application using the software SPI but want to use the hardware SPI for performance, if only I can get the right signals out of it. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jan 04, 2008 10:45 pm |
|
|
My purpose was just to answer your two questions. Not to do anything else. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|