Establish SPI communication between two components

I am new to microcontrollers programming and I am trying to establish SPI communication between PIC24FV32KA301 and MS5611.

After a week of searching and learning about SPI I am still not able to communicate between those two and I would appreciate if someone could help me.
Am I missing something?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  /* Device header file */#if defined(__XC16__)
    #include <xc.h>#elif defined(__C30__)
    #if defined(__PIC24E__)
        #include <p24Exxxx.h>
    #elif defined (__PIC24F__)||defined (__PIC24FK__)
    #include <p24Fxxxx.h>
    #elif defined(__PIC24H__)
    #include <p24Hxxxx.h>
    #endif#endif
#include <stdint.h>        /* Includes uint16_t definition                    */#include <stdbool.h>       /* Includes true/false definition                  */
#include "system.h"        /* System funct/params, like osc/peripheral config */#include "user.h"          /* User funct/params, such as InitApp              */
#include "uart1.h"
#include <libpic30.h>        // __delayXXX() functions macros defined here
/******************************************************************************//* Global Variable Declaration                                                *//******************************************************************************/
/* i.e. uint16_t <variable_name>; */
/******************************************************************************//* Main Program                                                               *//******************************************************************************/
void SPIinit(){
    //Master mode, 8bits
    SPI1CON1bits.MSTEN = 1;
    SPI1CON1bits.MODE16 = 0; //8bit
    SPI1CON1bits.DISSCK = 0;

    //SPI mode 0
    SPI1CON1bits.CKP = 0;
    SPI1CON1bits.CKE = 1;

    //1:1 prescale
    //SPI1CON1bits.SPRE = 0b111;
    //SPI1CON1bits.PPRE = 0b11;

    //Enable SPI operation
    SPI1STATbits.SPIROV = 0;
    SPI1STATbits.SPIEN = 1;}
char SPIsend(char command){

    unsigned int buffer;

    //wait until the device is available
    while( SPI1STATbits.SPITBF);
    // Initiate the exchange
    SPI1BUF = command;

    // Wait for the exchange to complete (or handle things via an ISR)
    while( !SPI1STATbits.SPIRBF);

    //Read from buffer
    buffer = SPI1BUF;

    //return first byte
    return buffer & 0xff;}
int16_t main(void){

    /* Configure the oscillator for the device */
    ConfigureOscillator();

    /* Initialize IO ports and peripherals */
    InitApp();



    //OSCCON = 0x11C0;   //Use primary, no divide FCY = 10Mhz/2 = 5Mhz
    OSCCON = 0x11C0;
    CLKDIV = 0x0000;     //do not divide


    ANSELA = 0x0000;
    ANSELB = 0x0000;

    //UART
    TRISBbits.TRISB2 = 1;
    TRISBbits.TRISB7 = 0;

    //SPI
    TRISBbits.TRISB12 = 0; //SCK
    TRISBbits.TRISB13 = 0; //SDO
    TRISBbits.TRISB14 = 1; //SDI
    TRISBbits.TRISB15 = 0; //CSB

    LATBbits.LATB15 = 0; //CSB

    //Disable Watch Dog Timer
    RCONbits.SWDTEN = 0;

    UART1Init(12);   //Initiate UART1 to 19200 at 8MHz OSCI
    SPIinit();
    __delay_ms(1000);

    SPIsend(0x1E);
    __delay_ms(3);


    //pressure command
    SPIsend(0x40);
    __delay_ms(9);

    //read
    UART1PutChar(SPIsend(0x00));
    UART1PutChar(SPIsend(0x00));
    UART1PutChar(SPIsend(0x00));

    while(1){

    }}


I am using UART1PutChar to send results back to my computer and I am always getting the response 0xFF from MS5611, but if I connect SDO and SDI on board I get back the same data I am sending to MS5611 which means my code works but sensor(MS5611) is dead?

More details of the components:
PIC24FV32KA301: http://www.kynix.com/Parts/170698/PIC24FV32KA301-E%2FP.html
MS5611: http://www.kynix.com/Detail/1178577/MS5611-01BA.html
Topic archived. No new replies allowed.