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 Problem 16F877A

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



Joined: 10 Feb 2007
Posts: 78

View user's profile Send private message Send e-mail Yahoo Messenger MSN Messenger

SPI Problem 16F877A
PostPosted: Tue Aug 05, 2008 1:15 pm     Reply with quote

Hi all,

I am trying to receive data from the SPI on the 16F877A via SDI pin. Although I am able to see the data being received on the pin via oscilloscope, I am not able to get the " spi_data_is_in() " to work.

Here is the simple code that I am trying to run

Code:

#include <16F877A.h>
#include <25640.h>

#fuses HS,NOLVP,NOWDT,PUT
#use delay(clock=20000000)
#use rs232(baud=4800,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8, STREAM=HOSTPC)

#include <stdio.h>
#include "lcd2.c"

#define TRUE 1

void main()
      {
      int8 data;
      lcd_init();
       setup_spi(SPI_SLAVE | SPI_H_TO_L | SPI_CLK_DIV_16);
     
      output_low(PIN_A1);//pic connected to DSP
     
       output_low(PIN_A0);//DSP connected to PIC
       while (1)
       {
       if(spi_data_is_in())
       {
       data=spi_read();
       lcd_putc(data);
       delay_ms(100);
       
       }
       
       
       }}


The data is coming from a DSP controller board, which is set as master.
Any suggestions?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Aug 05, 2008 1:23 pm     Reply with quote

The default is for \SS to be enabled on the PIC. You don't have it
disabled in your setup_spi() statement, so it is enabled. The SPI won't
work unless the \SS signal goes low during the SPI transaction.

Does the DSP have a \SS or \CS signal ? Normally an SPI master will
have one. Do you have it connected to the \SS pin on your PIC ?
tranz



Joined: 10 Feb 2007
Posts: 78

View user's profile Send private message Send e-mail Yahoo Messenger MSN Messenger

PostPosted: Tue Aug 05, 2008 1:43 pm     Reply with quote

Yes, the DSP controller is connected to the SS pin of the PIC, but unfortunately it selects the slave by pulling the pin high. :(

Any suggestions how to rectify the problem?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Aug 05, 2008 2:07 pm     Reply with quote

You could disable the slave select pin:
Quote:
setup_spi(SPI_SLAVE | SPI_H_TO_L | SPI_SS_DISABLED);


Also, slaves don't supply a clock. They don't need a divisor. You can
remove the divisor constant as I have shown above.

I assume you have checked the SPI mode used by the DSP. Do you
know for sure that that it uses Mode 2 ? That's what you're using.
Code:

#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)
tranz



Joined: 10 Feb 2007
Posts: 78

View user's profile Send private message Send e-mail Yahoo Messenger MSN Messenger

PostPosted: Tue Aug 05, 2008 2:23 pm     Reply with quote

I did the necessary changes as you had mentioned, the code is still skipping the like condition "spi_data_is_in()".
Since the DSP sends the data based on MSB first and LSB last, I presumed "SPI_H_TO_L" will do the trick.

But I also tested the other modes, its still skipping the data in condition.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Aug 05, 2008 2:27 pm     Reply with quote

That has nothing to do with the mode. The idle state of the clock and
the clock edge that samples incoming data are the two things that
determine the mode.
http://www.totalphase.com/support/kb/10045/#modes
http://elm-chan.org/docs/spi_e.html
Ttelmah
Guest







PostPosted: Tue Aug 05, 2008 2:48 pm     Reply with quote

Going to 'exceptional possibilities'. You mention data, but not the clock. spi_data_is_in, requires eight active transitions of the clock. It couldn't really care about the data...
Have you checked the voltages involved?. Both the clock line, and the data line, need to see voltages going below 1v, and above 4v (for a 5v supply). T former is 'easy', but a lot of logic outputs will not reliably supply the latter.

Best Wishes
tranz



Joined: 10 Feb 2007
Posts: 78

View user's profile Send private message Send e-mail Yahoo Messenger MSN Messenger

PostPosted: Tue Aug 05, 2008 6:14 pm     Reply with quote

Well this is what I see,

The DSP is programmed to send a series of 1's and 0's to the PIC, the clock always seems to be high. Even in normal communication, the DSP seems to send only high as clock pulse, I think that is where the problem lies.

I will debug it further and let you guys know with the results.

Thanks
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