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

Help!! SPI conn between 2 PICs

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








Help!! SPI conn between 2 PICs
PostPosted: Wed Aug 23, 2006 6:46 pm     Reply with quote

I am working on a SPI communication between 2 PICs

18F452 is Master
16F873 is Slave

I have the master code working, but I can not get the slave to work. I know master code is working because I have an Assembler code per MICROCHIP SPI tutorial in the slave. Confirmed that work, but I want to have it in CCS C instead of assembler.

How to get the data from the master?

CAn any body helP?

Here is my code:





Slave Code:

Code:

/*
//*********************
Example code demonstrates SPI connection between 2 PICs (18F452
and 16F876).
Master sends data (a counter), Slave receives and outputs to
LEDs on PORTB

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


#include <16F873.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) 

int data;

void main()
{
   
   //Ports Initialization
   set_tris_a(0x3F);            // PORTA to inputs
   set_tris_b(0x00);            // PORTB to outputs
   set_tris_c(0x18);            //Set SDO to output, SCK &  SDI to input


   setup_ADC_ports(NO_ANALOGS);   //Turn off A/D ports, all
                           //porta will be use as digital   
   
   //SPI configuration
   setup_spi(spi_slave | spi_h_to_l | spi_clk_div_16);


   while(1)
   {
         
      
      // Test to see if data transfer complete?
      if(!input(PIN_A5));

      //Get data
      data = spi_read();
      

      output_b(data);

   


   }

}



Here is the Master Code:


Code:

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

Example code demonstrates SPI connection between 2 PICs (18F452
and 16F876).
Master sends data (a counter), Slave receives and outputs to
LEDs on PORTB

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


#include <18F452.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) 


//Defined Chip Select PIN
#define SS   PIN_A2

//Global Variables
int counter,data;

void main()
{
   //Reset counter to 0
   counter = 0;

   //Ports Initialization
   set_tris_a(0x00);            // PORTA to outputs
   set_tris_c(0x10);            //Set SCK & SDO to output & SDI to input


   setup_ADC_ports(NO_ANALOGS);   //Turn off A/D ports, all
                           //porta will be use as digital   
   
   //SPI configuration
   setup_spi(spi_master | spi_h_to_l | spi_clk_div_16);


   while(1)
   {
      //Enable Chip Select pin (LOW)
      output_low(SS);

      //Send Data (counter) to Slave      
      spi_write(counter);

      // Test to see if data transfer complete?
      while(!spi_data_is_in());

      //get data (does not use at this time    
      data=spi_read();

      //Disable Chip Select PIN (HIGH)   
      output_high(SS);   
      
      //Update counter
      counter ++;

      //Delay routine so human eyes can see the flash
      Delay_ms(100);


   }

}


Please help...
Guest








PostPosted: Wed Aug 23, 2006 7:03 pm     Reply with quote

By the way just to clear, the comment said that I use the 876 but actually

I do use the 16F873 as the slave
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 23, 2006 7:14 pm     Reply with quote

Sample code for SPI slave:
http://www.ccsinfo.com/forum/viewtopic.php?t=26888
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