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

Transmission SPI

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



Joined: 19 Jan 2012
Posts: 6

View user's profile Send private message

Transmission SPI
PostPosted: Wed Jun 06, 2012 12:54 am     Reply with quote

Hi all !
I have a little problem with my SPI transmission. I have a master and a slave. I have to make a program for two. The master send a value and the slave receive.
Problem. My master send 30, and my slave in the LCD write 252. I need help. If you want, I can make my two programs.

Thanks
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Wed Jun 06, 2012 1:06 am     Reply with quote

Please see the second sticky message at the top of this forum. (PIC101)

Then come back and post the needed information (as listed there.)

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
kagalou



Joined: 19 Jan 2012
Posts: 6

View user's profile Send private message

PostPosted: Wed Jun 06, 2012 1:17 am     Reply with quote

Humm. I not sure I have all understand. But I test :p

So my slave is a 18F2580. For my project I need a SPI transmission.
So I make two program's with MPLAB.

For the slave :
Code:

#include <18F2580.h>

#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)
#use delay (clock=4000000)
#use SPI(DI=PIN_C4, DO=PIN_C5, CLK=PIN_C3, BITS=10)

#include "LCD Picdem2.C"

int a;

#int_SSP

void SSP_isr(void)
{
while (!spi_data_is_in());
a=spi_read();
}

/*/ Partie LCD/*/

void main ()
{
int a;

enable_interrupts(INT_SSP); 
enable_interrupts(GLOBAL);
lcd_init();
setup_spi (spi_slave| SPI_H_TO_L|SPI_CLK_DIV_16);

while(true)
   {
   lcd_gotoxy(1,1);
   PRINTF(LCD_PUTC,"Env master %u",a);
 }

}


My master is a 18F4580:
Code:

#include <18F4580.h>

#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
#define SS PIN_A5
#use delay (clock=4000000)
#use SPI(DI=PIN_C4, DO=PIN_C5, CLK=PIN_C3, BITS=10)

#include "LCD Picdem2.C"

int a=100;
int y=0,x=0;
int16 toto;

void main(void)
{
set_tris_b(0b11111110);
setup_spi (spi_master|SPI_L_TO_H|SPI_CLK_DIV_16);

lcd_init();

/*/BOUCLE/*/

while(true)
   {
   output_low(SS);
   spi_write(a);
   delay_ms(20);
   output_high(SS);

/*/ Partie LCD/*/

   LCD_PUTC ("\f");
   lcd_gotoxy(1,1);
   PRINTF(LCD_PUTC,"Envoi %u" a);
  }
}

So. The problem is the next. My slave receive the value "a" but the master send 100 and my slave receive 252, or 171 if it want.
Have you an idea ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Wed Jun 06, 2012 1:40 am     Reply with quote

Several things:

First INT_SSP, occurs _because_ a character has been received. You don't want/need to test if a character is available in the ISR. Just read it.

Second, the clock edges used by master and slave, _must_ be the same You are telling the slave to use the opposite edge of the clock from the master.....

Third, setup the SPI, _before_ enabling the interrupts.

Fourth, you _must not_ specify a clock rate in the slave. It is the master that controls the clock.

Fifth, you need some sort of signal to say to the main code in the slave that a character has been received. Otherwise you will keep displaying a character even if a new one has not arrived.

Best Wishes
ckielstra



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

View user's profile Send private message

PostPosted: Wed Jun 06, 2012 8:20 am     Reply with quote

6) Do Not mix the #USE SPI and setup_spi methods! The setup_spi command is an old mechanism in the CCS compiler which has proved to be working. The #USE SPI method is newer and has many more options. Choose one method and never mix them as that will lead to errors.

7) You don't have to setup the TRIS registers, the CCS compiler will do this for you. Only in special situations where you need speed or memory optimization you should set the TRIS registers yourself, but then you also need to define #USE FAST_IO otherwise the compiler will change your settings anyway.

8) Spi_setup should be the same in the master and slave. Now they are using different spi mode configurations and that is never going to work.

9) You have SPI_MODE_x defines in your code. Nice! But you never use them so now it is only garbage code.
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