• AVR Freaks

Hot!SPI communication failure between two PIC16F15345

Page: 12 > Showing page 1 of 2
Author
mexco
Starting Member
  • Total Posts : 60
  • Reward points : 0
  • Joined: 2009/08/30 07:02:44
  • Location: 0
  • Status: offline
2020/03/28 00:54:45 (permalink)
0

SPI communication failure between two PIC16F15345

I tried to transfer data between two microcontrollers as per circuit shown. When master has buffer full, it raises m_rdy high & slave raises s_rdy high. I observed using Digital Oscilloscope, that proper data is send by slave & it reaches upto SDI(RC0) of Master. But, data is not shown / latched in SSP1BUF of master. I tried all possible ways like : PPS1WAY '0' and '1', both cases and other trials also.

Attached Image(s)

#1

27 Replies Related Threads

    ric
    Super Member
    • Total Posts : 28390
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: SPI communication failure between two PIC16F15345 2020/03/28 02:09:23 (permalink)
    +1 (1)
    Just lookin gat the Master, that's plainly not all your code.
    The config settings are missing, there's no definition for "portc_tris"
     
    void main()
    should be
    void main(void)
     
    Your diagram shows RB4 as an output, but you never write to TRISB. Is that what "portc_tris" was meant to be?
    Your code is using RB4 as an input, and RB6 as an output.
     
    Always use LATx registers for outputs, so:
    #define valid_master RB6
    should be
    #define valid_master LATB6
     
    You are not using the SS pin on the slave. That is silly, it means more likely than not, your master and slave will not be in synch. You need SS to force the slave ontot the same bit clock as the Master.
     
     

    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!
    #2
    mexco
    Starting Member
    • Total Posts : 60
    • Reward points : 0
    • Joined: 2009/08/30 07:02:44
    • Location: 0
    • Status: offline
    Re: SPI communication failure between two PIC16F15345 2020/03/28 05:00:04 (permalink)
    0
    Dear LIC, thanks for prompt reply. I have shown partial program here. Actually , all the above changes you suggested, are in actual program, except LATB6 instead of RB6 like. But, there is no problem with these pins RB4 and RB6. In attachmrnt, I have shown the SPI Clock sent out form Master and data received from Slave on SDI of master. The data sent was 0x67, same is shown in screen shot of DSO. But, this data is not stored in ssp_rcv[] array of Maste during ISR. I have tried using Debugger PICKIT4 and stopping the program at    tmp =SSP1BUF; instruction in ISR, but SSP1BUG shows either 0x00 or 0xff. why ?
     
    I tried to generate PPS code using MCC. There was only differrence : RC0PPS = 0 ; was added by MCC and remaining PPS values are same. Then, Clock does not come out of Master, why ?
    what is purpose of RC0PPS = 0x00;, while in Table of PPS OUT, in dataheet, there no row/column with register value to be 0x00;
    post edited by mexco - 2020/03/28 05:04:53

    Attached Image(s)

    #3
    LdB_ECM
    Super Member
    • Total Posts : 435
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: SPI communication failure between two PIC16F15345 2020/03/28 06:02:34 (permalink)
    0
    On your slave are you sure about
    SSP1CON1 = 0x05;

    Reading the datasheet I would have gone for
    SSP1CON1 = 0x25;

    #4
    ric
    Super Member
    • Total Posts : 28390
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: SPI communication failure between two PIC16F15345 2020/03/28 14:57:46 (permalink)
    0
    LdB_ECM
    On your slave are you sure about
    SSP1CON1 = 0x05;

    Reading the datasheet I would have gone for
    SSP1CON1 = 0x25;


    He sets SSPEN by itself a couple of lines later.
     

    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!
    #5
    ric
    Super Member
    • Total Posts : 28390
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: SPI communication failure between two PIC16F15345 2020/03/28 15:02:46 (permalink)
    +2 (2)
    mexco
    Dear LIC, thanks for prompt reply. I have shown partial program here.

    It wastes your time and ours to not post it all.
    More often than not, the problem is in what you didn't think was relevant.
     

    Actually , all the above changes you suggested, are in actual program, except LATB6 instead of RB6 like. But, there is no problem with these pins RB4 and RB6. In attachmrnt, I have shown the SPI Clock sent out form Master and data received from Slave on SDI of master. The data sent was 0x67, same is shown in screen shot of DSO. But, this data is not stored in ssp_rcv[] array of Maste during ISR. I have tried using Debugger PICKIT4 and stopping the program at    tmp =SSP1BUF; instruction in ISR, but SSP1BUG shows either 0x00 or 0xff. why ?
     

    Show me the correct entire program and I'll start trying to make guesses.

    I tried to generate PPS code using MCC. There was only differrence : RC0PPS = 0 ; was added by MCC and remaining PPS values are same. Then, Clock does not come out of Master, why ?

    Personally, I would leave out ALL the PPS locking and unlocking code until you have this working.

    what is purpose of RC0PPS = 0x00;, while in Table of PPS OUT, in dataheet, there no row/column with register value to be 0x00;

    0x00 is the power on value of each PPS output register. It just leaves the pin controlled by the LATx register, not any peripheral.
    If MCC is adding that to your Master code, it is a bug in MCC.
     

    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!
    #6
    mexco
    Starting Member
    • Total Posts : 60
    • Reward points : 0
    • Joined: 2009/08/30 07:02:44
    • Location: 0
    • Status: offline
    Re: SPI communication failure between two PIC16F15345 2020/03/29 01:04:07 (permalink)
    0
    Dear Frineds .. today I modified the complete ciruit. I used two laptops Lap1 and Lap2. Master was connected to Lap1 through PICKIT4 and USB to Serial converter. Similary, Slave also was connected to Lap2 using another PICKIT4 and USB to Serial convertet. On both latops, Hyper Terminal was run. Then 'A' is sent from Master to slave & 'a' is sent from Slave to master using SPI. on both sides, the received data was displayed in Hyper Terminal Window. It was observed that Slave receives the data corrrectly, but Master does not. Master SSP1BUF always shows 0x00 received. Complete program Files run on both laptops are in attachment.
    #7
    ric
    Super Member
    • Total Posts : 28390
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: SPI communication failure between two PIC16F15345 2020/03/29 01:41:38 (permalink)
    +1 (1)
    Personally, I would not have used an interrupt service in the Master for the SSP.
    As you have, you must add a "volatile" qualifier when you create the "tmp" variable.
    This is because you are writing to it inside the ISR, which the non-interrupt code has no knowledge of it, so the optimiser will assume it never changes.
     

    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!
    #8
    mexco
    Starting Member
    • Total Posts : 60
    • Reward points : 0
    • Joined: 2009/08/30 07:02:44
    • Location: 0
    • Status: offline
    Re: SPI communication failure between two PIC16F15345 2020/03/29 04:02:06 (permalink)
    0
    Dear Friends, as Ric, suggested to try without interrupt.. I tried :
     
    void main(void)
    {
    ......
    ...  initialization same as in master.c
    SSP1BUF = 'A';
     while(1)
        {
            while(!SSP1IF);
            tmp = SSP1BUF;
            SSP1IF = 0; 
            unsigned char a = tmp+1;            
            transmit_usart_byte(a);transmit_usart_byte('\r');transmit_usart_byte('\n');
            SSP1BUF= a;
      }
    }
    remaining part as shown in matster.c, but without SSP1IF handling in ISR. Even now, same problem that master does not sample the SDI data.. it means, to use RC1 as SDI, the PPS code may be wrong. Presently, SSP1DATPPS = 0x11; is used.
    #9
    ric
    Super Member
    • Total Posts : 28390
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: SPI communication failure between two PIC16F15345 2020/03/29 04:06:11 (permalink)
    +1 (1)
    mexco
    ...  initialization same as in master.c

    Except I hope you did NOT enable SSPIE this time.
     

    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
    mexco
    Starting Member
    • Total Posts : 60
    • Reward points : 0
    • Joined: 2009/08/30 07:02:44
    • Location: 0
    • Status: offline
    Re: SPI communication failure between two PIC16F15345 2020/03/29 20:38:52 (permalink)
    0
    YES, SSP1IE is not enabled in above program without interrupt. Now, I decided to use default SDI1 pin as SDI for Master. I observed a mistake in datasheet of 16F15345. The screen shots of those pages in att. In two tables default SDI1 is shown at RB6 and in PPS table SDI1 at RB4.
    AS well, I saw in the errata, nowhere this printing mistake has been mentioned and corrected.
    post edited by mexco - 2020/03/29 20:40:09

    Attached Image(s)

    #11
    ric
    Super Member
    • Total Posts : 28390
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: SPI communication failure between two PIC16F15345 2020/03/29 21:09:57 (permalink)
    +1 (1)
    That's why experienced programmers never trust defaults...
     

    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!
    #12
    mexco
    Starting Member
    • Total Posts : 60
    • Reward points : 0
    • Joined: 2009/08/30 07:02:44
    • Location: 0
    • Status: offline
    Re: SPI communication failure between two PIC16F15345 2020/03/30 22:50:17 (permalink)
    0
    Dear Friends, How to detect the default SDI1 pin. In datasheet in pin description RB6 and in PPS table RB4 is shown as default SDI1. I tested using RC1 as SDI1, but Master does not latch data in SSP1BUF from the pin used SDI.
    post edited by mexco - 2020/03/31 05:09:51
    #13
    ric
    Super Member
    • Total Posts : 28390
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: SPI communication failure between two PIC16F15345 2020/03/30 22:55:26 (permalink)
    +1 (1)
    If you set it yourself, it's irrelevant what the default is.
     

    Master does not latch data in SSP1BUF from this pin.

    How do you know?
    It's much more useful to report how you tested, and what you observed, than just what you THINK the problem is.
     
     

    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!
    #14
    mexco
    Starting Member
    • Total Posts : 60
    • Reward points : 0
    • Joined: 2009/08/30 07:02:44
    • Location: 0
    • Status: offline
    Re: SPI communication failure between two PIC16F15345 2020/03/31 05:08:59 (permalink)
    0
    Dear RIC and friends, How I tested that data is not being latched in SSP1BUF from SDI1 pin.
    I tried different methods: But in all methods, the PIN used as SDI1 was programmed as : Direction => Input, Mode => Digital, Pull - UP = OFF, Level Control => TTL, programmed as SDI1 using PPS.
    I took single screen shot of Master Clock and SDI data using DSO. I tried both Polling and interrupt methods for all these.
    Case 1: I used RC1 as SDI PPS. 'a' was sent by slave. It was tested that '0x61' reaches upto RC1 using DSO
    Case 2: I used RB4 as SDI1, 5 volt a square wave generated by slave, was sent to it. The wave reaches upto RB4, but data in SSP1BUF always shown 0xff. The  baud rate of master SPI clock is such that Time period  = 5 uS.
    Case 3: I used RB6 as SDI1, applied same square wave and same behavior of SSP1BUF. Screen shot and programs are shown in previous posts.
    From all these observations, I concluded that data is not being read and latched in SSP1BUF from the PIN used as SDI1. I have worked already on many PIC microcontrollers, never faced problem about SPI.
    To verify, pls,see the DSO screen shot in previous post and the program file of both Master and Slave also are there. This Problem is in Master Mode only. The slave latches SDI1 data properly.
    post edited by mexco - 2020/03/31 20:14:16
    #15
    ric
    Super Member
    • Total Posts : 28390
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: SPI communication failure between two PIC16F15345 2020/03/31 12:18:41 (permalink)
    +1 (1)
    So in case 2 + 3, you had 0xFF in SSPBUF after the transfer. Was case#1 the same?
    Is the SSPIF bit set after the transfer?
    Is the BF flag set after the transfer?
     
    Bottom, line, are the flags (SSPIF and BF) indicating that a transfer took place, but just the value in SSPBUF is always 0xFF after the transfer?

    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!
    #16
    mexco
    Starting Member
    • Total Posts : 60
    • Reward points : 0
    • Joined: 2009/08/30 07:02:44
    • Location: 0
    • Status: offline
    Re: SPI communication failure between two PIC16F15345 2020/03/31 19:41:12 (permalink)
    0
    Yes, SSP1IF is set and transfer is completed. Also whtaever data is sent out through SDO1, is received by Slave properly. But, Master does not read SDI1 pin. I tried to see the value in SSP1BUF in SFR while debugging with PICKIT4 and a breakpoint at:
    1. Just after while(!BF); when used polling method
    2. just after if(SSP1IF); when used ISR
    In. both cases, the data in SSP1BUF does not show the data properly. It seems, that SDI1 PPS table has some code mistake. SSP1BUF is being read from some other pin.
    In, previous post master.c and slave.c fies are there. Polling and Interrrput methods are both. Interrupt part is yet commented in those. You can comment Polling method and uncomment Interrupt method. 
    Now, I shall try to Simulte SPI just in software. But, the problem during simulation is that PPS block does not pass the logic to/ from peripheral.
    #17
    LdB_ECM
    Super Member
    • Total Posts : 435
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: SPI communication failure between two PIC16F15345 2020/03/31 21:07:46 (permalink)
    +1 (1)
    Which sounds exactly correct ... you seem to be expecting it to immediately daisy chain?
    If you look at the MISO it should look like below which is indeed 0xFF ... fire a dummy byte out the master after the first byte and tell us what happens.
     

     
    post edited by LdB_ECM - 2020/03/31 21:16:47
    #18
    mexco
    Starting Member
    • Total Posts : 60
    • Reward points : 0
    • Joined: 2009/08/30 07:02:44
    • Location: 0
    • Status: offline
    Re: SPI communication failure between two PIC16F15345 2020/03/31 22:25:27 (permalink)
    0
    Dear LDB_ECM and friends, I fired dummy byte as well. But, no success. I observed the following behavior :
    I used RB6 a SDI1. Used PICKIT4 to debug and forced breakpoint just after while(!SSP1IF); Polling method was used. After first time start or (on every restart without stopping the debugger),  the value in SSP1BUF is shown 0x00 continuously. The initial logic at SDI1 does not matter. When, logic at SDI1 (RB6) is changed, then, data in SSP1BUF becomes 0xff. So, I concluded that it has some link with IOC. The data change direction does not matter on SDI1 i.e. both Low to High or High to Low, make SSP1BUF = 0xff and then SSP1BUF stucks to 0xff after first change.
    I got through the Errata pdf of PIC16F15345. The correction about Slave mode only is given here. But, in my case, problem is in Master Mode.
    post edited by mexco - 2020/03/31 22:48:24

    Attached Image(s)

    #19
    LdB_ECM
    Super Member
    • Total Posts : 435
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: SPI communication failure between two PIC16F15345 2020/04/01 05:18:22 (permalink)
    +1 (1)
    So you are trying to daisychain the slave
    Read 32.2.4.1 Daisy-Chain Configuration in the datasheet page 384
    Then read the description on page 418.
     
    Note your current slave setting
    SSP1CON3 = 0;

     
    I am guessing with your current settings if you load a set value into the slave shift register it will shift out correctly.
     
    To that end it would be much more useful to see MISO into master versus SCLK waveforms rather than your trying to explain what you think is happening.
    post edited by LdB_ECM - 2020/04/01 05:31:55
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2020 APG vNext Commercial Version 4.5