Hot!Cannot get SPI1 to work with dsPIC33FJ128GP802

Page: 12 > Showing page 1 of 2
Author
ezedhill
Starting Member
  • Total Posts : 36
  • Reward points : 0
  • Joined: 2019/02/07 06:43:05
  • Location: 0
  • Status: online
2019/02/07 13:41:39 (permalink)
0

Cannot get SPI1 to work with dsPIC33FJ128GP802

I have tried so many things and it still doesn't work. I know I'm over looking something, but I can't see it. Maybe someone else can see what I'm doing wrong. 
 
Here is the source;
 
// must be defined before libpic30.h is included
#define FCY 7370000UL // define clk frequency to use delay routines in libpic30

#include <pps.h>
#include <spi.h>
#include <libpic30.h>

#include "xc.h"

// prototypes
void openSPI(void);
void SPIWrite(uint16_t data);
void SPIClose(void);

// pin definitions
#define LED LATAbits.LATA1

int main(void) {
    uint16_t command;
    
    AD1PCFGL = 0xffff; // make all IO pins digital

    TRISBbits.TRISB13 = 0; // make RB13 an output,
    TRISBbits.TRISB14 = 0; // make RB14 an output,
    TRISBbits.TRISB15 = 0; // make RB15 an output,

    TRISAbits.TRISA1 = 0; // make RC9 an output, LED

    // re-map SPI1 SCLK and SDO1 pins to RP14 and RP15
    PPSUnLock;
    iPPSOutput(OUT_PIN_PPS_RP14, OUT_FN_PPS_SCK1);
    iPPSOutput(OUT_PIN_PPS_RP15, OUT_FN_PPS_SDO1);
    iPPSOutput(OUT_PIN_PPS_RP13, OUT_FN_PPS_SS1);
    PPSLock;

    openSPI(); // initialize and open SPI1

    command = 0x1155;

    while (1) {
        // to see if it's alive
        LED = 1; // turn on LED
// __delay_ms(1000); // ~1 sec delay
// LED = 0; // turn LED off

        // write to the SPI digital pot
        SPIWrite(command); // set the digital pot to 170th step
    }
    return 0;
}

void openSPI(void) {
    unsigned int SPICONValue;
    unsigned int SPISTATValue;
    __builtin_write_OSCCONL(OSCCON & 0xBF);
    // set SPICON and SPISTAT registers
    SPICONValue = FRAME_ENABLE_OFF & //FRMEN: 0 = Framed SPI support disabled
            FRAME_SYNC_OUTPUT & //SPIFSD: 0 = Frame sync pulse output (master)
            ENABLE_SDO_PIN & //DISSDO: 0 = SDOx pin is controlled by the module
            SPI_MODE16_ON & //MODE16: 1 = Communication is 2 byte-wide (16 bits)
            SPI_SMP_OFF & //SMP: 0 = Input data sampled at middle of data output time
            SPI_CKE_OFF & //CKE: 0 = Serial output data changes on transition from Idle clk state to active clk state
            SLAVE_ENABLE_ON & //SSEN: 0 = SS pin not used by module. Pin controlled by port function
            CLK_POL_ACTIVE_HIGH & //CKP: 0 = SS pin not used by module. Pin controlled by port function
            MASTER_ENABLE_ON & //MSTEN: 1 = Master mode
            SEC_PRESCAL_3_1 & //SPRE<2:0>: Secondary Prescale 3:1
            PRI_PRESCAL_1_1; //PPRE<1:0> Primary Prescale 1:1
    SPISTATValue = SPI_ENABLE & //SPIEN: 1 = Enables module and configures SCKx, SDOx, SDIx and SSx as serial port pins
            SPI_IDLE_CON & //SPISIDL: 0 = Continue module operation in Idle mode
            SPI_RX_OVFLOW_CLR; //SPIROV: 0 = No overflow has occurred. Clear receive overflow bit.
    OpenSPI1(SPICONValue, 0, SPISTATValue); // open SPI1
}

void SPIWrite(uint16_t data) {
    SPI1BUF = data;
    while (SPI1STATbits.SPITBF);
    //WriteSPI1(data);
}

void SPIClose(void) {
    CloseSPI1();
}

#1

35 Replies Related Threads

    ezedhill
    Starting Member
    • Total Posts : 36
    • Reward points : 0
    • Joined: 2019/02/07 06:43:05
    • Location: 0
    • Status: online
    Re: Cannot get SPI1 to work with dsPIC33FJ128GP802 2019/02/07 13:56:56 (permalink)
    0
    I'm using the Pickit 4 with MPLAB v5.10 and the XC16 compiler. 
    #2
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 2959
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Cannot get SPI1 to work with dsPIC33FJ128GP802 2019/02/07 15:42:42 (permalink)
    0
    The FCY should be the fcy you are running at.
    40,000,000
     
     
    void SPIWrite(uint16_t data) {
        SPI1BUF = data;
        while (SPI1STATbits.SPITBF);
        SPI1BUF; ######## ADD THIS #########
    }


    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #3
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 2959
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Cannot get SPI1 to work with dsPIC33FJ128GP802 2019/02/07 15:58:12 (permalink)
    0
    SPI1CON1L = _SPI1_CKE_MASK|_SPI1_SMP_MASK
     
    They are the C macros masks, just replace with the register name.
    _SPI1_xxx_MASK
    All other bits are clear.
     
    Try doing it without MCC, I don't like the AND method of masking bits.
     
     

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #4
    MBedder
    Circuit breaker
    • Total Posts : 6672
    • Reward points : 0
    • Joined: 2008/05/30 11:24:01
    • Location: Zelenograd, Russia
    • Status: offline
    Re: Cannot get SPI1 to work with dsPIC33FJ128GP802 2019/02/07 16:02:01 (permalink)
    +1 (1)
    Gort2015The FCY should be the fcy you are running at.
    40,000,000
    How did you know?


    #5
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 2959
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Cannot get SPI1 to work with dsPIC33FJ128GP802 2019/02/07 17:34:23 (permalink)
    0
    Lucky guess but fcy 7,370,000 looks like the internal RC.

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #6
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 2959
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Cannot get SPI1 to work with dsPIC33FJ128GP802 2019/02/07 17:37:09 (permalink)
    0
    I have not used that library but should it be XTAL or something for the delay functions?

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #7
    qhb
    Superb Member
    • Total Posts : 8712
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: Cannot get SPI1 to work with dsPIC33FJ128GP802 2019/02/07 17:44:16 (permalink)
    0
    Gort2015
    I have not used that library but should it be XTAL or something for the delay functions?

    No, it's XC8 that uses _XTAL_FREQ.
    XC16 uses FCY
     
     
    #8
    Aussie Susan
    Super Member
    • Total Posts : 3444
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: Cannot get SPI1 to work with dsPIC33FJ128GP802 2019/02/07 18:32:19 (permalink)
    0
    What do you mean "...doesn't work"? That is far too vague an error description to be of use.
    What does it do that you do not expect? What does it not do that you expect? How are you determining that it does or doesn't do something?
    You are using the SPI peripheral in master mode. Therefore you need to drive the \SS\ line (which need not be the \SS\ pin that the SPI1 peripheral only uses for input) yourself. Make sure that the library you are using does and knows which pin to use this because you aren't driving this yourself.
    You don't show the config settings but as a general recommendation you should not unlock and lock the PPS registers until you have finished your development work. The PPS registers are unlocked by default but the default IOL1WAY config setting is to only allow you to unlock them once after each reset - as you expand your development (and especially if you use separate code files that each try to configure the PPS registers they way that module needs them) then you will very quickly run into problems.
    Susan
    #9
    ezedhill
    Starting Member
    • Total Posts : 36
    • Reward points : 0
    • Joined: 2019/02/07 06:43:05
    • Location: 0
    • Status: online
    Re: Cannot get SPI1 to work with dsPIC33FJ128GP802 2019/02/07 19:15:34 (permalink)
    0
    Yes, I am using the internal RC clock. Isn't that what I should set FCY to?
    #10
    qhb
    Superb Member
    • Total Posts : 8712
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: Cannot get SPI1 to work with dsPIC33FJ128GP802 2019/02/07 19:28:00 (permalink)
    0
    ezedhill
    Yes, I am using the internal RC clock. Isn't that what I should set FCY to?

    Correct. Leave FCY as it is.
    As for your other issues, I agree with Susan, it's impossible to comment until you are a bit more forthcoming about what "doesn't work" actually means.
     
    #11
    ezedhill
    Starting Member
    • Total Posts : 36
    • Reward points : 0
    • Joined: 2019/02/07 06:43:05
    • Location: 0
    • Status: online
    Re: Cannot get SPI1 to work with dsPIC33FJ128GP802 2019/02/07 19:39:35 (permalink)
    0
    There is no clock or data on the pins. I have the dspic interfaced to a MCP44110 digital pot. This device starts up with the wiper set to midrange. I check the wiper output with a volt meter and voltage always reads 1.65 v. Here is the code. 
     
    /*
     * File: main.c
     * Author: Eddie Hill
     *
     * Created on February 5, 2019, 9:42 AM
     */
    //// DSPIC33FJ128GP802 Configuration Bit Settings
    //
    //// 'C' source line config statements

    // FBS
    #pragma config BWRP = WRPROTECT_OFF // Boot Segment Write Protect (Boot Segment may be written)
    #pragma config BSS = NO_FLASH // Boot Segment Program Flash Code Protection (No Boot program Flash segment)
    #pragma config RBS = NO_RAM // Boot Segment RAM Protection (No Boot RAM)

    // FSS
    #pragma config SWRP = WRPROTECT_OFF // Secure Segment Program Write Protect (Secure segment may be written)
    #pragma config SSS = NO_FLASH // Secure Segment Program Flash Code Protection (No Secure Segment)
    #pragma config RSS = NO_RAM // Secure Segment Data RAM Protection (No Secure RAM)

    // FGS
    #pragma config GWRP = OFF // General Code Segment Write Protect (User program memory is not write-protected)
    #pragma config GSS = OFF // General Segment Code Protection (User program memory is not code-protected)

    // FOSCSEL
    #pragma config FNOSC = LPRCDIVN // Oscillator Mode (Internal Fast RC (FRC) with divide by N)
    #pragma config IESO = ON // Internal External Switch Over Mode (Start-up device with FRC, then automatically switch to user-selected oscillator source when ready)

    // FOSC
    #pragma config POSCMD = NONE // Primary Oscillator Source (Primary Oscillator Disabled)
    #pragma config OSCIOFNC = OFF // OSC2 Pin Function (OSC2 pin has clock out function)
    #pragma config IOL1WAY = ON // Peripheral Pin Select Configuration (Allow Only One Re-configuration)
    #pragma config FCKSM = CSDCMD // Clock Switching and Monitor (Both Clock Switching and Fail-Safe Clock Monitor are disabled)

    // FWDT
    #pragma config WDTPOST = PS32768 // Watchdog Timer Postscaler (1:32,768)
    #pragma config WDTPRE = PR128 // WDT Prescaler (1:128)
    #pragma config WINDIS = OFF // Watchdog Timer Window (Watchdog Timer in Non-Window mode)
    #pragma config FWDTEN = ON // Watchdog Timer Enable (Watchdog timer always enabled)

    // FPOR
    #pragma config FPWRT = PWR128 // POR Timer Value (128ms)
    #pragma config ALTI2C = OFF // Alternate I2C pins (I2C mapped to SDA1/SCL1 pins)

    // FICD
    #pragma config ICS = PGD1 // Comm Channel Select (Communicate on PGC1/EMUC1 and PGD1/EMUD1)
    #pragma config JTAGEN = OFF // JTAG Port Enable (JTAG is Disabled)

    //// #pragma config statements should precede project file includes.
    //// Use project enums instead of #define for ON and OFF.

    // must be defined before libpic30.h is included
    #define FCY 7370000UL // define clk frequency to use delay routines in libpic30

    #include <pps.h>
    #include <spi.h>
    #include <libpic30.h>

    #include "xc.h"

    // prototypes
    void openSPI(void);
    void SPIWrite(uint16_t data);
    void SPIClose(void);

    // pin definitions
    #define LED LATAbits.LATA1

    int main(void) {
        uint16_t command;
        
        AD1PCFGL = 0xffff; // make all IO pins digital

        TRISBbits.TRISB13 = 0; // make RB13 an output,
        TRISBbits.TRISB14 = 0; // make RB14 an output,
        TRISBbits.TRISB15 = 0; // make RB15 an output,

        TRISAbits.TRISA1 = 0; // make RC9 an output, LED

        // re-map SPI1 SCLK and SDO1 pins to RP14 and RP15
        //PPSUnLock;
        iPPSOutput(OUT_PIN_PPS_RP14, OUT_FN_PPS_SCK1);
        iPPSOutput(OUT_PIN_PPS_RP15, OUT_FN_PPS_SDO1);
        iPPSOutput(OUT_PIN_PPS_RP13, OUT_FN_PPS_SS1);
        //PPSLock;

        openSPI(); // initialize and open SPI1

        command = 0x5511;

        while (1) {
            // to see if it's alive
            LED = 1; // turn on LED
            __delay_ms(2000); // ~1 sec delay
            LED = 0; // turn LED off

            // write to the SPI digital pot
            SPIWrite(command); // set the digital pot to 170th step
        }
        return 0;
    }

    void openSPI(void) {
        unsigned int SPICONValue;
        unsigned int SPISTATValue;
        //__builtin_write_OSCCONL(OSCCON & 0xBF);
        // set SPICON and SPISTAT registers
        SPICONValue = FRAME_ENABLE_OFF & //FRMEN: 0 = Framed SPI support disabled
                FRAME_SYNC_OUTPUT & //SPIFSD: 0 = Frame sync pulse output (master)
                ENABLE_SDO_PIN & //DISSDO: 0 = SDOx pin is controlled by the module
                SPI_MODE16_ON & //MODE16: 1 = Communication is 2 byte-wide (16 bits)
                SPI_SMP_OFF & //SMP: 0 = Input data sampled at middle of data output time
                SPI_CKE_OFF & //CKE: 0 = Serial output data changes on transition from Idle clk state to active clk state
                SLAVE_ENABLE_ON & //SSEN: 0 = SS pin not used by module. Pin controlled by port function
                CLK_POL_ACTIVE_HIGH & //CKP: 0 = SS pin not used by module. Pin controlled by port function
                MASTER_ENABLE_ON & //MSTEN: 1 = Master mode
                SEC_PRESCAL_3_1 & //SPRE<2:0>: Secondary Prescale 3:1
                PRI_PRESCAL_1_1; //PPRE<1:0> Primary Prescale 1:1
        SPISTATValue = SPI_ENABLE & //SPIEN: 1 = Enables module and configures SCKx, SDOx, SDIx and SSx as serial port pins
                SPI_IDLE_CON & //SPISIDL: 0 = Continue module operation in Idle mode
                SPI_RX_OVFLOW_CLR; //SPIROV: 0 = No overflow has occurred. Clear receive overflow bit.
        OpenSPI1(SPICONValue, 0, SPISTATValue); // open SPI1
    }

    void SPIWrite(uint16_t data) {
        SPI1BUF = data;
        while (SPI1STATbits.SPITBF);
        SPI1BUF;
    }

    void SPIClose(void) {
        CloseSPI1();
    }

    post edited by ezedhill - 2019/02/07 19:42:09
    #12
    qhb
    Superb Member
    • Total Posts : 8712
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: Cannot get SPI1 to work with dsPIC33FJ128GP802 2019/02/07 19:51:46 (permalink)
    0
    This probably is not your problem, but in your write function
        while (SPI1STATbits.SPITBF);
    should be
        while (SPI1STATbits.SPIRBF);


    You didn't mention if your LED is flashing.
    As Susan mentioned, in Master mode, it is YOUR JOB to control the SS pin, the SPI peripheral doesn't do it.
     
    #13
    ezedhill
    Starting Member
    • Total Posts : 36
    • Reward points : 0
    • Joined: 2019/02/07 06:43:05
    • Location: 0
    • Status: online
    Re: Cannot get SPI1 to work with dsPIC33FJ128GP802 2019/02/07 20:04:33 (permalink)
    0
    No, the LED is not flashing. It turns on and stays on. So you're saying the program is getting hung up in the write function. Well, that makes sense. Is it me, or do others find setting these devices up a little confusing. I'm new to the dsPIC and the MPLAB X ide. 
    #14
    qhb
    Superb Member
    • Total Posts : 8712
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: Cannot get SPI1 to work with dsPIC33FJ128GP802 2019/02/07 20:20:56 (permalink)
    0
    ezedhill
    No, the LED is not flashing.

    That is a crucial detail you should have mentioned at the start.
     

    It turns on and stays on. So you're saying the program is getting hung up in the write function. Well, that makes sense.

    Have you tried just stepping through your code with the PK4 in debug mode?
    That would very quickly let you see where it is getting stuck.
     
    #15
    ezedhill
    Starting Member
    • Total Posts : 36
    • Reward points : 0
    • Joined: 2019/02/07 06:43:05
    • Location: 0
    • Status: online
    Re: Cannot get SPI1 to work with dsPIC33FJ128GP802 2019/02/07 21:14:07 (permalink)
    0
    I didn't know it was flashing, because it's flashing every few minutes. N, I haven't stepped through the code. I'm starting to do this now. 
     
    So do I set the CS low just before calling SPIWrite?
    #16
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 2959
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Cannot get SPI1 to work with dsPIC33FJ128GP802 2019/02/07 21:19:36 (permalink)
    0
    Try modifying  this part:
    // to see if it's alive
            LED ^= 1; // turn on LED
            __delay_ms(1000); // ~1 sec delay

    The led may have turned off but it was immediately turned on again.


    post edited by Gort2015 - 2019/02/07 21:22:18

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #17
    qhb
    Superb Member
    • Total Posts : 8712
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: Cannot get SPI1 to work with dsPIC33FJ128GP802 2019/02/07 21:20:26 (permalink)
    0
    ezedhill
    So do I set the CS low just before calling SPIWrite?

    Probably yes.
    What does the datasheet for the device you are trying to talk to say the CS signal has to do?
     
    #18
    ezedhill
    Starting Member
    • Total Posts : 36
    • Reward points : 0
    • Joined: 2019/02/07 06:43:05
    • Location: 0
    • Status: online
    Re: Cannot get SPI1 to work with dsPIC33FJ128GP802 2019/02/07 21:35:35 (permalink)
    0
    Yes, the device is a digital pot. 
    #19
    ezedhill
    Starting Member
    • Total Posts : 36
    • Reward points : 0
    • Joined: 2019/02/07 06:43:05
    • Location: 0
    • Status: online
    Re: Cannot get SPI1 to work with dsPIC33FJ128GP802 2019/02/07 21:37:16 (permalink)
    0
    I'll try this. 
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2019 APG vNext Commercial Version 4.5