Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

SPI COMMUNICATION Between two PIC (USING CCS)

Status
Not open for further replies.

maiyeu512

Newbie level 1
Joined
Aug 5, 2015
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
9
Referring to previous thread https://www.edaboard.com/threads/261292/


Code:
Master
////////////////////////////////////////////
unsigned int spi_write_hw(unsigned char val)
 {
    unsigned char ctrl;
    SS=0;
    SSPBUF=val;    while(!SSPIF);
    while(!BF);    ctrl=SSPBUF;
    SS=1;
    return ctrl; 
 }


Code:
slave
//////////////////////////////////
  void interrupt isr()
{
  if(SSPIF)
 {
   SSPIF=0;
   rcv=SSPBUF;  
   SSPBUF=snd;
 }
}

Please! I want your code full
 
Last edited by a moderator:

Hi

SPI & USART are same. If you made any usart communication program.
You should follow that proceeder.

Simple Exe.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <p18f2520.h>
void write_Str_SPI( const rom char *wrptr );     // Write string
unsigned char Write_Chr_SPI( unsigned char dt )
 
 
void Write_Str_SPI( const rom char *wrptr )
{
  while ( *wrptr )                // test for string null character
  {
     SSPBUF = *wrptr++;           // initiate SPI bus cycle
     while(SSPSTATbits.BF==0 );    // wait until 'BF' bit is set
  }
}
 
unsigned char WriteSPI( unsigned char dt)
{
  SSPBUF = dt;           // write byte to SSPBUF register
  if ( SSPCON1 & 0x80 )        // test if write collision occurred
   return ( -1 );              // if WCOL bit is set return negative #
  else
  {
    while( !SSPSTATbits.BF );  // wait until bus cycle complete 
  }
  return ( 0 );                // if WCOL bit is not set return non-negative#
}



For calling the subject: -

Code:
           write_Str_SPI("This is example");
           write_Chr_SPI('A');
 
Last edited by a moderator:

Hi
SPI & USART are same. If you made any usart communication program.
You should follow that proceeder.
I'm sorry but SPI and USART are most certainly NOT the same. SPI uses the MSSP peripheral which is quite different to the USART peripheral. Also the means of communication are different as are the hardware level protocols.
Referring back to the original question in the referred to thread (which is really a request to understand how SPI exchanges work) I suggest that you do a search for "SPI" as there are very many descriptions of the SPI process.
Basically SPI is a synchronous exchange of values between a master and a slave device. The master device generates not only the data pulses (often referred to as MOSI - Master Our Slave In) but also the clock pulses. On the defined clock edge (leading or trailing - this is one of the design decisions you need to make) the slave will present its own data bit (often called MISO - Master In Slave out) and clock in the bit from the master. The master will do the same (present the bit from the value it is sending and receive the bit from the slave).
A master is able to communicate with several slave devices (one at a time) as the slave device has a 'slave select' line that the master must set just before it starts the clock and data pulses, and then resets when the exchange has completed.
Therefore you need to know if you are writing code for the master or the slave device.
For the master, the sequence is typically:
- set the slave select line
- write the value to send to the peripheral's buffer
- wait until the exchange has completed
- reset the slave select line
- read the received value from the peripheral's buffer
Depending on the device, you should check that the peripheral is ready to begin the next exchange.
For the salve, things are a little different because you are normally not in control of when the exchange occurs. This has several implications. Firstly, the slave will exchange whatever value is in its buffer when the master initiates the exchange. If you have not set a value then the value that is sent may not be defined. Also, slaves often use interrupts so that the whole MCU is not 'stuck' waiting for the exchange to complete. However, if you do block, then the process is typically:
- set the value you want to exchange
- wait until the exchange is complete
- read the value that has been received
For many devices, a key step is that the slave must read the received value before the master initiates the next exchange or you will get a 'buffer overflow'. When this occurs, some devices will simply 'freeze' until the error is corrected - however the master cannot tell this has occurred and so will continue attempting to exchange values and therefore values will be lost by the slave and/or received values misinterpreted by the master.
You might see code examples where there is a SPI 'read' and a SPI 'write' function. AS you can see from the above, SPI is an exchange process and so all you really need is a single function that takes a parameter of the value you want to send and a return value of the value you have received. If you simply want to 'send' a value then you can ignore the returned value form the function (but it is very important that the function has read the received value to stop overflows). If you want to 'read' a value, then you still need to 'send' something and so you can send a dummy value (0x00 is often used but you can also define your own).
Susan
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top