• AVR Freaks

Hot!PIC16LF18344 SPI SDI not in SSP1BUF after read

Page: 12 > Showing page 1 of 2
Author
JustRob
Super Member
  • Total Posts : 587
  • Reward points : 0
  • Joined: 2008/09/04 12:49:27
  • Location: Hudson Massachusetts United States
  • Status: offline
2020/07/03 09:02:46 (permalink)
0

PIC16LF18344 SPI SDI not in SSP1BUF after read

I am using the pic16lf18344 in a design with MPLAB X IDE v5.40 and the XC8 v2.20 compiler.
 
I am trying to do a simple spi read register of a gyro/accelerometer breakout board.  I am trying to read the WHO_AM_I register (0x0F) of the gyro chip which should return 0x69.
 
I am getting the correct response on a signal analyzer but the actual SSP1BUF register shows 0x0 after the read.  Below is the signal analyzer plot of the gyro_who_am_I() function, the gyro_who_am_I() function and the SPI_read_write() function:

uint8_t gyro_who_am_i(){
    
    uint8_t value = 0;
    
    gyro_cs_low();
    
    value = (SPI_read_write(0x80 + WHO_AM_I));
    value = SPI_read_write(0x00);

    gyro_cs_high();
    
    return value;
    
}

 

uint8_t SPI_read_write(uint8_t data_byte){

    volatile uint8_t temp = SSP1BUF;
    
    SSP1BUF = data_byte;
    NOP();
    NOP();
    
    while(!SSP1STATbits.BF);
    temp = SSP1BUF;
    
    return SSP1BUF;
    
} // SPI_read_write

 
Note: When I break on return SSP1BUF; the value in temp = 0x0 even though the signal analyzer SDI shows SSP1BUF should contain 0x69
 

Attached Image(s)


Sometimes the obstacles in our path ARE our path!
 
https://abateandempower.com/
#1

28 Replies Related Threads

    NorthGuy
    Super Member
    • Total Posts : 6228
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: PIC16LF18344 SPI SDI not in SSP1BUF after read 2020/07/03 09:14:55 (permalink)
    +1 (1)
    Your code reads SPIBUF twice:
     
    temp = SSP1BUF;
    return SSP1BUF;

     
    Each read retrieves a new value. You need to do only one read:
     
    return SSP1BUF;

     
    #2
    JustRob
    Super Member
    • Total Posts : 587
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: Hudson Massachusetts United States
    • Status: offline
    Re: PIC16LF18344 SPI SDI not in SSP1BUF after read 2020/07/03 09:37:25 (permalink)
    0
    That's how I first had it.  I put the temp = SSP1BUF so I could bypass any debugger issue.  This is how I have it now:

    uint8_t SPI_read_write(uint8_t data_byte){

    // volatile uint8_t temp = SSP1BUF;
        
        SSP1BUF = data_byte;
        NOP();
        NOP();
        
        while(!SSP1STATbits.BF);
    // temp = SSP1BUF;
        
        return SSP1BUF;
        
    } // SPI_read_write

     
     

    Sometimes the obstacles in our path ARE our path!
     
    https://abateandempower.com/
    #3
    JustRob
    Super Member
    • Total Posts : 587
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: Hudson Massachusetts United States
    • Status: offline
    Re: PIC16LF18344 SPI SDI not in SSP1BUF after read 2020/07/03 09:46:56 (permalink)
    0
    I wonder if it is my PPS assignments:
     
    #define SPI_SCK_TRIS TRISCbits.TRISC0
    #define SPI_SDO_TRIS TRISCbits.TRISC1
    #define SPI_SDI_TRIS TRISAbits.TRISA2
    #define SPI_GYRO_CSn_TRIS TRISCbits.TRISC6

    #define SPI_SDI_PPS 0b00010 // SPI SDI peripheral select pin
    #define SPI_GYRO_CS_PPS RC6PPS // SPI CS peripheral select pin
    #define SPI_SCK_PPS 0b11000
    #define SPI_SDO_PPS 0b11001
        

     
    void system_init(){
        
        X_ENABLEn_LATCH = DISABLE;
        X_ENABLEn_ANSEL = DIGITAL;
        X_ENABLEn_TRIS = OUTPUT; // set as output
        X_RESETn_LATCH = 0;
        X_RESETn_ANSEL = DIGITAL;
        X_RESETn_TRIS = OUTPUT; // set as output
     
        Y_ENABLEn_LATCH = DISABLE;
        Y_ENABLEn_ANSEL = DIGITAL;
        Y_ENABLEn_TRIS = OUTPUT; // set as output
        Y_RESETn_LATCH = 0;
        Y_RESETn_ANSEL = DIGITAL;
        Y_RESETn_TRIS = OUTPUT; // set as output
        
        X_DIR_LATCH = RIGHT;
        X_STEP_LATCH = 0;
        MS1_LATCH = 0;
        
        Y_DIR_LATCH = UP;
        Y_STEP_LATCH = 0;
        
        SPI_SCK_LATCH = 1;
        SPI_SDO_LATCH = 1;
        SPI_GYRO_CSn_LATCH = 1;
        
        X_DIR_ANSEL = DIGITAL;
        X_STEP_ANSEL = DIGITAL;
        MS1_ANSEL = DIGITAL;
        
        Y_DIR_ANSEL = DIGITAL;
        Y_STEP_ANSEL = DIGITAL;
        
        SPI_SCK_TRIS = OUTPUT; // SPI SCK set to output
        SPI_SDI_TRIS = INPUT; // SPI SDI set to input
        SPI_SDO_TRIS = OUTPUT; // SPI SDO data set to output
        SPI_GYRO_CSn_TRIS = OUTPUT; // SPI GYRO cs set to output
        
        // assign peripheral pins per register 13-2
        //SSP1CLKPPS = SPI_SCK_PPS; // SPI SCK peripheral select pin
        RC0PPS = SPI_SCK_PPS;
        RC1PPS = SPI_SDO_PPS;
        RA2PPS = SPI_SDI_PPS;
        SSP1DATPPS = SPI_SDI_PPS; // SPI SDI peripheral select pin
        
        SSP1SSPPS = SPI_GYRO_CS_PPS; // SPI CS peripheral select pin

        SPI_SCK_ANSEL = DIGITAL;
        SPI_SDO_ANSEL = DIGITAL;
        SPI_SDI_ANSEL = DIGITAL;
        SPI_GYRO_CSn_ANSEL = DIGITAL;
        
        X_DIR_TRIS = OUTPUT; // set as output
        X_STEP_TRIS = OUTPUT; // set as output
        MS1_TRIS = OUTPUT; // set as output
        
        Y_DIR_TRIS = OUTPUT; // set as output
        Y_STEP_TRIS = OUTPUT; // set as output
        
        SPI_SCK_LATCH = 1;
        SPI_SDO_LATCH = 1;
        SPI_GYRO_CSn_LATCH = 1;

        SPI_SCK_TRIS = OUTPUT; // set as output
        SPI_SDO_TRIS = OUTPUT; // set as output
        SPI_SDI_TRIS = INPUT; // set as input
        SPI_GYRO_CSn_TRIS = OUTPUT; // set as output


     

    Sometimes the obstacles in our path ARE our path!
     
    https://abateandempower.com/
    #4
    NorthGuy
    Super Member
    • Total Posts : 6228
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: PIC16LF18344 SPI SDI not in SSP1BUF after read 2020/07/03 09:52:36 (permalink)
    0
    JustRob
    That's how I first had it.  I put the temp = SSP1BUF so I could bypass any debugger issue.



    If you watch SSP1BUF in debugger, it will be a separate read. Therefore if you want to debug, do
     
    temp = SSP1BUF;
    return temp;

     
    and watch "temp".
    #5
    JustRob
    Super Member
    • Total Posts : 587
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: Hudson Massachusetts United States
    • Status: offline
    Re: PIC16LF18344 SPI SDI not in SSP1BUF after read 2020/07/03 10:02:12 (permalink)
    0
    I did try that still with no value in temp even though my signal analyzer shows 0x69 on the SDI signal right before the return.

    Sometimes the obstacles in our path ARE our path!
     
    https://abateandempower.com/
    #6
    NorthGuy
    Super Member
    • Total Posts : 6228
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: PIC16LF18344 SPI SDI not in SSP1BUF after read 2020/07/03 10:04:53 (permalink)
    0
    Concerning PPS, you do not need this line:
     
    RA2PPS = SPI_SDI_PPS;

     
    This would route the output of PWM5 (because SPI_SDI_PPS is defined as 2) to RA2. This line should be enough:
     
    SSP1DATPPS = SPI_SDI_PPS;

     
     
     
     
    #7
    JustRob
    Super Member
    • Total Posts : 587
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: Hudson Massachusetts United States
    • Status: offline
    Re: PIC16LF18344 SPI SDI not in SSP1BUF after read 2020/07/03 10:07:43 (permalink)
    0
    My current SPI_read_write()

    uint8_t SPI_read_write(uint8_t data_byte){

        volatile uint8_t temp = 0;
        
        SSP1BUF = data_byte;
        NOP();
        NOP();
        
        while(!SSP1STATbits.BF);
        temp = SSP1BUF;
        
        return temp;
        
    } // SPI_read_write


     

    Sometimes the obstacles in our path ARE our path!
     
    https://abateandempower.com/
    #8
    JustRob
    Super Member
    • Total Posts : 587
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: Hudson Massachusetts United States
    • Status: offline
    Re: PIC16LF18344 SPI SDI not in SSP1BUF after read 2020/07/03 11:14:39 (permalink)
    +1 (1)
    I'm fairly certain my problem is in the pps assignments:
        RC0PPS = SPI_SCK_PPS;
        RC1PPS = SPI_SDO_PPS;
        SSP1DATPPS = SPI_SDI_PPS; // SPI SDI peripheral select pin and SPI_SDI_PPS is defined as 0b00010
     

    Sometimes the obstacles in our path ARE our path!
     
    https://abateandempower.com/
    #9
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16LF18344 SPI SDI not in SSP1BUF after read 2020/07/03 16:05:31 (permalink)
    0
    I think it's more lilkely you have left the SDI pin in analog mode, as you've made no mention of switching it to digital.
    Can only guess, as you haven't posted a complete test program.
     
    I missed that init function in post#4.
    It looks like you ARE trying to switch SDI to digital, but you didn't show the definition for SPI_SDI_ANSEL so can't be sure.
    I would double check this, as your reported symptoms are just like the SDI pin is always reading zero.
    post edited by ric - 2020/07/03 16:11:56

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #10
    JustRob
    Super Member
    • Total Posts : 587
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: Hudson Massachusetts United States
    • Status: offline
    Re: PIC16LF18344 SPI SDI not in SSP1BUF after read 2020/07/04 04:43:35 (permalink)
    0
    I do set SDI to digital
    My init:
    void system_init(){
        
        X_ENABLEn_LATCH = DISABLE;
        X_ENABLEn_ANSEL = DIGITAL;
        X_ENABLEn_TRIS = OUTPUT; // set as output
        X_RESETn_LATCH = 0;
        X_RESETn_ANSEL = DIGITAL;
        X_RESETn_TRIS = OUTPUT; // set as output
     
        Y_ENABLEn_LATCH = DISABLE;
        Y_ENABLEn_ANSEL = DIGITAL;
        Y_ENABLEn_TRIS = OUTPUT; // set as output
        Y_RESETn_LATCH = 0;
        Y_RESETn_ANSEL = DIGITAL;
        Y_RESETn_TRIS = OUTPUT; // set as output
        
        X_DIR_LATCH = RIGHT;
        X_STEP_LATCH = 0;
        MS1_LATCH = 0;
        
        Y_DIR_LATCH = UP;
        Y_STEP_LATCH = 0;
        
        SPI_SCK_LATCH = 1;
        SPI_SDO_LATCH = 1;
        SPI_GYRO_CSn_LATCH = 1;
        
        X_DIR_ANSEL = DIGITAL;
        X_STEP_ANSEL = DIGITAL;
        MS1_ANSEL = DIGITAL;
        
        Y_DIR_ANSEL = DIGITAL;
        Y_STEP_ANSEL = DIGITAL;
        
        SPI_SCK_TRIS = OUTPUT; // SPI SCK set to output
        SPI_SDI_TRIS = INPUT; // SPI SDI set to input
        SPI_SDO_TRIS = OUTPUT; // SPI SDO data set to output
        SPI_GYRO_CSn_TRIS = OUTPUT; // SPI GYRO cs set to output
        
        // assign peripheral pins per register 13-2
        //SSP1CLKPPS = SPI_SCK_PPS; // SPI SCK peripheral select pin
        RC0PPS = SPI_SCK_PPS;
        RC1PPS = SPI_SDO_PPS;
        SSP1DATPPS = SPI_SDI_PPS; // SPI SDI peripheral select pin
        
        SSP1SSPPS = SPI_GYRO_CS_PPS; // SPI CS peripheral select pin

        SPI_SCK_ANSEL = DIGITAL;
        SPI_SDO_ANSEL = DIGITAL;
        SPI_SDI_ANSEL = DIGITAL;
        SPI_GYRO_CSn_ANSEL = DIGITAL;
        
        X_DIR_TRIS = OUTPUT; // set as output
        X_STEP_TRIS = OUTPUT; // set as output
        MS1_TRIS = OUTPUT; // set as output
        
        Y_DIR_TRIS = OUTPUT; // set as output
        Y_STEP_TRIS = OUTPUT; // set as output
        
        SPI_SCK_LATCH = 1;
        SPI_SDO_LATCH = 1;
        SPI_GYRO_CSn_LATCH = 1;

        SPI_SCK_TRIS = OUTPUT; // set as output
        SPI_SDO_TRIS = OUTPUT; // set as output
        SPI_SDI_TRIS = INPUT; // set as input
        SPI_GYRO_CSn_TRIS = OUTPUT; // set as output


        // init x stepper
        X_STEP_LATCH = 0;
        X_RESETn_LATCH = 1;
        MS1_LATCH = 1;
        X_ENABLEn_LATCH = DISABLE;
        Y_DIR_LATCH = UP;
        
        // init y stepper
        Y_STEP_LATCH = 0;
        Y_RESETn_LATCH = 1;
        MS1_LATCH = 1;
        Y_ENABLEn_LATCH = DISABLE;
        
    }

     
    And definitions:
    #ifndef SYSTEM_RESOURCES_H
    #define SYSTEM_RESOURCES_H

    #ifdef __cplusplus
    extern "C" {
    #endif

    #include <xc.h>
    #include <stdint.h>
        
    #define INPUT 1
    #define OUTPUT 0
    #define RIGHT 0
    #define LEFT 1
    #define UP 0
    #define DOWN 1
    #define FULL_STEP 0
    #define HALF_STEP 1
    #define ENABLE 0
    #define DISABLE 1
    #define ANALOG 1
    #define DIGITAL 0
        
    #define X_DIR_LATCH LATAbits.LATA5
    #define X_STEP_LATCH LATAbits.LATA4
    #define X_RESETn_LATCH LATCbits.LATC5
    #define MS1_LATCH LATCbits.LATC4
    #define X_ENABLEn_LATCH LATCbits.LATC3

    #define Y_DIR_LATCH LATBbits.LATB6
    #define Y_STEP_LATCH LATBbits.LATB5
    #define Y_RESETn_LATCH LATBbits.LATB4
    #define Y_ENABLEn_LATCH LATCbits.LATC2

    #define SPI_SCK_LATCH LATCbits.LATC0
    #define SPI_SDO_LATCH LATCbits.LATC1
    #define SPI_GYRO_CSn_LATCH LATCbits.LATC6

    #define X_DIR_ANSEL ANSELAbits.ANSA5
    #define X_STEP_ANSEL ANSELAbits.ANSA4
    #define X_RESETn_ANSEL ANSELCbits.ANSC5
    #define MS1_ANSEL ANSELCbits.ANSC4
    #define X_ENABLEn_ANSEL ANSELCbits.ANSC3
        
    #define Y_DIR_ANSEL ANSELBbits.ANSB6
    #define Y_STEP_ANSEL ANSELBbits.ANSB5
    #define Y_RESETn_ANSEL ANSELBbits.ANSB4
    #define Y_ENABLEn_ANSEL ANSELCbits.ANSC2

    #define SPI_SCK_ANSEL ANSELCbits.ANSC0
    #define SPI_SDO_ANSEL ANSELCbits.ANSC1
    #define SPI_SDI_ANSEL ANSELAbits.ANSA2
    #define SPI_GYRO_CSn_ANSEL ANSELCbits.ANSC6

    #define X_DIR_TRIS TRISAbits.TRISA5
    #define X_STEP_TRIS TRISAbits.TRISA4
    #define X_RESETn_TRIS TRISCbits.TRISC5
    #define MS1_TRIS TRISCbits.TRISC4
    #define X_ENABLEn_TRIS TRISCbits.TRISC3
        
    #define Y_DIR_TRIS TRISBbits.TRISB6
    #define Y_STEP_TRIS TRISBbits.TRISB5
    #define Y_RESETn_TRIS TRISBbits.TRISB4
    #define Y_ENABLEn_TRIS TRISCbits.TRISC2

    #define SPI_SCK_TRIS TRISCbits.TRISC0
    #define SPI_SDO_TRIS TRISCbits.TRISC1
    #define SPI_SDI_TRIS TRISAbits.TRISA2
    #define SPI_GYRO_CSn_TRIS TRISCbits.TRISC6

    #define SPI_SDI_PPS 0b00010 // SPI SDI peripheral select pin
    #define SPI_GYRO_CS_PPS RC6PPS // SPI CS peripheral select pin
    #define SPI_SCK_PPS 0b11000
    #define SPI_SDO_PPS 0b11001
        
        

    void system_init(void);

    #ifdef __cplusplus
    }
    #endif

    #endif /* SYSTEM-RESOURCES_H */



    Sometimes the obstacles in our path ARE our path!
     
    https://abateandempower.com/
    #11
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3984
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: PIC16LF18344 SPI SDI not in SSP1BUF after read 2020/07/04 07:09:47 (permalink)
    -2 (2)
    There is an unlock sequence when setting RPs.
    Lock after.
     
    Check the default settings.
     

    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.
    #12
    du00000001
    Just Some Member
    • Total Posts : 3851
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: PIC16LF18344 SPI SDI not in SSP1BUF after read 2020/07/04 07:17:45 (permalink)
    0
    Hi Rob,
     
    would be nice if you could provide the whole project (zip and add .txt to the filename to  enable he uploading).
    We might need to see your onfiguration, ALL of the #defines etc.
     
    P.S.: How is the "camera remote" project ?
    [Edit/Added]
    Afterthought: Or is this '18344 on the receiving end? No luck with using the ESP for stepper control?
    post edited by du00000001 - 2020/07/04 07:23:21

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #13
    JustRob
    Super Member
    • Total Posts : 587
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: Hudson Massachusetts United States
    • Status: offline
    Re: PIC16LF18344 SPI SDI not in SSP1BUF after read 2020/07/04 07:39:02 (permalink)
    0
    Ask and ye shall receive...
    Project zip is attached as *.txt

    Sometimes the obstacles in our path ARE our path!
     
    https://abateandempower.com/
    #14
    JustRob
    Super Member
    • Total Posts : 587
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: Hudson Massachusetts United States
    • Status: offline
    Re: PIC16LF18344 SPI SDI not in SSP1BUF after read 2020/07/04 08:02:52 (permalink)
    0
    P.S.: How is the "camera remote" project ?

    This is the camera project except I'm using a sparkfun gyro/accelerometer module instead of trying to hand solder my own gyro IC.
     
    SDI changed from RB4 to RA2 ~ I'm initializing everything the same way.

    Sometimes the obstacles in our path ARE our path!
     
    https://abateandempower.com/
    #15
    du00000001
    Just Some Member
    • Total Posts : 3851
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: PIC16LF18344 SPI SDI not in SSP1BUF after read 2020/07/04 08:33:07 (permalink)
    0
    Might take some hours until I find the time ...
     
    One tip in advance: MCC is really your friend when setting up the hardware (peripherals, pin configuration, PPS etc.)
    Even when not intending to use the whole code generated, the initialization values are - - - valuable. It requires some sifting through all the code generated (but you'll soon get accustomed to that), but it's worth the effort.

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #16
    JustRob
    Super Member
    • Total Posts : 587
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: Hudson Massachusetts United States
    • Status: offline
    Re: PIC16LF18344 SPI SDI not in SSP1BUF after read 2020/07/04 08:35:04 (permalink)
    0
    I will give that a try

    Sometimes the obstacles in our path ARE our path!
     
    https://abateandempower.com/
    #17
    JustRob
    Super Member
    • Total Posts : 587
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: Hudson Massachusetts United States
    • Status: offline
    Re: PIC16LF18344 SPI SDI not in SSP1BUF after read 2020/07/04 12:49:45 (permalink)
    0
    I used the code configurator and I don't see where I'm doing anything different.

    Sometimes the obstacles in our path ARE our path!
     
    https://abateandempower.com/
    #18
    JustRob
    Super Member
    • Total Posts : 587
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: Hudson Massachusetts United States
    • Status: offline
    Re: PIC16LF18344 SPI SDI not in SSP1BUF after read 2020/07/05 12:15:03 (permalink)
    0
    Here is page 1 of the schematic. 
    I didn't foresee a problem of putting SDI to RA2.  I'm using the 20 pin package and don't see a peripheral conflict but I am still not seeing why data on RA2 (SDI) is not being read into the SSP1BUF register.
     

    Attached Image(s)


    Sometimes the obstacles in our path ARE our path!
     
    https://abateandempower.com/
    #19
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16LF18344 SPI SDI not in SSP1BUF after read 2020/07/05 13:13:21 (permalink)
    0
    Have you tried just looping back SDO to SDI (with the slave disconnected) to see if you see your own data?
    (Or alternatively, pulling SDI high to see if you get 0xFF?)
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2020 APG vNext Commercial Version 4.5