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

Problem when send data from slave

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



Joined: 15 Feb 2015
Posts: 39

View user's profile Send private message

Problem when send data from slave
PostPosted: Sun Feb 15, 2015 10:17 am     Reply with quote

Hello evryone, now i am try to testing SPI protocol with 2 pic 16f887 and button. I can send data from master to slave ok, but i can not resend data from slave, it error. This is my simple code to test, please help me...thank so much.

This is master

Code:

#INCLUDE <TV_16F887.C>
#USE SPI(FORCE_HW, BITS=16, stream=SPI_STREAM)
UNSIGNED INT DATA1,DATA2,DATA3,X,I;

#INT_SSP
VOID NGAT_SSP()
{
   DATA1=SPI_READ();
}

VOID MAIN()
{
   TRISB=0XFF;TRISD=0X00;PORTD=0;TRISE=0;PORT_B_PULLUPS(0XFF);
   SETUP_SPI(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_16);
   KHOITAO_LCD();
   GIE=1;PEIE=1;SSPIE=1;
   WHILE(TRUE)
   {
   
      OUTPUT_LOW(PIN_C0);
     
      IF(!INPUT(PIN_B0))
      {
         DELAY_MS(30);
         IF(!INPUT(PIN_B0))
         {
            SPI_WRITE(0X01);
            WHILE(!INPUT(PIN_B0));
         }
      }
     
      IF(!INPUT(PIN_B1))
      {
         DELAY_MS(30);
         IF(!INPUT(PIN_B1))
         {
            SPI_WRITE(0X02);
            WHILE(!INPUT(PIN_B1));
         }
      }
      IF(!INPUT(PIN_B2))
      {
         DELAY_MS(30);
         IF(!INPUT(PIN_B2))
         {
            SPI_WRITE(0X03);     
            WHILE(!INPUT(PIN_B2));
         }
      }
      OUTPUT_HIGH(PIN_C0); 
     
      THIETLAP_LCD(0X80,0);
      THIETLAP_LCD(DATA1+ 0X30,1);
   }
}


This is slave
Code:

#INCLUDE <TV_16F887.C>
#USE SPI(FORCE_HW, BITS=16, stream=SPI_STREAM)
UNSIGNED INT DATA1,DATA2,DATA3;

#INT_SSP
VOID NGAT_SSP()
{
      DATA1=SPI_READ();
}

VOID MAIN()
{
   TRISB=0XFF;TRISD=0X00;PORTD=0;TRISE=0;
   PORT_B_PULLUPS(0XFF);
   SETUP_SPI(SPI_SLAVE|SPI_L_TO_H|SPI_CLK_DIV_16);
   GIE=1;PEIE=1;SSPIE=1;
   KHOITAO_LCD();
   WHILE(TRUE)
   {
     
      OUTPUT_LOW(PIN_A5);
     
      IF(!INPUT(PIN_B0))
      {
         DELAY_MS(30);
         IF(!INPUT(PIN_B0))
         {
            SPI_WRITE(0X01);
            WHILE(!INPUT(PIN_B0));
         }
      }
     
      IF(!INPUT(PIN_B1))
      {
         DELAY_MS(30);
         IF(!INPUT(PIN_B1))
         {
            SPI_WRITE(0X02);
            WHILE(!INPUT(PIN_B1));
         }
      }
      IF(!INPUT(PIN_B2))
      {
         DELAY_MS(30);
         IF(!INPUT(PIN_B2))
         {
            SPI_WRITE(0X03);     
            WHILE(!INPUT(PIN_B2));
         }
      }
      OUTPUT_HIGH(PIN_A5);
     
      THIETLAP_LCD(0X80,0);
      THIETLAP_LCD(DATA1+ 0X30,1);
   }
   
   
}


Library tv_pic16f887.c is my library which i create, don't care about that, everything is ok except sent data. I push button 1 and lcd of slave show '1', but when i push button of slave, lcd of master show nothing.

note:rc0 of master is chip select
temtronic



Joined: 01 Jul 2010
Posts: 9221
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Feb 15, 2015 12:49 pm     Reply with quote

You're not understanding the whole SPI mode of communications.
An SPI 'master' can send data TO a slave and receive data from a slave at the same time. The MASTER controls the communications. A SLAVE ,is well, a 'slave' , it cannot send data to a 'master' as such. It can send data ONLY when the 'master' asks for it.
The SPI 'master' sends it's buffer of data to the SPI 'slave' buffer AND at the SAME time, the 'slave' sends it's buffer of data to the 'master's buffer. The 'master' controls which SPI 'slave' by the use of a 'control ' pin called SS(Slave select).
Please read the datasheet section on MSSP, SPI modes. CCS does have a few examples you should look at as well.

If you want to have a 'remote' send data to a 'host' at anytime, then you should consider 'RS-232' or 'RS-485' type of communications. SPI is a 'master' initiated form of communications.

hth
Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19499

View user's profile Send private message

PostPosted: Sun Feb 15, 2015 12:56 pm     Reply with quote

In SPI, _one_ device is the master. This device is the only one that can initiate a transfer.
There is no point in an INT_SPI in a master device. It _has_ to initiate all transfers, so cannot have a receive interrupt triggered.

To 'receive' a byte on the master, the _slave_ device loads the byte into it's buffer. The _master_ then sends a byte, and receives _back_ the byte already waiting in the slave. This is returned by the write command.

If you need the slave to be able to initiate a transfer, then you need an extra wire from the slave to the master. The slave loads the byte ready to send, and toggles this line. You use this to trigger an interrupt in the master, which then performs the transfer.

If you want multi master, then look at a bus that supports this. It can be done in SPI, but the bus is not designed for this, and it is awkward and cumbersome to do. I2C, can do this slightly easier, or a bus like RS485 serial is fundamentally designed to do this.
Ttelmah



Joined: 11 Mar 2010
Posts: 19499

View user's profile Send private message

PostPosted: Sun Feb 15, 2015 1:04 pm     Reply with quote

I see we both posted at the same time. You beat me to it, by posting while I was typing. Smile
talamahahahe



Joined: 15 Feb 2015
Posts: 39

View user's profile Send private message

PostPosted: Mon Feb 16, 2015 3:27 am     Reply with quote

Dear temtronic and Ttelmah....thank you so much for help me to understand about SPI ,i was think i can tranfer from master to slave and contrary like i2c or rs232 ,but i was wrong...but one more help, i have 2 pic,i call PIC A and PIC B,if i want to send data from PIC A by push button(button connect portb) and show in port d of PIC B ,how can i do,i was try like that but fail Sad This is my code which i was fix

code for master
Code:

#INCLUDE <TV_16F887.C>
#USE SPI(FORCE_HW, BITS=16, stream=SPI_STREAM)
UNSIGNED INT DATA1,DATA2,DATA3,X,I;

VOID MAIN()
{
   TRISB=0XFF;TRISD=0X00;PORTD=0;TRISE=0;PORT_B_PULLUPS(0XFF);
   SETUP_SPI(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_16);
   KHOITAO_LCD();
   WHILE(TRUE)
   {
      IF(!INPUT(PIN_B0))
      {
         DELAY_MS(30);
         IF(!INPUT(PIN_B0))
         {
            OUTPUT_LOW(PIN_C0);
            SPI_WRITE(0X01);
            DATA1=SPI_READ();
            OUTPUT_HIGH(PIN_C0);
            WHILE(!INPUT(PIN_B0)); 
         }
      }
     
      IF(!INPUT(PIN_B1))
      {
         DELAY_MS(30);
         IF(!INPUT(PIN_B1))
         {
            OUTPUT_LOW(PIN_C0);
            SPI_WRITE(0X02);
            DATA2=SPI_READ();
            OUTPUT_HIGH(PIN_C0);
            WHILE(!INPUT(PIN_B1)); 
         }
      }

      THIETLAP_LCD(0X80,0);
      THIETLAP_LCD(DATA1+ 0X30,1);
      THIETLAP_LCD(DATA2+ 0X30,1);
      THIETLAP_LCD(DATA3+ 0X30,1);
   }
}


code for slave
Code:

#INCLUDE <TV_16F887.C>
#USE SPI(FORCE_HW, BITS=16, stream=SPI_STREAM)
UNSIGNED INT DATA1,TT;

VOID MAIN()
{
   TRISB=0XFF;TRISD=0X00;PORTD=0;TRISE=0;
   PORT_B_PULLUPS(0XFF);
   SETUP_SPI(SPI_SLAVE|SPI_L_TO_H|SPI_CLK_DIV_16);
   KHOITAO_LCD();
   WHILE(TRUE)
   {

      TT=SPI_READ();
      IF(TT==0X01)       {DATA1=0X01;SPI_WRITE(0X01);}// i think i was wrong at here ,slave can't write data unless master ask like your opion,how can i fix it
      ELSE IF(TT==0X02)  {DATA1=0X02;SPI_WRITE(0X02);}// wrong
     
      THIETLAP_LCD(0X80,0);
      THIETLAP_LCD(DATA1+ 0X30,1);
   }
   
   
}


my idea is when i push button at pin b0 0f PIC A ,PIC A will send 0x01 for PIC B, PIC B receive and resend 0x01 for PIC A. when i push button at pin b1 of PIC A ,PIC A whill send 0x02 for PIC B,PIC B receive and resend for PIC A 0x02....i choose pic A is mater
talamahahahe



Joined: 15 Feb 2015
Posts: 39

View user's profile Send private message

PostPosted: Mon Feb 16, 2015 3:32 am     Reply with quote

if you can,please give me some example simple code about spi which communicate betwen 2 pic of more...thank Smile
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