Interfacing two microcontrollers through SPI protocol

Home Forums Microcontrollers PIC Microcontroller Interfacing two microcontrollers through SPI protocol

Tagged: , , ,

This topic contains 2 replies, has 3 voices, and was last updated by  Ligo George 6 months, 3 weeks ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #11767

    sandeep
    Participant

    Hi sir,
    As I am learning SPI, i thought of interfacing two microcontrollers using SPI protocol, by passing a data in master and retrieving in the slave microcontroller.

    The code is as given below, the problem is I send a data of 0x05, and I get a different output on the slave microcontroller. I use PIC 16F877A microcontroller.

    //master code
    #include <htc.h>
    #include <pic.h>
    #include <stdio.h>
    #include <stdlib.h>
    __CONFIG_3F31;
    
    // __CONFIG(INTCLK & WDTDIS & PWRTDIS & MCLRDIS & UNPROTECT & DUNPROTECT & BORDIS & IESODIS & FCMDIS & LVPDIS);__CONFIG(BORV40);
    
    // function prototypesvoid init_io(void);
    void initialise_SPI(void);
    void spi_w(unsigned char data);
    void init_io(void)
    {
      TRISA = 0x01;
      TRISB = 0x01; // bit 0 = output, 1 = input
      TRISC = 0b10010111;
      TRISD = 0x00;
      TRISE = 0x00;
    }
    //SPI write command function
    void spi_w(unsigned char data)
    {
      unsigned char x;
      PORTC,RC6=0;
      while (!SSPIF); //wait for transmission complete
      x=SSPBUF; //dummy read
      SSPBUF=data;
      while (!SSPIF); //wait for transmission complete
      x=SSPBUF;
      PORTC,RC6=1;
    }
    void initialise_SPI(void)
    {
      SSPEN = 0;
      SSPSTAT=0b01000000;
      SSPCON=0b00100010;
      SSPEN = 1;
    }
    void main()
    {
      unsigned char data= 0x05;
      init_io();
      initialise_SPI();
      while(1)
      {
        spi_w(data);
        PORTD=data;
      }
    }
    
    //slave code
    
    void initialise_SPI(void);
    unsigned char spi_r(void);
    void init_io(void)
    {
      TRISA = 0b00100000;
      TRISB = 0x00;
      TRISC = 0b00011000;
      TRISD = 0x00;
      TRISE = 0x00;
    }
    void initialise_SPI()
    {
      SSPEN = 0;
      SSPSTAT=0b01000000;
      SSPCON=0b00100100;
      SSPEN = 1;
    }
    
    unsigned char spi_r()
    {
      unsigned char temp;
      PORTA,RA5= 0;
      while(!SSPIF);
      temp=SSPBUF;
      while(!SSPIF);
      temp=SSPBUF;
      PORTA,RA5 =1;
      return temp;
    }
    void main ()
    {
      unsigned char temp ;
      init_io();
      initialise_SPI();
      while(1)
      {
        temp=spi_r();
        PORTD=temp;
      }
    }
    
    #11873

    RonnieLord
    Participant

    The code you are using is looking fine and workable? Did you tried it on your hardware? Also what is your whole setup? What are the other components?

    Can you please show all about this here? Also try by using the different port?

    Why you want to interface two microcontrollers? Many readymade boards are available to do this job.

    #13735

    Ligo George
    Keymaster
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

string(7) "9898001"