It is currently Thu Aug 17, 2017 9:45 am

All times are UTC

Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Fri Mar 10, 2017 9:46 am 

Joined: Fri Mar 10, 2017 9:41 am
Posts: 1
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?

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

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC

Who is online

Users browsing this forum: No registered users and 2 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group