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 support@ccsinfo.com

SPI for PIC18f4520

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



Joined: 05 Feb 2008
Posts: 1

View user's profile Send private message

SPI for PIC18f4520
PostPosted: Tue Feb 05, 2008 11:00 pm     Reply with quote

We're trying to hook up two PICs through SPI.
We've connected:

MASTER RC5 --> SlAVE RC4
MASTER RC4 --> SLAVE RC4
MASTER CLK (PIN RC3, SCK)--> SLAVE CLK
SLAVE SELECT --> logic low (so that PIC2 is always the slave)

our initial code for MASTER:

Code:
#include <18f4520.h>
#device high_ints=TRUE        // this allows raised priority interrupts, which we need
#DEVICE ADC=10                   // set ADC to 10 bit accuracy, or it could be just 8
#fuses HS,NOLVP,NOWDT,NOPROTECT
#use delay(clock=20000000)
#use spi(MASTER)

int value_send = 0;
int value_read = 0;


void main()
{
setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_CLK_DIV_16);


 
     
     While(TRUE){
   
      spi_write(6);
   
      output_d(6); // leds light up to show what it sent
     
   
      value_read = spi_read(0);
   
     }
}


Our initial code for the SLAVE:

Code:
#include <18f4520.h>
#device high_ints=TRUE        // this allows raised priority interrupts, which we need
#DEVICE ADC=10                   // set ADC to 10 bit accuracy, or it could be just 8
#fuses HS,NOLVP,NOWDT,NOPROTECT
#use delay(clock=20000000)
#use spi (SLAVE)

int recieve_buf = 0;

void main(void){
 
 
   setup_spi(SPI_SLAVE | SPI_H_TO_L);
 
 
  while(TRUE){
   recieve_buf = 0;
 
 
   if(spi_data_is_in())
   recieve_buf = spi_read();
 
 

   output_d(recieve_buf);
 
 
  spi_write(0);
 
}
}


Nothing is working. We can't get any communication going.
Thanks
Foppie



Joined: 16 Sep 2005
Posts: 138
Location: The Netherlands

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Wed Feb 06, 2008 1:33 am     Reply with quote

I think you should connect the Slave Select Pin to a output pin of the master. Then, when the master starts communication, the master should first take this pin low, and after that send the spi data and at last make the slave select pin high again.

The slave select pin is not used to define a device as slave (what is done in software), but to say that the slave is active.
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