View Single Post
  #6   Spotlight this post!  
Unread 08-01-2007, 15:22
Dave K.'s Avatar
Dave K. Dave K. is offline
Engineer/Mentor
FRC #0930
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 2005
Location: WI
Posts: 91
Dave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to behold
Re: SPI Port on Robot Controller

Quote:
Originally Posted by taflick View Post
On the 2006 controller, the PIC micro had the SPI port #2 connected to the Relay outs 5, 6 and 7. With this upcoming season almost upon us, I have thinking about ways to use an SPI port to talk to custom electronics. Not being a programmer, I wonder if any one has written code for such a purpose, ie using the SPI port with the relay outputs of the RC.
The following is untested on the RC and provided only as a prototypical example of how you can use the generic I/O lines to do this on your own.

You didn't indicate what you were intending to interface with, but you need to pay attention to the clock and data line phase relationship as well as recognising that some SPI periphials will have input and output data transitions occurring on opposite clock phases.

In many cases you will find that if your intent is to us a microcontroller's onboard SPI interface and operate it at relatively high speeds, that by the time you get the byte or word loaded into the SPI shift register, the shift action occurs so quickly (with respect to CPU execution speed) that you really don't have time to interact with the SPI port on an interrupt basis, because by the time you exited from the interrupt, the SPI shift would be completed, and you'd immediately re-enter your interrupt service routine.

All things considered, you'll find that bit banging the SPI data on generic I/O pins with a few bytes of data here and there does not take an appreciablly greater period of time than using on board SPI hardware.

If you have larger amounts of data to transfer between CPU's or perhiphials on a very regular basis (such as the inter-processor communication between the two CPU's in the RC), then the onboard SPI hardware can be quite helpful.

Code:
#include "ifi_aliases.h"

#define MYSPI_CLK	rc_dig_out16
#define MYSPI_DOUT	rc_dig_out17
#define MYSPI_DIN	rc_dig_in18


// call this on init to setup the port I/O as needed

void MySPI_Init(void) {

        MYSPI_CLK = 0;
	digital_io_16 = OUTPUT;
	digital_io_17 = OUTPUT;
	digital_io_18 = INPUT;

}


unsigned char MySPI_ByteIO(unsigned char dat_out) {

unsigned char mask = 0x80;
unsigned char dat_in = 0;

	while (mask) {
		MYSPI_DOUT = (dat_out & mask)?1:0;
		dat_in <<=1;
		if (MYSPI_DIN) {
			dat_in |= 1;
		}
		MYSPI_CLK = 1;
		mask >>= 1;
		MYSPI_CLK = 0;
	
	}
	return(dat_in);

}
__________________
--Dave