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

PIC to PIC SPI (again!)

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



Joined: 28 Nov 2003
Posts: 39
Location: Essex UK

View user's profile Send private message

PIC to PIC SPI (again!)
PostPosted: Mon Nov 08, 2004 1:49 pm     Reply with quote

The following code follows suggestion in previous posts.
PCWH version 3.212 PIC18F452 at both ends.

Master
snippet - data clock and SS drive all present on scope.

Slave - stripped down to bare essentials
For each SS low period from the master only the first byte of the 6 sent generates an SSP interrupt at the slave. Using SS select and interrupt driven reciever.

MASTER SNIPPET

Code:

write_to_slave(byte dat)
{
   spi_write(dat);
   delay_ms(2); 
}

// Output a few bytes
send_spi()
{
   setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_64 |
                                                                    SS_DISABLED);
   delay_ms(1);
   SPISSOUT = 0; // Select slave
   delay_ms(1);

   write_to_slave(0x30);
   write_to_slave(0x31);
   write_to_slave(0x32);
   write_to_slave(0x33);
   write_to_slave(0x34);
   write_to_slave(0x35);
   write_to_slave('\r');
   SPISSOUT = 1; // Deselect slave
}


SLAVE CODE

Code:

#include <18F452.h>
#device ADC=10
#fuses  H4,NOPROTECT,PUT,WDT,WDT64,NOLVP,BROWNOUT,BORV45
#priority SSP

#byte PORTA = 0xF80
#byte PORTB = 0xF81
#byte PORTC = 0xF82
#byte PORTD = 0xF83
#byte PORTE = 0xF84
#byte SSPBUF = 0xFC9

#use delay(clock=40000000, RESTART_WDT)
#use rs232(baud=19200,xmit=PIN_C6,rcv=PIN_C7,errors)

#zero_ram

#define IDDRA         0b00100000   // SS inpu
#define IDDRB         0b00000010
#define IDDRC         0b10011000
#define IDDRD         0b11111111
#define IDDRE         0b00000000

#define IPORTA        0b00100000   // SS high
#define IPORTB        0b00000000
#define IPORTC        0b10000010
#define IPORTD        0b00000000
#define IPORTE         0b00000000

spi_init()
{
   setup_spi(SPI_SLAVE | SPI_H_TO_L);
   enable_interrupts(INT_SSP);
}

// Receive SPI
#INT_SSP
spi_isr()
{
char spi_dat;
   output_low(PIN_C1);
   spi_dat = spi_read();  // Get the data
   delay_cycles(40); // Need a short delay to see pulse on scope
   output_high(PIN_C1); // Scope to see ints
}

void main(void)
{
char k;

   restart_WDT();

   PORTA = IPORTA;
   PORTB = IPORTB;
   PORTC = IPORTC;
   PORTD = IPORTD;
   PORTE = IPORTE;

   set_tris_a(IDDRA);
   set_tris_b(IDDRB);
   set_tris_c(IDDRC);
   set_tris_d(IDDRD);
   set_tris_e(IDDRE);

   printf("Start\r");

   spi_init();
   enable_interrupts(GLOBAL);

   while (1)
      {
      restart_WDT();
      }
}


Any Ideas?
This should be easy, SPI works brilliantly with loads of external devices.
Why is it so hard with 2 PICs?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Nov 08, 2004 2:10 pm     Reply with quote

http://www.ccsinfo.com/forum/viewtopic.php?t=328&highlight=intssp+spi+i2c
Kieran



Joined: 28 Nov 2003
Posts: 39
Location: Essex UK

View user's profile Send private message

SPI receive problems
PostPosted: Mon Nov 08, 2004 3:33 pm     Reply with quote

Thanks PCM programmer
I missed that post.

I have just tried replacing

spi_dat = spi_read(); // Get the data

with

spi_dat = SSPBUF;

No change, I thought these were functionally the same.
Doesn't spi_read wait only if you pass data in e.g. spi_read(0x55).
Kieran
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