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.

Question for SPI on 18f8722

Status
Not open for further replies.

wilsonz91

Newbie level 3
Joined
Aug 8, 2011
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,305
Question for SPI

Hello,

i am trying to interface two PIC18F8722 with each other using SPI. I manage to get it going (one way communication) doing a simple countdown but there some problems which I could not find a solution to.

here is the code for:

master
Code:
#include <p18f8722.h>    
#include "spi.h"
#include <delays.h>

void main()
{
	unsigned char a = 0xFF,b;

	TRISC = 0xD7;
	TRISF = 0x00;
	
	OpenSPI1(SPI_FOSC_4, MODE_01, SMPMID);
	
	while(1){
		WriteSPI1(a);
		while(!DataRdySPI());
		b=ReadSPI1();
		LATF = b;
		Delay10KTCYx(200);
		
		
		a--;

		if (a==0)
			a = 0xFF;
	}
}

Slave:
Code:
#include <p18f8722.h>     
#include "spi.h"

void main()
{
	unsigned char a;
	
	TRISC = 0x1F;
	TRISF = 0x00;
	
	OpenSPI1(SLV_SSOFF, MODE_01, SMPMID);
	LATF = ReadSPI1();                                       [I]//without this line, SPI would not work. why? wouldn't having this line in the while loop be sufficient?[/I]

	while(1){
		while(!DataRdySPI1());
		a = ReadSPI1();
		LATF = a;
		a--;
		WriteSPI1(a);

	if (a == 0)
		a = 0xFF;
	}
}

Connections are:
master slave
RC3 -> RC3 (SCK)
RC5 -> RC4 (master out to slave in)
RC4 -> RC5 (slave out to master in)

What I am trying to achieve is two-way communication where both master and slave would be counting down eg master-255, slave-254,master-253,slave,252.

Please advice, thank you =)
 
Last edited:

Are you doing simulation on MPSIM or on hardware?
BTW I would enable CS line before writing any code and disable after transfer done.
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top