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>
#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?
Best Answer
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.