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

SPI slave sends only every other time - huh?

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



Joined: 23 Feb 2011
Posts: 21
Location: Oceanside, CA USA

View user's profile Send private message

SPI slave sends only every other time - huh?
PostPosted: Tue Mar 08, 2011 6:22 pm     Reply with quote

Hi Folks,

I'm new to CCS and the Microchips in general (more familiar with MSP430 stuff) and I have a situation where the SPI slave works but only every other time the master sends data. I'm using the PIC18F24K22 chip as the slave and the PICkit Serial dongle as the master. I'm using "if( spi_data_is_in()" to detect when the hardware SPI detects a character. This will work every other time I send data and not every time. Any ideas? Do I have to clear something?

Here's my code:

Code:

//  Include section
#include <18F24K22.h>
#fuses INTRC_IO,NOWDT,NOPROTECT,NOLVP
#use delay(clock=16M)

void main(void)
{
    int8 recd_char1;            // char received from master spi
    int8 recd_char2;            // char received from master spi
    int8 data = 0;
   
    set_tris_a (0x20);  // RA5 input, RA0-RA4, RA6, RA7 outputs
    set_tris_b (0x39);  // RB0, RB3, RB4, RB5 inputs, RB1, RB2, RB6, RB7  outputs
    set_tris_c (0x38);  // RC3, RC4 inputs, RC0-RC2, RC5-RC7 outputs       
    setup_spi(SPI_SLAVE|SPI_SCK_IDLE_HIGH|SPI_SAMPLE_AT_END|SPI_XMIT_H_TO_L);
 // sets SPI#1 as SLAVE, Idles high, data changes on falling edge, data stable on rising
   
    while (1) {
        if( spi_data_is_in() ) {    // <<- only works every other time
            recd_char1 = spi_read(data);
            recd_char2 = spi_read(data+1);
      }
        output_low(pin_A0);     // toggle LED_0 to show activity
        delay_ms(150);
        output_high(pin_A0);
        delay_ms(150);

        data++;
        if (data > 100) {
            data = 0;
        }
    } // end of while (1)
} // end of main()


Thanks,

Richard
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 08, 2011 7:11 pm     Reply with quote

Look at how they receive two bytes in this CCS example file:
Quote:

c:\program files\picc\examples\ex_spi_slave.c

Re-write your program to do it the same way.
rcooke



Joined: 23 Feb 2011
Posts: 21
Location: Oceanside, CA USA

View user's profile Send private message

PostPosted: Tue Mar 08, 2011 7:23 pm     Reply with quote

PCM, Thanks. It's working every time now but the loop isn't working properly. After reset the LED toggles like I planned but as soon as I send a char from the master the LED blinks once then nothing. As long as I send data from the master the LED will blink. Is there something to clear that allows the spi_data_is_in() function to work again?

Thanks,

RIchard
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 08, 2011 7:36 pm     Reply with quote

Post your latest program.
rcooke



Joined: 23 Feb 2011
Posts: 21
Location: Oceanside, CA USA

View user's profile Send private message

PostPosted: Tue Mar 08, 2011 7:43 pm     Reply with quote

OK here it is:

Code:

//  Include section
#include <18F24K22.h>
#fuses INTRC_IO,NOWDT,NOPROTECT,NOLVP
#use delay(clock=16M)

//****************************************************************************
//  Defines section
//****************************************************************************
#define PORTA_CONFIG    0x20    // RA5 as input, the rest as outputs
#define PORTB_CONFIG    0x39    // RB5, RB4, RB3, RB0 = inputs
#define PORTC_CONFIG    0x38    // RC5, RC4, RC3 = inputs

void main(void)
{
    int8 recd_char1;            // char received from master spi
    int8 recd_char2;            // char received from master spi
    int8 data = 0;
   
    set_tris_a (0x20);   // RA5 input, RA0-RA4, RA6, RA7 outputs
   set_tris_b (0x39);  // RB0, RB3, RB4, RB5 inputs, RB1, RB2, RB6, RB7 outputs
   set_tris_c (0x38);  // RC3, RC4 inputs, RC0-RC2, RC5-RC7 outputs
   
    setup_spi(SPI_SLAVE | SPI_SCK_IDLE_HIGH | SPI_SAMPLE_AT_END | SPI_XMIT_H_TO_L );
    // sets SPI#1 as SLAVE, Idles high, data changes on falling edge, data stable on rising
   
    while (1) {
        if( spi_data_is_in() ) {
            recd_char1 = spi_read();
            if( spi_data_is_in() );
            recd_char2 = spi_read();
            spi_write(data);
            spi_write(data+1);
        }
        output_low(pin_A0);     // toggle LED_0 to show activity
        delay_ms(150);
        output_high(pin_A0);
        delay_ms(150);

        data++;
        if (data > 100) {
            data = 0;
        }
    } // end of while (1)
} // end of main()
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 09, 2011 12:30 am     Reply with quote

My advice is, if you want to do two things at a time, then use the #int_ssp
interrupt and save the incoming bytes in a buffer. Example:
http://www.ccsinfo.com/forum/viewtopic.php?t=26888
Then you can blink an LED in main() without much worry about the SPI.
Just periodically check to see if there are any bytes in the buffer, and if
so, then handle them. Make the buffer large enough so that you won't
lose any data if you don't check it for a while.
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