It is currently Thu May 25, 2017 9:54 pm

All times are UTC




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

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?

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


Top
 Profile  
 
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 1 guest


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