Kieran
Joined: 28 Nov 2003 Posts: 39 Location: Essex UK
|
PIC to PIC SPI (again!) |
Posted: Mon Nov 08, 2004 1:49 pm |
|
|
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? |
|