Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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.

Could someone advise me on how to go about doing this?

Thanks
 
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();
dummy1 = ReadSPI(); //reading rate output from gyro
dummy2 = ReadSPI();
MSEL1=1;

Nop();
Nop();
// CloseSPI();
}
}
 
Last edited:
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()&0x03)<<12;
    Adc|=ReadSPI()<<4;
    Adc|=(ReadSPI()&0xf0)>>4;

Mike.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top