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_read on slave with simple code not working

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



Joined: 21 Nov 2006
Posts: 129

View user's profile Send private message

spi_read on slave with simple code not working
PostPosted: Thu Jul 15, 2010 4:44 pm     Reply with quote

Hi,

I'm using 4.092 PCH. Two different PIC18's trying to do some very simple communications with SPI just to get it working.

The master does get the expected value from the slave (0x08). However, the value that the slave reads is not correct (should be 0x15). If in my diagnostic line, if I enter 0x00, then I get a match.

In the master, I try putting spi_read(0), but then nothing works.

Any ideas?! I've tried every possible combination. I'm wondering if it's a compiler problem.

Master:

Code:
#include <18f4685.h>

#fuses H4,WDT256,NOMCLR,NOWDT,NOBROWNOUT,BORV20,NOLVP
#fuses NOPROTECT,NOCPD,NOCPB,NOWRT,NOWRTD,NOWRTB,NOWRTC,NOEBTR,NOEBTRB

#use delay(clock=40000000)

// SPI mode definitions.
#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1  (SPI_L_TO_H)
#define SPI_MODE_2  (SPI_H_TO_L)
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)

#define SLAVE_SELECT PIN_D7

void main() {

     int8 return_value;
     
     setup_spi(SPI_MASTER | SPI_MODE_3 | SPI_CLK_DIV_64);

     while(1) {
          output_low(SLAVE_SELECT);
          spi_write(0x15);
          output_high(SLAVE_SELECT);

          output_low(SLAVE_SELECT);
          return_value = spi_read();
          output_high(SLAVE_SELECT);
         
          if(return_value == 0x08) output_high(PIN_C7);
          else output_low(PIN_C7);
         
          delay_ms(1);
     }
}


Slave:

Code:
#include <18f14k22.h>

#fuses INTRC,PLLEN,WDT256,NOMCLR,NOWDT,NOBROWNOUT,BORV22,NOLVP
#fuses NOPROTECT,NOCPD,NOCPB,NOWRT,NOWRTD,NOWRTB,NOWRTC,NOEBTR,NOEBTRB

#use delay(clock=64000000)

// SPI mode definitions.
#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1  (SPI_L_TO_H)
#define SPI_MODE_2  (SPI_H_TO_L)
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)


int8 rx_byte;

void main() {
     setup_oscillator(OSC_64MHZ);
     
     setup_spi(SPI_SLAVE | SPI_MODE_3 );

     while(1) {

          output_low(PIN_C0); // diagnostics
         
          if(spi_data_is_in()) {
               output_high(PIN_C0); // diagnostics
               
               rx_byte = spi_read();
               spi_write(0x08);
               
               if(rx_byte == 0x15) output_high(PIN_C1); // diagnostics
               else output_low(PIN_C1); // diagnostics
          }
         
     }
}
evsource



Joined: 21 Nov 2006
Posts: 129

View user's profile Send private message

PostPosted: Thu Jul 15, 2010 5:44 pm     Reply with quote

Just a couple more things I've discovered. Watching the signals on the scope, if send 0x0F from master to slave, I can see the four highest bits of 0x0F clocked in with the data line low, and then the last four bits clocked in with the data line low. The slave select line drops low just before the clock starts ticking. The slave select line goes high again right after the clock stops. I placed an output high to a pin right after the spi_data_is_in() call, and it goes high just a very small time after the slave select line goes high.

Why in the world is the slave reading zero instead of the value being sent??!! I looked at the list file to make sure it was reading from the correct register, which it is. I can't figure out where it would be getting the 0x00 from.


Last edited by evsource on Thu Jul 15, 2010 6:03 pm; edited 1 time in total
evsource



Joined: 21 Nov 2006
Posts: 129

View user's profile Send private message

PostPosted: Thu Jul 15, 2010 6:02 pm     Reply with quote

Just checked the errata for the 18F14K22, and there are SPI issues. However, they are all for master configuration. I'm using it in the slave mode.

The 18F4685's errata shows a small glitch that can happen on the SCK line. I don't think that's related to the slave receiving 0x00 when it's supposed to get 0x15.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Jul 15, 2010 11:34 pm     Reply with quote

The receive event should be signalled after one SPI frame has been clocked in, independent of the SS pin transition.
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