Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

spi communication

Home Forums MPLAB XC8 Tutorial Discussions spi communication

This topic contains 7 replies, has 2 voices, and was last updated by  Bitahwa Bindu 7 months, 2 weeks ago.

  • Author
  • #3368


    hello members

    my question concerns the following tutorial :


    i downloaded this tutorial and try to learn from it

    in this tutorial a ‘1’ is sent over the bus to the slave  which lights some leds up in a sequence.  i reconstruct this  code a bit because i want to sent a byte over the bus,  but i discovered that the ‘1’ is sent over the bus as an  char    according to asci table as 0x31 hex/  49 dec.

    i want to do it that if the master sends a 238 in a byte the slave also receives it as 238 or better not a byte but an integer

    what do i need to change





  • #3369

    Bitahwa Bindu

    I hope what you mean is that you copy the link of  that tutorial or you downloaded the source files because we don’t approve of people downloading the content of our tutorials without prior approval.

    You can only download the downloadable files like the Source code of the project, not the tutorial text.

    What are you exactly trying to achieve?

    SPI is a data exchange mechanism.  As one device sends a byte of data, so the other transmits one. You can also convert your integer number into bytes or strings then send byte per byte.

  • #3370


    no i only download the source files as is possible from the site ,   studied some tutorials on the site and watched student companion videos on youtube. i have no bad intentions i just read a lot

    what i try to achieve is that now in the tutorial one byte/8bits is sent over the bus,  but i wonder what do i need to change if i want to sent more data over the bus,  for instance 6 times a  unsigned long int after each other



  • #3371

    Bitahwa Bindu

    No problem. You can read as much as you like, you can also download the source files no problem. We are just worried about people who counterfeit materials like text and images and post them in other websites without approval.

    For multiple byte transfer, you just have to keep the chip select bit low for a particular slave between every byte by setting the CS_SetLow(). After the last byte, you can deselect the slave by setting CS_SetHigh().  You can use a loop 6 times or write the exchange function 6 times.

    Since SPI is a synchronous protocol there should be no issue with having a delay in between bytes provided the select is maintained.

  • #3373


    So underneath i made an example of how to sent four bytes of data to a slave

    LATC0 = 0;                                        // Select slave 1 (Active low for Device 1)
    SPI_Exchange8bit     (0xff);          //this is the first byte to be sent                                          SPI_Exchange8bit     (0xe5);         // this is the second byte to be sent                                SPI_Exchange8bit     (ox34);         // third byte to be sent                                                        SPI_Exchange8bit      (0x8b);        //  fourth byte to be sent
    LATC0 = 1;                                        // Deselect slave 1

    and this is the code for the slave to receive 4 bytes of data from the master:

    while (1) {
    data= SPI1_Exchange8bit(0x00);      //first byte of data to receive                                                   variable2= SPI1_Exchange8bit(0x00);      //second byte of data to receive                             variable3= SPI1_Exchange8bit(0x00);      //third byte of data to receive                                    variable4= SPI1_Exchange8bit(0x00);      //fourth byte of data to receive
    LATB = data;

    this is the right procedure for sending four bytes of data to an slave


  • #3377

    Bitahwa Bindu

    When you are learning, it’s always better to play around, use the debugger or Proteus simulation and check and see what you are sending and what you are receiving. You’ll learn faster and better than asking each step as a question.

    Have you tried simulating or debugging?  There is even an SPI protocol analyzer in Proteus.

  • #3379


    yes i know debugging and thought about it,  but then i need to debug two pic’s master and slave ad the same time but anyway i will try,    also that spi protocol analyser


  • #3380

    Bitahwa Bindu

    There is no other way.

You must be logged in to reply to this topic.

Select your currency
EUR Euro
USD United States (US) dollar