View previous topic :: View next topic |
Author |
Message |
ck
Joined: 02 May 2012 Posts: 18
|
SPI Problem |
Posted: Wed Feb 20, 2013 10:19 am |
|
|
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
|
|
Posted: Wed Feb 20, 2013 11:18 am |
|
|
Anybody can give a stupid simple spi communication code between 2 PIC.
Thanks in advantage |
|
|
ck
Joined: 02 May 2012 Posts: 18
|
|
Posted: Wed Feb 20, 2013 11:37 am |
|
|
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
|
|
Posted: Wed Feb 20, 2013 12:22 pm |
|
|
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:
....
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: 19499
|
|
Posted: Wed Feb 20, 2013 12:24 pm |
|
|
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
|
|
Posted: Wed Feb 20, 2013 2:22 pm |
|
|
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
|
|
Posted: Thu Feb 21, 2013 3:25 am |
|
|
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
|
|
Posted: Thu Feb 21, 2013 6:42 am |
|
|
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 |
|
|
|