problem getting input from SPI device

Status
Not open for further replies.

ra0002al

New Member
Hi,

I'm using the PIC 18f4520 for a project along with a gyroscope (Analog Devices Model ADIS16060). The gyro measures rotational velocity about its z-axis and sends the data through SPI to the microcontroller.

The problem I'm facing is that the gyro sends its measurements in a 14 bit form and I'm unable to receive that data at the microcontroller because of its 8-bit SSPBUF.

Thanks

Nigel Goodwin

Super Moderator
Do the SPI in software, it's easy to do, and means you can use any word length you like.

ra0002al

New Member
I'm kind of new to this so am unsure of what to do. I'm using the MPLAB C18 compiler. I'm using the C18 programming library functions like ReadSPI and WriteSPI. This is the code I'm using. It isn't giving me the output I need and I can't spot what I might be doing wrong.

#include <p18cxxx.h>
#include <usart.h>
#include <delays.h>
#include <spi.h>
#include <stdio.h>
#include <string.h>

#pragma config WDT = OFF
#pragma config LVP = OFF

#define MSEL1 PORTDbits.RD0
#define MSEL2 PORTDbits.RD7

#pragma config OSC = INTIO67 //for internal clock 1Mhzs instruction cycle frequency

unsigned char cmd;
unsigned char dummy1;
unsigned char dummy2;

//value assignment within main loop;
void main()
{
cmd=0x20;
dummy1=0x00;
dummy2=0x00;
SSPBUF = cmd;
TRISDbits.TRISD0=0; //Set RD0 as output
TRISDbits.TRISD7=0; //Set RD7 as output

OpenSPI(SPI_FOSC_4,MODE_01,SMPEND);
Nop();
Nop();
MSEL2=0;
Nop();
Nop();
WriteSPI(SSPBUF); //configuring the gyro for rate sensing
MSEL2=1;
Nop();
while(1)
{
Nop();
Nop();
MSEL1=0;
Nop();
Nop();
MSEL1=1;

Nop();
Nop();
// CloseSPI();
}
}

Last edited:

Nigel Goodwin

Super Moderator
I'm kind of new to this so am unsure of what to do. I'm using the MPLAB C18 compiler.
Can't help you with C, I never use it - but you're using the SPI hardware there, you need to write your own software routines for it.

Pommie

Well-Known Member
You need to send 0x20 (not via SSPBUF, just WriteSPI(cmd) and then read 3 bytes. The data will be the 14 bits starting with bit 1 of the first read byte.

Edit, you cound do,
Code:
unsigned int Adc;

Adc|=(ReadSPI()&0xf0)>>4;