• AVR Freaks

SPI beginner

Author
BulleTracker
Starting Member
  • Total Posts : 34
  • Reward points : 0
  • Joined: 2008/12/29 02:47:52
  • Location: Normandy, FRANCE
  • Status: offline
2009/03/25 06:04:14 (permalink)
0

SPI beginner

Hello,

I am quite new in PIC24F and also it will be my first use of SPI

I read DataSheet (PIC24FJ128GA010 since this the one i use), a generic Chapter on SPI and a generic SPI overview (.PPT on Microchip web site)
I still have questions on my specific interest (want to connect 2 PIC24FJ128GA010 via SPIs):

1)  First PIC is in MASTER mode (and not in enhanced buffer mode) , on every write to SPIxBUF i understood i have to immediately read incomming data since they a sync to clock generated by the pic in master mode .... ok but if the second PIC in Slave mode doesn't send anything what will i read ?   0 values ????

2)  Datasheets DS39699B says In Standard modes, the SPIxBUF register must not be written before SPIxTBF bit is
set.  Is this true or is this an error ? i thougt .... not to be written while it is set or before it is cleared ?

3) I thought (according to Datasheet  of PIC24FJ128GA010) that to identify if my device is concerned with Errata datasheet i had to check my version at the end of my programm memory ..... MPLAB and ICD2 show me FFFF  FFFF ? Strange isn't it or did i do something wrong ?

4) on many examples i see that TRIS registers are used for signals.  I did not notice that in case of PIC24FJ128GA010. So do i have to use TRIS or not ?

Thank'sw for help
post edited by BulleTracker - 2009/03/25 06:17:06
#1

5 Replies Related Threads

    BulleTracker
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2008/12/29 02:47:52
    • Location: Normandy, FRANCE
    • Status: offline
    RE: SPI beginner 2009/03/26 15:14:32 (permalink)
    0

    Hello,
    No body to help me so i searched on my own and here are the answers :

    >>>2)  Datasheets DS39699B says In Standard modes, the SPIxBUF register must not be written before SPIxTBF bit is
    >>>set.  Is this true or is this an error ? i thougt .... not to be written while it is set or before it is cleared ?

    According to my working code ..... write to SPI2BUF only when  SPI2STATbits.SPITBF  == 0     (DataSheet error ?!)

    >>>4) on many examples i see that TRIS registers are used for signals.  I did not notice that in case of PIC24FJ128GA010. So do i have to use >>>TRIS or not ?

    According to my working code .....   there is no need to TRIS  the bits


    Here is working code on Master side


    void SPI2Init(int Mode)
    {
        SPI2STATbits.SPIEN = 0;           // Disable SPIx module

        IFS2bits.SPI2IF = 0;                 // Clear SPIx interrupt flag   
        IEC2bits.SPI2IE = 0;                 // Enables SPIx interrupts
        IPC8bits.SPI2IP = 5;                 // SPIx interrupt priority

        SPI2STAT = 0;                        // SPIEN = 0    Disables module
                                                    // not used 1
                                                    // SPISIDL = 0  continues in Idle mode
                                                    // not used 1
                                                    // not used 1
                                                    // SPIBEC 000   in Master Mode: Nb of transfer pendinf

                                                    // SRMPT = 0    Shift register is not empty
                                                    // SPIROV = 0   No Overflow
                                                    // SRXMPT = 0   Rx Fifo not empty
        SPI2STATbits.SISEL = 2;           // SISEL = 010  Interrupt mode : receive buffer 3/4 full
                                                    // SPITBF = 0   Transmit started, SPI2TXB is empty
                                                    // SPIRBF = 0   Receive is not complete, SPI2RXB is empty

        SPI2CON1 = 0;                        // not used 3
                                                    // DISSCK = 0   Internal clock enabled
                                                    // DISSDO = 0   SDO pin controled by module
                                                    // MODE16 = 0   Communication is Byte wide
        SPI2CON1bits.SMP = 0;             // SMP = 0      in Master Mode: Input data sample at middle of data
        SPI2CON1bits.CKE = 1;              // CKE = 1      Data changes on transition from active to idle

        SPI2CON1bits.SSEN = 0;           // SSEN = 0      Slave mode : not used here
        SPI2CON1bits.CKP = 0;             // CKP = 0        idle state is low level, active state is high level                                           
        SPI2CON1bits.MSTEN = 1;         // MSTEN = 1    Master mode
        SPI2CON1bits.SPRE = 4;            // SPRE = 100   Secondary Prescale 4/1
        SPI2CON1bits.PPRE = 2;            // PPRE = 10    Primary Prescale 4/1

        SPI2CON2 = 0;                        // FRMEN = 0    Framed SPI disabled
                                                    // SPIFSD = 0   Frame sync pulse output
                                                    // SPIFPOL = 0  Frame sync pulse is active-low
                                                    // not used 5

                                                    // not used 6
                                                    // SPIFE = 0    frame sync pulse precedes first bit clock
        SPI2CON2bits.SPIBEN = 0;        // SPIBEN = 0   Enhanced buffer disabled 
                                                   
        SPI2STATbits.SPIEN = 1;          // Enables SPI2
    }
    //-----------------------------------------------------------------------
    unsigned char SPI2SendReceive(unsigned char Data)
    {
    unsigned char DataRead ;

        if (SPI2STATbits.SPITBF == 1)
            return 0xFF;
          else
            {   
               SPI2BUF = Data;
               DataRead = SPI2BUF;
               return DataRead;
            }
    }
    /************************************************************************
    * Function:        SPI2 Interrupt
    * PreCondition:   
    * Input:      
    * Output:     
    * Overview:       
    ************************************************************************/
    void __attribute__((__interrupt__, __shadow__)) _SPI2Interrupt(void)
    {
        IFS2bits.SPI2IF = 0;        
    }


    #2
    Mike017
    Super Member
    • Total Posts : 2698
    • Reward points : 0
    • Joined: 2007/08/25 08:11:31
    • Location: Lincoln, RI, USA
    • Status: offline
    RE: SPI beginner 2009/03/26 16:12:45 (permalink)
    0
    Hi,

    No body to help me so i searched on my own and here are the answers :
    It's actually better for one to do it if there is time. I think we learn more. But the forum is great if I'm really stuck.

    From the FRM
    In Standard modes, the SPIxBUF register must not be written before SPIxTBF bit is
    set. Similarly, the SPIxBUF register must not be read before the SPIxRBF bit is set.

    I think you are correct. Once there is a write, the flag is set and should not be written until it is cleared again which means the transmit is done. That being said, based on forum advice, I monitor the receive buffer to really be certain the entire transfer is complete.

    >>>4) on many examples i see that TRIS registers are used for signals.  I did not notice that in case of PIC24FJ128GA010. So do i have to use >>>TRIS or not ? According to my working code .....   there is no need to TRIS  the bits
    You are correct if your speaking of the case of using SPI. In general, one needs to use the TRIS bits for general I/O. Many peripherals control the pins but sometimes it's hard to find it written in the datasheet or FRM. In this case (SPI), it states here:

    SPIEN: SPIx Enable bit
    1 = Enables module and configures SCKx, SDOx, SDIx and SSx as serial port pins
    0 = Disables module
    If I can't find it written out like this I set the TRIS bits just to be sure.

    Good Luck,
    Mike





    post edited by Mike017 - 2009/03/26 16:14:12
    #3
    BulleTracker
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2008/12/29 02:47:52
    • Location: Normandy, FRANCE
    • Status: offline
    RE: SPI beginner 2009/03/28 14:24:38 (permalink)
    0
    Hello Mike

    Thank you for your confirmations

    My full duplex SPI connection between 2 PIC24FJ128GA010 works well now .... i had trouble in fact because my first board (with PIC24F) and my explorer16 (second PIC24F) were not on the same board .... and stupidly forgot to join ground signals. Shame on me !
    post edited by BulleTracker - 2009/03/28 15:29:37
    #4
    Mike017
    Super Member
    • Total Posts : 2698
    • Reward points : 0
    • Joined: 2007/08/25 08:11:31
    • Location: Lincoln, RI, USA
    • Status: offline
    RE: SPI beginner 2009/03/28 15:19:44 (permalink)
    0
    Hi,
     
    My full duplex SPI connection between 2 PIC24FJ128GA110 works well now

     
    Maybe you have checked these already but it's worth mentioning the errata sheets. You can find them on the same web page as the data sheet. For instance, the GA110 has a datasheet errata document as well as silicon errata document. In your case there is no mention of the issue in your post but it's worth checking. One can save a lot of head scratching by checking these.
     
    Good Luck,
    Mike
    #5
    BulleTracker
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2008/12/29 02:47:52
    • Location: Normandy, FRANCE
    • Status: offline
    RE: SPI beginner 2009/03/28 15:43:32 (permalink)
    0

    Hello,
    Sorry for the error in my previous post :: chips i am using are PIC24FJ128GA010...

    I also checked in error data sheet and i also scratched my head a long time .... before i realized i had two seperate boards .... end no common ground!
    In this case, it was kind of erratic !

    Now i am happy
    #6
    Jump to:
    © 2019 APG vNext Commercial Version 4.5