Hi, I’m doing a school project to turn a slot car into being controlled by RF. I’m using a PIC18F2620 and the Olimex RF unit since the MiRF v2 was sold out when we had to buy parts and we’re under time constraints. I am using the library from Brennon’s library from diyembedded.com and I am having trouble talking over SPI.
After searching on these forums I found some SPI comm test code, basically writing to the TX_ADDR register and then reading back to see if the values are the same. Here is the code I’m using:
#include <p18f2620.h>
#include <spi.h>
//function prototypes
unsigned char spi_Send_Read(unsigned char);
// Defines
#define SPI_SCK LATCbits.LATC3 // Clock pin, PORTC pin 3
#define SPI_SO LATCbits.LATC5 // Serial output pin, PORTC pin 5
#define SPI_SI PORTCbits.RC4 // Serial input pin, PORTC pin 4
#define SPI_CSN LATCbits.LATC0 // CSN output pin, PORTC pin 2
#define SPI_CE LATCbits.LATC6 // CE output pin, PORTC pin 1
#define SPI_IRQ PORTBbits.RB0 // IRQ input pin, PORTB pin 0
#define SPI_SCALE 4 // postscaling of signal
// Macros
#define nop() _asm nop _endasm
void main(void)
{
unsigned char status = 0;
// unsigned char data[5];
unsigned char one = 0;
unsigned char two = 0;
unsigned char three = 0;
unsigned char four = 0;
unsigned char five = 0;
PORTA = 0x00;
ADCON1 = 0x0F; // set up PORTA to be digital I/Os
TRISA = 0x00; // set up all PORTA pins to be digital outputs
TRISCbits.TRISC3 = 0; // SDO output
TRISCbits.TRISC5 = 0; // SCK output
TRISCbits.TRISC0 = 0; // CSN output
TRISCbits.TRISC6 = 0; // CE output
SPI_CSN = 1; // CSN high
SPI_SCK = 0; // SCK low
SPI_CE = 0; // CE low
nop();
OpenSPI(SPI_FOSC_64, MODE_00, SMPMID);
//write TX_ADDRESS register
SPI_CSN = 0; //CSN low
spi_Send_Read(0x30);
spi_Send_Read(0x11);
spi_Send_Read(0x22);
spi_Send_Read(0x33);
spi_Send_Read(0x44);
spi_Send_Read(0x55);
SPI_CSN = 1; //CSN high
//read TX_ADDRESS register
SPI_CSN = 0; //CSN low
status = spi_Send_Read(0x10);
one = spi_Send_Read(0x00);
two = spi_Send_Read(0x00);
three = spi_Send_Read(0x00);
four = spi_Send_Read(0x00);
five = spi_Send_Read(0x00);
SPI_CSN = 1; //CSN high
while (1)
;
}
/********************************************************************
* Function: unsigned char spi_Send_Read(unsigned char bytein)
*
* Description: This function outputs a single byte onto the
* SPI bus, MSb first. And it gets the response.
*******************************************************************/
unsigned char spi_Send_Read(unsigned char bytein)
{
SSPBUF = bytein;
while(!DataRdySPI());
return SSPBUF;
}
the values I get back are the following:
status = 0x11
one = 0x80
two = 0x00
three = 0x00
four = 0x00
five = 0x00
I’ve tried this with both of my modules and I get the same response for both. When I unplug the module, everything is 0x00. It definitely seems like it’s talking to something, I just don’t know what’s going on with it.
One possibility is that earlier while working with it, I had forgotten to attach my ground bus to ground, so I wasn’t getting any response. During this time I had attached it to 5V thinking that it needed the same reference voltage as my PIC. I later found out that 5V was over the max voltage and since I discovered my ground issue, I have only attached it to 3.3V. Could this cause the behavior I’m seeing? I stupidly did this with both modules even after I told myself not to attach both to 5V in case that it was too much, I’d be able to prove it later if only one was attached. But when one wasn’t working I switched to check the other and forgot about the 5V thing.
Anyway, any help would be greatly appreciated.