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 communication b/w master-slave PICs

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
dima2882



Joined: 13 Jul 2005
Posts: 25
Location: Maryland

View user's profile Send private message

SPI communication b/w master-slave PICs
PostPosted: Wed Jul 13, 2005 12:34 pm     Reply with quote

Hi all,
I set up a PIC16F88 to act as an 8-bit parallel port expander linked through SPI to the master PIC18F458. The F88 can read 8-bit parallel data from parallel hardware, such as a CPLD, which is what I used this for. The Master selects the slave, gets its data, and sends it to the PC over the serial port. In this example, all this happens in an infinite loop, but of course that will be changed to suit the particular application.

***PIC16F88 SLAVE SPI DEVICE CODE***
Code:
// Configure chip and compiler
#include "16F88.h"
#fuses INTRC_IO,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOMCLR
#use delay(clock=4000000)
#zero_ram
#byte PORTA = 5
// PORTA is a variable mapped to the 8 physical pins of port A of the PIC
#define   F88_CS   PIN_B5

void main(void)
{
   // Variables
   int F88data;

   // Configure pin direction
   #use fast_io(A)
   #use fast_io(B)
   set_tris_a(0b11111111);
   set_tris_b(0b00111010);
   //PORTA = 0;

   // Setup the SPI port
   setup_spi(SPI_SLAVE | SPI_L_TO_H | SPI_CLK_DIV_4);

   while(1)
   {
      if(input(F88_CS) == 0)
      {
         F88data = PORTA;
         spi_write(F88data);
      }   
      else
      {
         delay_cycles(1);
      }

   }
   return;
}


***PIC18F458 MASTER SPI DEVICE CODE***
Code:
// Configure chip and compiler
#include "18F458.h"
#fuses EC_IO,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP
#use delay(clock=16000000)
#use rs232(baud=57600, xmit=PIN_C6, rcv=PIN_C7)
#zero_ram

// Aliases for the pins used
#define   F88_Servo_CS   PIN_B5

#define     F88_Aquire_Time       100

// Function prototypes
int readF88(void);

void main(void)
{
   // Variables
   int data;

   // Configure pin direction, format is port X, pin 7 => pin 0; 1 is in, 0 is out
   #use fast_io(A)
   #use fast_io(B)
   #use fast_io(C)
   #use fast_io(D)
   #use fast_io(E)
   set_tris_a(0b00000000);
   set_tris_b(0b10000000);
   set_tris_c(0b10010000);
   set_tris_d(0b00000000);
   set_tris_e(0b00000000);

   // Initially set all SPI chip selects to disable
   output_high(F88_Servo_CS);         // F88 CS is active low

   // Setup the SPI port
   setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16);
   
                while(1)
                {
                           data = readF88();
                           printf("This is the data from F88: %d\n",data);
                           delay_us(100);
                }

   return;
}

int readF88(void)
{
   int F88data;

   output_low(F88_Servo_CS);                           // select the F88 to begin aquisition of data from external source
   delay_us(F88_Aquire_Time);                           // wait the prescribed aquisition delay
   F88data = spi_read(0);                              // aquire the data from the F88 by sending out a clock
   output_high(F88_Servo_CS);                           // disable the F88   

   return F88data;
}


Last edited by dima2882 on Fri Jul 21, 2006 6:41 am; edited 2 times in total
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Thu Jul 14, 2005 12:49 pm     Reply with quote

You can buy a chip that does this in hardware.
dima2882



Joined: 13 Jul 2005
Posts: 25
Location: Maryland

View user's profile Send private message

PostPosted: Thu Jul 14, 2005 1:56 pm     Reply with quote

Yes, that is true, in fact Microchip makes those also and it would be a better fit for this particular application. I was trying to go for the PIC-PIC communication angle, and SPI is a good fit for that. I2C would be even better, but there is a lot of I2C code in the forums already.
Breno



Joined: 20 Jul 2006
Posts: 5
Location: Belo Horizonte, Brazil

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

PostPosted: Thu Jul 20, 2006 2:01 pm     Reply with quote

Hi dima, this code is actually using PIC's SPI hardware, isn't it?

In the "PIC16F88 SLAVE SPI DEVICE CODE" you used a variable called F88_CS, what variable is that? I understand it is the pin that correspond to the Chip Select but it should be defined before, right? And it is connected to the PIN_B5 of the PIC Master, right?

Did you test it? Does it work?

Thanks Exclamation
dima2882



Joined: 13 Jul 2005
Posts: 25
Location: Maryland

View user's profile Send private message

PostPosted: Fri Jul 21, 2006 6:39 am     Reply with quote

Breno,
Your're right, F88_CS does indeed need to be declared. It is a variable equivalent to a pin on the F88 chip, of your choice. When I pasted the code in, I removed some lines that were application-specific, and must have deleted this one by accident:
Code:
#define   F88_CS      PIN_B5

This needs to be placed at the top of the file, where the other preprocessor commands are located. The code listing has been subsequently fixed. Yes, this code worked, as I ripped it out of an application.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library 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