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

how to start with spi communication?

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



Joined: 22 Nov 2011
Posts: 32
Location: puducherry

View user's profile Send private message Send e-mail

how to start with spi communication?
PostPosted: Tue Nov 22, 2011 12:37 am     Reply with quote

I am beginner to spi communication.

1. I don't know where I made mistake in the following program. (I have tried to send a bit from master to slave and display through slave).
2. I don't know how to connect wires in hardware part and what are all required.

Whether we have to write code for clock pulse generation or PIC do it on own?
plz explain from ABC....

Provide example code for the following cases if possible.

1. Master sends data. Slave receives and displays.
2. Master sends data. Slave re-send some data and master displays.
Rolling Eyes Rolling Eyes Rolling Eyes Rolling Eyes Rolling Eyes Rolling Eyes Rolling Eyes Rolling Eyes


MASTER CODE:
Code:

#include <18F4431.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

# define ssbar PIN_A0

// SPI mode definitions.
#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)

void main()
{
int data, result;

output_bit(PIN_A0,1);
delay_ms(3000);

setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_4);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_oscillator(False);

data=1;
delay_ms(3000);

output_bit(PIN_A0,0);
spi_write(data);
output_bit(PIN_A0,1);

delay_ms(3000);

output_bit(PIN_A0,0);
result=spi_read(0);
output_bit(PIN_A0,1);

printf("the added value is %x", result);
output_b(result);
}


SLAVE CODE:
Code:

#include <18F4431.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

int8 command;
int ret;
#byte SSPBUF= 0xFC9

// SPI modes for setup_spi() function.
#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)

#int_SSP
SSP_isr()
{
ret=0;
command=SSPBUF;

switch(command)
   {
   case 1:
   SSPBUF=1;
   break;

   default:
   SSPBUF=0;
   break;
   }


ret=SSPBUF;
}


void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_spi(SPI_SLAVE|SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
setup_oscillator(False);

output_bit(PIN_B0,ret);
delay_ms(30000);

}
Geps



Joined: 05 Jul 2010
Posts: 129

View user's profile Send private message

PostPosted: Tue Nov 22, 2011 3:15 am     Reply with quote

Go to the manual:
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf

And look at the SPI section on page 78.
gokulrred



Joined: 22 Nov 2011
Posts: 32
Location: puducherry

View user's profile Send private message Send e-mail

how to start with spi communication?
PostPosted: Tue Nov 22, 2011 3:33 am     Reply with quote

@Geps:

hi,
I saw that pdf.

I could be able to understand the commands to be used.

I don't know how to use those commands at which places.

If I could get a sample program which is ready to run, it'll be useful for me to understand.

Provide me some basic starter example progs if possible.
Geps



Joined: 05 Jul 2010
Posts: 129

View user's profile Send private message

PostPosted: Tue Nov 22, 2011 3:51 am     Reply with quote

Quote:
EX_SPI.C
Communicates with a serial EEPROM using the H/W SPI module
EX_SPI_SLAVE.C
How to use the PIC's MSSP peripheral as a SPI slave. This example will talk to the ex_spi.c example.


Located in your program directory
gokulrred



Joined: 22 Nov 2011
Posts: 32
Location: puducherry

View user's profile Send private message Send e-mail

PostPosted: Tue Nov 22, 2011 3:57 am     Reply with quote

Is there any mistakes in the program done by me?? will it run?

I don't have ppl to check whether thats right or wrong?
Geps



Joined: 05 Jul 2010
Posts: 129

View user's profile Send private message

PostPosted: Tue Nov 22, 2011 5:10 am     Reply with quote

Flash it to a real chip and give it a go......
ckielstra



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

View user's profile Send private message

PostPosted: Tue Nov 22, 2011 5:29 am     Reply with quote

Your program is not too bad but with computers it only works if there is no single bug left.

Here are some tips:
Code:
setup_oscillator(False);
You are using the external oscillator with fuse XT. A description of this function depends on the chips you are using and is described in the header files. I don't have the header files here but my guess is this function is doing nothing. Remove this line in both the master and sleve because it is confusing and making things more difficult to understand.

Code:
// SPI mode definitions.
#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)
CCS is using their own method for defining the SPI configuration that is very confusing. The defines here above are a more common method for describing the 4 possible SPI configurations. But... you are never using these defines... Why?

One bug is that you are using a different configuration for the master and slave. It is very important that both use the same SPI mode! Change the master to:
Code:
setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_4);
and the slave to:
Code:
setup_spi(SPI_SLAVE | SPI_SS_DISABLED | SPI_MODE_0);


Now, here we see another problem. For the slave you have configured SPI_SS_DISABLED. Doing so saves you one I/O pin but makes it very difficult to synchronize the master and slave. Just assume there is a noise induced spike on your clock line, then the slave will clock in one bit too many and from that moment on the master and slave will be out of sync by 1 bit. This can be corrected in software but will cause you a lot of trouble. The common solution is to enable the Slave Select pin. Then, when the master wants to send data it activates the Select signal, sends the data, and disables the Select line again. The Slave uses the enabling/disabling to detect the start of a new message and is garuanteed to be synchronized at the start of the message.

Code:
delay_ms(30000);

}
These are the last two lines of your slave code. After running for 30 seconds the slave will exit main(). What happens then, is that your program stops. The compiler inserted here a sleep instruction and execution stops. A more common practice is to have an endless while-loop in your program so the program never stops execution. Example:
Code:
void main()
{
  setup instructions
  ..
  while(TRUE)    // run forever
  {
    do your send / receive
  }
}

Or when all the important stuff is happening in interrupts you can have an empty loop at the end of your program:
Code:
void main()
{
  setup instructions
  ..
  while(TRUE)
  {
    // do nothing
  }
}


In the past several other discussions about master/slave communication have been going on. You can use the search function of this forum to find them, personally I find Google a better search tool when you add the term "site:ccsinfo.com/forum"

The already mentioned CCS examples are another good source of information.

For the rest I would say: experiment, make errors and learn.
gokulrred



Joined: 22 Nov 2011
Posts: 32
Location: puducherry

View user's profile Send private message Send e-mail

PostPosted: Wed Nov 23, 2011 12:54 am     Reply with quote

Thank you ckielstra for the detailed reply.

I have gone through the reply.
Still I have doubts.

About clock:
Whether we have to generate clock in prog????
What are all the header files we have to include.????

Since am very beginner to all CCS, PIC, SPI I do not know how to proceed.

I understand the concept of SPI but while coming to programming I lag very very very much.

I do not know which bits has to be set and which has to be reset.

and more....

Can you please optimise the code such that it it ready to run ?

I have divided it to the following step by step targets.
1. Send one bit data from master to slave and display at slave port.
2. Send 8 bit data from master to slave and display at slave
3. Send 8 bit data from master to slave... receive some 8 bit data from slave and display at master.

As a first step I wanted to do the target 1.
Plz help me in this.
I'll try by myself for the rest of the targets.

If possible plz post the (ready to burn) code for the target 1.
Geps



Joined: 05 Jul 2010
Posts: 129

View user's profile Send private message

PostPosted: Wed Nov 23, 2011 3:04 am     Reply with quote

What do you think the files I pointed you to do exactly?
gokulrred



Joined: 22 Nov 2011
Posts: 32
Location: puducherry

View user's profile Send private message Send e-mail

PostPosted: Wed Nov 23, 2011 3:50 am     Reply with quote

@geps

it involves 2 programs with EEPROM

so its little bit confusing when going to PIC
so oly...
Geps



Joined: 05 Jul 2010
Posts: 129

View user's profile Send private message

PostPosted: Wed Nov 23, 2011 3:54 am     Reply with quote

The second file simulates an EEPROM. IE it takes the values from the master, stores it, and then allows the master to fetch the data.

Change the storing to printing and you've got what you asked for.
gokulrred



Joined: 22 Nov 2011
Posts: 32
Location: puducherry

View user's profile Send private message Send e-mail

PostPosted: Wed Nov 23, 2011 4:32 am     Reply with quote

I got the output for the target 1... Laughing Laughing Laughing Laughing Laughing Laughing Laughing

Thanks for the support.

Now working on the next.

I'll ask if I have doubts.

Thank you thank you very much.....
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