Birdasaur
Joined: 07 Oct 2003 Posts: 29
|
Here is the code that works like a dream! |
Posted: Fri Dec 12, 2003 6:57 am |
|
|
//master
setup_spi(spi_master | spi_l_to_h | spi_clk_div_16 );
delay_ms(100);
spi_write(byte1); //to wake up the slave
byte1 = spi_read(); //to read in what the slave has to say
delay_ms(1); //delay for slave vectoring
byte2 = spi_read(byte2); //start swapping bytes
delay_ms(1); //way more than I needed
byte3 = spi_read(byte3);
delay_ms(1);
//slave
setup_spi(spi_slave | spi_l_to_h | spi_clk_div_16 );
while(1)
{
spi_write(byte1); //the trick was to load the buffer ahead of time
if(spi_data_is_in()) //becareful with this, it sucks up clocks
{
byte1 = spi_read();
byte2 = spi_read(byte2);
byte3 = spi_read(byte3);
}
}
The delays in between the master spi_read() calls are needed, however 1 ms is FOREVER. I haven't bothered to experiment with reducing that but I'm sure a delay_us(50) or something like that would be more than sufficient. Thanks for all your help and good luck guys.
BTW... I didn't use any interrupts. I had the slave CS grounded at all times. To test this, I logged an overnight while(1) loop of this code, with a 1 second delay between packets, and it was flawless. |
|