CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

SPI problem (not even clock) with PIC18F452

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Nora



Joined: 23 Mar 2008
Posts: 50

View user's profile Send private message

SPI problem (not even clock) with PIC18F452
PostPosted: Sat Feb 19, 2011 1:15 pm     Reply with quote

Hello-
I am trying to get SPI communications working with a PIC18F452. I do not even get clock. My oscilloscope is able to read 10MHz signal (on slave transceiver) but seems to stop the chip's operation (I have a blinking LED) when I put it to read on the PIC's 20 MHz external oscillator.

Therefore, I used the SPI_CLK_DIV_64, hoping that this means that the SPI clock signal is 20 MHZ / 64 = 312 KHz... so if I am understanding this correctly, I should be able to read the clock signal.

I am reading all over this forum that it is best not to use both setup_SPI and #use spi at the same time. True?

I'm using the pins that the PIC says to use for SPI : C3 = SCK, CC4 = SDI, C5 = SDO
When I uncomment the #use spi, the word "DO" goes navy blue.
???

SPI_Write() can only put out 8 bits? What if I need to send 16 bits all at once? Should I use the SPI_XMIT function? Does it send LSB or MSB first?

I guess the biggest problem is no clock signal. So I can't even get to the rest to troubleshoot. Hoping there is just some simple setting that I got wrong. I did not do anything with the SPI pins (ie: no extra hardware).
Here's the code. All I am trying to do at this point is get a clock output.
Thanks in advance,
Nora

Code:

#include <18F452.H>
#fuses HS,NOWDT,NOPROTECT,BROWNOUT   
#use delay(clock=20000000)                         // one instruction=0.2us
#include <stdlib.h>
//#include <math.h>
//#use spi(Di=PIN_C4, Do=PIN_C5, CLK=PIN_C3)
//BITS = 16, BAUD = 9600,

#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1  (SPI_L_TO_H)
#define SPI_MODE_2  (SPI_H_TO_L)
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)

//***********************************************************

void main()
{
//set_tris_c(0x00);
// sets transmitter up as master (master generates clock), low to high clock transmission
setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_64);

while(1){
   //spi_write(0b1000000010110000); //
   //spi_write(0x38);
   //delay_ms(100);
           output_low(PIN_A5);
           delay_ms(1000);
           output_high(Pin_A5);
           delay_ms(1000);
       }
 }
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Feb 19, 2011 1:25 pm     Reply with quote

Run a simple test program. Then look at Pin C3 for the SPI clock.
Example:
Code:

#include <18F452.h>
#fuses HS, NOWDT, PUT, BROWNOUT, NOLVP   
#use delay(clock = 20000000)   

#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1  (SPI_L_TO_H)
#define SPI_MODE_2  (SPI_H_TO_L)
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)

//========================================
void main()
{

setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_64);

while(1)
  {
   spi_write(0x55);
   delay_us(100);
  }

}

Don't run it in Debug mode. Don't attempt to step through the code.
Compile and run it in Release mode. You should see the SPI clock.
Nora



Joined: 23 Mar 2008
Posts: 50

View user's profile Send private message

PostPosted: Sat Feb 19, 2011 1:33 pm     Reply with quote

I DO see the clock.
Thanks!
Ttelmah



Joined: 11 Mar 2010
Posts: 19505

View user's profile Send private message

PostPosted: Sat Feb 19, 2011 4:17 pm     Reply with quote

And, on sending 16bits, just send two lots of 8bits. The _hardware_ only sends 8bits at a time, and if you load a new byte immediately, the transaction is effectively 'non stop' (which shouldn't matter anyway with SPI - this is why it sends a clock...).

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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