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

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?

/* 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>

#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 */

    /* Initialize IO ports and peripherals */

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

    ANSELA = 0x0000;
    ANSELB = 0x0000;

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

    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


    //pressure command




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?


2 Answers 2


I probably should write this into a comment, but I don't have enough reputation to write comments, so here goes:

I have several devices in production use with both PIC24FV32KA301 and PIC24F32KA301 using SPI all over the place.

As far as I can tell from what you posted, your SPI usage looks sound.

Although the sensor datasheet doesn't state it, but the things I use SPI with usually require the CSB to be pulled high, maybe kept high a bit, then pulled low again between commands. You might try that...

Also the conversion supply current is measurably higher than the standby current. You may try to bombard the sensor with conversion commands and check the current to see if it gets/executes the commands at all.

  • \$\begingroup\$ Thanks a lot, by bombarding my sensor with commands I noticed that SDO line from PIC was not generating enough power, so i replaced PIC with new one and now it works perfectly. Amplifying SDO with transistor also solved the problem. \$\endgroup\$
    – Nejc Okorn
    Commented Sep 1, 2015 at 9:44

After starting your transmission by writing to SPI1BUF, I think you will need to wait until the transmission is complete before you then start checking if the receive is complete. You also seem to be ignoring the values you want to send and sending 0x00 instead.

  • \$\begingroup\$ Waiting until transmission is complete does not change a thing(tried that). I am sending 0x00 to read back from sensor as it is written in Datasheet or am I wrong? \$\endgroup\$
    – Nejc Okorn
    Commented Aug 21, 2015 at 15:35
  • \$\begingroup\$ You are calling spisend(0x1e) and (0x40) but your spisend routine does not send those values it just sends 0x00, \$\endgroup\$
    – HandyHowie
    Commented Aug 21, 2015 at 21:28
  • \$\begingroup\$ Tnx, did not notice that before, but even now I am not getting anything from sensor. \$\endgroup\$
    – Nejc Okorn
    Commented Aug 21, 2015 at 21:32
  • \$\begingroup\$ Can you try this, it works for me unsigned char spi1Write (unsigned char command) { SPI1BUF = command; while (SPI1STATbits.SPITBF==1); while (SPI1STATbits.SPIRBF==0); return SPI1BUF; } \$\endgroup\$
    – HandyHowie
    Commented Aug 21, 2015 at 22:00
  • \$\begingroup\$ Szidor has got a point, the CSB line should be lowered for the duration of the command then raised. \$\endgroup\$
    – HandyHowie
    Commented Aug 21, 2015 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.