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 Problem

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



Joined: 02 May 2012
Posts: 18

View user's profile Send private message

SPI Problem
PostPosted: Wed Feb 20, 2013 10:19 am     Reply with quote

Hi friends,
excuse me for disturb but i have spi communication problem between PIC18f4580 and PIC16f877A. I'm using ccs 4.134

Master Setting:
Code:

setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_64);


and in the main main:
Code:

output_low(CS_SLAVE);
spi_write(21); //Command
log_data=spi_read();
output_high(CS_SLAVE);


Slave Setting:
Code:

setup_spi(SPI_SLAVE|SPI_L_TO_H);


and in slave main:
Code:

#int_ssp
void spi_int(void){
 int command;
 command=spi_read();
 switch(command){
   case 21: spi_write(5);
     break;
   default:
     break;
 clear_interrupt(INT_SSP);
 }


void main(){
  enable_interrupts(INT_SSP);
  enable_interrups(GLOBAL);
  while(TRUE){}
}


what i read for log_data is 0 - 21 - 0 - 21 and so on (but never 5)
where i wrong?
Thanks in advantage
ck



Joined: 02 May 2012
Posts: 18

View user's profile Send private message

PostPosted: Wed Feb 20, 2013 11:18 am     Reply with quote

Anybody can give a stupid simple spi communication code between 2 PIC.
Thanks in advantage
ck



Joined: 02 May 2012
Posts: 18

View user's profile Send private message

PostPosted: Wed Feb 20, 2013 11:37 am     Reply with quote

Still
Main
Code:

output_low(CS_SLAVE);
spi_write(command);
log_data=spi_read(0);
output_high(CS_SLAVE);


and slave
Code:

if(spi_data_is_in()){
 command=spi_read();
 spi_write(data);
}


still not work

I'm using fast_io with set_tris_x
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Wed Feb 20, 2013 12:22 pm     Reply with quote

Dude, chill.

you posted your problem like an hour ago...

People here work and have their own lives, and are not obliged to help.

For SPI you need to have the right SPI Mode:

Courtesy of Mr. PCMProgrammer:
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)


Examample:
Code:
setup_spi(SPI_MASTER | SPI_MODE_1 | SPI_CLK_DIV_64);


Also remember that your SPI Master needs to Generate a Clock even when reading..
so you need to send a "dummy" byte..

Code:
MEM_VALUE=spi_read(DUMMY);    // read data


Where DUMMY:
Code:
#define DUMMY 0x00



....

my 2 Cents... others can join in
(When and if they have time)


G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
Ttelmah



Joined: 11 Mar 2010
Posts: 19540

View user's profile Send private message

PostPosted: Wed Feb 20, 2013 12:24 pm     Reply with quote

First thing, you are operating a select line on the master, connect it to the SS pin on the slave, and set the slave SPI to use this. SPI is vastly easier to do with a select. Without this you have problems, depending on the mode used, with the possibility of the slave getting unexpected clocks during boot, as the lines float before they are driven, and then things don't work. Other way is to wait in the slave for the line to go to the idle state, before you enable the slave.
Second thing, you need a pause between sending a byte, and reading the reply. When you send a byte, the buffer contains the byte you have just sent. The slave takes _time_ to enter the interrupt, and load the reply. Typically perhaps 50 instructions. Till this happens, you can only read back the sent byte.

Best Wishes
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Feb 20, 2013 2:22 pm     Reply with quote

ck wrote:
Anybody can give a stupid simple spi communication code between 2 PIC.
Easiest answer: look into the examples provided with your CCS compiler. A short description of the example programs can be found in the end the CCS manual, chapter "Example Programs". What you want to look at is called with the easy to guess names: ex_spi.c and ex_spi_slave.c

Several examples have been posted in this forum. You can find them using the search function of this forum, but personally I found Google to give better results. Try Google with a string like "site:ccsinfo.com/forum spi example" this limits the results to the CCS forum site.

Performing these actions will cost me similar time as it will take you to do. I don't see why I should do your work.
ck



Joined: 02 May 2012
Posts: 18

View user's profile Send private message

PostPosted: Thu Feb 21, 2013 3:25 am     Reply with quote

First, excuse me for my insistence.
I spend one day getting crazy trying to use spi. I know that all people that answer me also have work and have no time and interest to help me.

so i am really sorry for my chill, i hope you can give me help anymore.
I read your answer and 'lll try.

Thank you
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Thu Feb 21, 2013 6:42 am     Reply with quote

Quote:
I'm using fast_io with set_tris_x


Why?

Dont mess with Fast_io or tris unless you _need_ it.
for this, you dont.

Post your complete test code... fuses and everything...
its hard to tell what is wrong with partial information.

G
_________________
CCS PCM 5.078 & CCS PCH 5.093
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