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

how to transmit data in spi slave mode?
Goto page Previous  1, 2, 3, 4
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
nahumof



Joined: 09 Apr 2011
Posts: 15

View user's profile Send private message

PostPosted: Fri May 13, 2011 12:54 pm     Reply with quote

i really don't know the cause... but i try with all the mode configurations for master and slave and works with mode 3 for the slave and any mode for master!!! it just don't care!!!

master
#use spi (DO=PIN_D6,DI=PIN_D5,CLK=PIN_D4,MODE=3,BITS=8,MSB_FIRST)
mode 0 cause errors
mode=1 ok
mode=2 ok
mode=3 ok

slave
mode
setup_spi(SPI_SLAVE |SPI_MODE_0_0) return trash

setup_spi(SPI_SLAVE |SPI_MODE_0_1) return trash

setup_spi(SPI_SLAVE |SPI_MODE_1_0) also trash

setup_spi(SPI_SLAVE |SPI_MODE_1_1) WORKS!!! but it still send 1 byte delayed...



i still wonder what happens?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri May 13, 2011 12:59 pm     Reply with quote

Quote:
#use spi (DO=PIN_D6,DI=PIN_D5,CLK=PIN_D4,MODE=3,BITS=8,MSB_FIRST)

You're using software i2c with the #use spi() library. The library code
could have bugs.
nahumof



Joined: 09 Apr 2011
Posts: 15

View user's profile Send private message

PostPosted: Fri May 13, 2011 3:41 pm     Reply with quote

i guess that´s it...

i´m using SPI with software spi for master using #use spi() library and using SPI HW for slave using setup_spi(SPI_SLAVE |SPI_MODE_1_1); ....
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

PostPosted: Wed Jul 20, 2011 6:49 am     Reply with quote

I have a problem. I'm using the program from the first post. I'm using mode 0 and only 2 chips. The pins are connected correctly, but the master recieves 1 bit less- if i send 2(10) he receive - 1 (01). Only the last bit is lost. Here is the code I'm using:
Master:

Code:
#include <18F4520.h>
#fuses HS,NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=12000000)

#use spi(master)
#define SPI_SS   PIN_D7
#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)

int transmit(int data)
{
int result;
output_low(SPI_SS);
spi_write(data);  // Send command to slave
output_high(SPI_SS);

delay_ms(1);

output_low(SPI_SS);
result = spi_read(0); 
delay_ms(1);   // Read response from slave
output_high(SPI_SS);
return result;
}

void main()
{
int result,d;
output_high(SPI_SS);
setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_4);

while(1){
result=transmit(51);

output_a(result);
delay_ms(1000);
output_a(0);
delay_ms(1000);

}
}


slave:
Code:

#include <18F4520.h>
#fuses HS,NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=12000000)

#use spi(SLAVE)
#byte SSPBUF = 0x0FC9
#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)

#int_ssp
void ssp_isr(void)
{
int8 comand,dop;
dop=read_eeprom(2);
comand=SSPBUF;

SSPBUF=dop;
write_eeprom(1,comand);
}

void main(){
int d,q;
setup_spi(spi_slave|SPI_L_TO_H | spi_clk_div_4);
output_high(PIN_E0);
clear_interrupt(INT_SSP);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);

output_d(0);
output_high(PIN_E0);
output_low(PIN_E2);
output_low(PIN_E1);
write_eeprom(2,20);

while(1)
  {
  d=read_eeprom(1);
  if(d!=0){
     output_d(d);
     delay_ms(500);
     output_d(0);

     write_eeprom(1,0);
     q=read_eeprom(2);
     q=q+2;
     write_eeprom(2,q);
     delay_ms(500);
    }
  }
}

This happens only in master's side. The slave receives everything correct.
Can you tell me where is the problem???
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

PostPosted: Wed Jul 20, 2011 8:28 am     Reply with quote

Can somebody post an example with software spi tranfer(i mean without using ISR)???
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jul 20, 2011 12:25 pm     Reply with quote

Quote:

#use spi(master)
setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_4);

These are two different methods of setting up the SPI module. Do not
mix the two methods. I suggest that you only use setup_spi().


Quote:
#use spi(SLAVE)
setup_spi(spi_slave|SPI_L_TO_H | spi_clk_div_4);

1. The same comment applies regarding mixing the two methods.
Delete the #use spi() line.
2. Also, don't change the case of constants. They should be kept in
UPPER CASE the same way they are given in the .h file.
3. Also, slaves don't use a clock divisor. The master supplies the clock.
Delete the clock divisor parameter.
4. Most importantly, you are using using a different SPI mode for the
slave. You must use the same SPI mode for both master and slave.

Quote:
I have a problem. I'm using the program form the first post.

You have massively changed the code from that post.

There are other problems besides these, especially regarding using
write_eeprom() inside the #int_ssp isr.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3, 4
Page 4 of 4

 
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