Home › Forums › Microcontrollers › PIC Microcontroller › Interfacing two microcontrollers through SPI protocol
- This topic has 2 replies, 3 voices, and was last updated 7 years, 7 months ago by Ligo George.
-
AuthorPosts
-
September 19, 2015 at 3:29 pm #11767sandeepParticipant
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; } }
November 24, 2015 at 9:08 pm #11873RonnieLordParticipantThe 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.
May 1, 2017 at 1:38 pm #13735Ligo GeorgeKeymasterHi,
You can refer the following tutorials.
-
AuthorPosts
- You must be logged in to reply to this topic.