18f4550 PROBLEM IN SPI

Discussion in 'Embedded Systems and Microcontrollers' started by ayhampic, Aug 12, 2011.

  1. ayhampic

    Thread Starter New Member

    Aug 12, 2011
    19
    0
    HI,
    I have a project of two pics (18f4550 master and 18f4550 slave using spi )
    the problem is when i start the simulation in Proteus I didn't find any problem , and everything is ok , But if I try to wire the circuit I got a corrupted data received in serial comm ?

    could anyone please help me

    the master code :

    Code ( (Unknown Language)):
    1. Device 18F4550  
    2. Xtal = 48
    3. Declare Stack_Size = 10      
    4. TRISB.0 = 1
    5. TRISC.7 = 0
    6. TRISB.1 = 0  'MASTER
    7. 'TRISB.1 = 1 'SLAVE
    8. TRISA.5 = 1 'SLAVE
    9. TXSTA.5=1 ' setting Transmit Enable Bit
    10. RCSTA.7=1
    11. RCSTA.4=1
    12. Hserial_Baud 9600 ' Setting Baud rate
    13. INTCON=0
    14. INTCON2=0
    15. INTCON3=0
    16. PIE1=0
    17. PIE2=0
    18. Symbol WCOL  = SSPCON1.7 'Write Collision Detect bit
    19. Symbol SSPOV = SSPCON1.6 'Receive Overflow (SLAVE)
    20. Symbol SSPEN = SSPCON1.5 'MSSP Enable/DISABLE bit
    21. Symbol CKP   = SSPCON1.4 'SCK Release Control bit
    22. Symbol SSPM3 = SSPCON1.3 '0101:Slave ss disableed,0100:slave ss ena
    23. Symbol SSPM2 = SSPCON1.2 '0011:master clock=tmr/2
    24. Symbol SSPM1 = SSPCON1.1 '0010:master clock=fosc/64 ,0001: fosc/16
    25. Symbol SSPM0 = SSPCON1.0 '0000:master clock=fosc/4
    26.  
    27. Symbol SMP = SSPSTAT.7  ' Sample bit (INSLAVE =0 )
    28. Symbol CKE = SSPSTAT.6  ' SPI Clock Edge Select
    29. Symbol BF  = SSPSTAT.0  ' Buffer Full Status flag
    30.  
    31. Symbol GIE_GIEH      INTCON.7   'GLOBAL Interrupts enable
    32. Symbol PEIE_GIEL     INTCON.6   'SIDE DEVICES INTERRUPT ENABLE
    33. Symbol SSPIF         PIR1.3     'Parallel rad/write finish FLAG
    34. Symbol SSPIE         PIE1.3     'PARALLEL INTERRUPTS ENABLE
    35. Symbol SSPIP         IPR1.3     'PARALEL PRIORITY
    36. Symbol CS    =       PORTB.7    ' chip select pin
    37.    
    38. SSPCON1 = %00100010                    
    39.    
    40. Dim i As Byte                   ' loop index
    41. Dim Byte_Out As Byte
    42. Dim Byte_In As Byte
    43. Dim DataOut[30] As Byte
    44. Dim DataIn[30] As Byte
    45. '//////////////////////////////////////////////////////////////////
    46. DelayMS 500                     ' Wait to stabilize  
    47. SMP   = 0   ' Data input sampled at middle of data output time
    48. CKE   = 0   ' transmit of idle to active edge
    49. CKP   = 0   ' idle state is low level
    50. SSPEN = 1                        ' SSP Enable
    51. High CS                         ' unselect slave    
    52. '//////////////////////////////////////////////////////////////////    
    53. Str DataOut  = "give me the order hello",0  ' send to Slave
    54. GIE_GIEH = 1
    55. PEIE_GIEL = 1  
    56. SSPIE = 1
    57. On_Interrupt GoTo Global_SPI
    58.  
    59. i=0
    60. Dim t As Byte
    61. Main:              
    62.      HSerOut [Str DataOut,13]
    63.      For t = 0 To 3
    64.       SSPIE=0
    65.         Low CS                      ' enable Slave
    66.      
    67.         For i = 0 To 26
    68.             'GoSub SPI_Function  [DataOut[i]], DataIn[i]
    69.             SSPBUF =DataOut[i]
    70.             While SSPIF = 0 : Wend 'BF WORK
    71.             SSPIF=0
    72.             DataIn[i]= SSPBUF
    73.         Next
    74.         High CS
    75.         SSPIE=1                     ' disable Slave
    76.         HSerOut ["master",13]
    77.         For i = 0 To 20
    78.             HSerOut [DataIn[i]]
    79.         Next i
    80.         HSerOut [13]
    81.         'i=0
    82.         DelayMS 200
    83.       Next t
    84.       SSPIF=0 :i=0
    85.  
    86.    'HSerOut [Bin1 SSPIF, " ",Bin1 BF," ",Bin1 WCOL," ",Bin1 SSPOV, 13]
    87.    
    88.  
    89. GoTo Main
    90.  
    91. SPI_Function:
    92.     Pop SSPBUF                      ' pop directly to buffer
    93.     While SSPIF = 0 : Wend          ' wait for end of transaction
    94.     SSPIF = 0                       ' clear the SSP flag
    95.     Return SSPBUF                   ' push directly from buffer
    96.    
    97. Global_SPI:
    98.  
    99. Context Restore
    100. [/i][/i][/i][/i][/i]
    the slave code :
    Code ( (Unknown Language)):
    1.  
    2. Device 18F4550
    3. Xtal = 48
    4. 'SKAVE SETTING
    5. Input PORTA
    6. Input  PORTB.0      'SDI INPUT
    7. Output PORTC.7      'SDO OUTPUT
    8. Input  PORTB.1      'SCK INPUT
    9. Input PORTA.5       'SS INPUT
    10. Declare Stack_Size = 10          
    11.  
    12. TXSTA.5=1 ' setting Transmit Enable Bit
    13. RCSTA.7=1
    14. RCSTA.4=1
    15. Hserial_Baud 9600 ' Setting Baud rate
    16.  
    17. Symbol WCOL  = SSPCON1.7 'Write Collision Detect bit
    18. Symbol SSPOV = SSPCON1.6 'Receive Overflow (SLAVE)
    19. Symbol SSPEN = SSPCON1.5 'MSSP Enable/DISABLE bit
    20. Symbol CKP   = SSPCON1.4 'SCK Release Control bit
    21. Symbol SSPM3 = SSPCON1.3 '0101:Slave ss disableed,0100:slave ss ena
    22. Symbol SSPM2 = SSPCON1.2 '0011:master clock=tmr/2
    23. Symbol SSPM1 = SSPCON1.1 '0010:master clock=fosc/64 ,0001: fosc/16
    24. Symbol SSPM0 = SSPCON1.0 '0000:master clock=fosc/4
    25.  
    26. Symbol SMP = SSPSTAT.7  ' Sample bit (INSLAVE =0 )
    27. Symbol CKE = SSPSTAT.6  ' SPI Clock Edge Select
    28. Symbol BF  = SSPSTAT.0  ' Buffer Full Status flag
    29.  
    30. Symbol GIE_GIEH      INTCON.7   'GLOBAL Interrupts enable
    31. Symbol PEIE_GIEL     INTCON.6   'SIDE DEVICES INTERRUPT ENABLE
    32. Symbol SSPIF         PIR1.3     'Parallel rad/write finish FLAG
    33. Symbol SSPIE         PIE1.3     'PARALLEL INTERRUPTS ENABLE
    34. Symbol SSPIP         IPR1.3     'PARALEL PRIORITY
    35. Symbol CS    =       PORTA.5    ' chip select pin          
    36. SSPCON1= 0b00000100    
    37. Dim i As Byte                   '
    38. Dim Byte_Out As Byte
    39. Dim Byte_In As Byte
    40. Dim DataOut[30] As Byte
    41. Dim DataIn[30] As Byte
    42. '//////////////////////////////////////////////////////////////
    43. DelayMS 500                      
    44. SMP   = 0    ' Data input sampled at middle of data output time
    45. CKE   = 0    ' transmit of idle to active edge
    46. CKP   = 0    ' idle state is low level
    47. SSPEN = 1    ' SSP Enable
    48.    
    49. Str DataOut  = "AAAA BB CCC DDDD", 0 ' Slave  
    50. GIE_GIEH = 1
    51. PEIE_GIEL = 1  
    52. SSPIE = 1
    53. Dim t As Byte
    54. On_Interrupt GoTo Global_SPI
    55. Main:
    56.  
    57.  
    58.  
    59. GoTo Main
    60.  
    61.  
    62. Global_SPI:
    63.  
    64. If SSPIF = 1 And BF = 1 Then
    65.         i=i+1      
    66.         DataIn[i] = SSPBUF
    67.         SSPBUF = DataOut[i]
    68.         While SSPIF = 0 : Wend
    69.         SSPIF=0
    70.         If i = 26 Then
    71.         HSerOut ["slave",13]
    72.        
    73.         For i = 1 To 26
    74.         HSerOut [DataIn[i]]
    75.         Next i
    76.         HSerOut [13]
    77.         i=0
    78.         End If            
    79. End If
    80. Context Restore
    81. [/i][/i][/i]
    I'm using the following setting :

    SMP = 0 ' Data input sampled at middle of data output time

    CKE = 0 ' transmit of idle to active edge

    CKP = 0 ' idle state is low level

    SSPEN = 1 ' SSP Enable



    and I am receiving the following in Serial com:

    slave

    000 255 255 255 255 255 255 255 255 255

    slave

    255 000 000 000 000 000 000 000 000 000

    slave

    000 255 255 255 255 255 255 255 255 255

    slave

    255 000 000 000 000 000 000 000 000 000

    slave

    000 255 255 255 255 255 255 255 255 255

    slave



    i have tried to use Pull up resistors for the SDO, SDI WITH 10K ohm but i
    have received either 255 255 255 if i connect the resistor to the vdd
    or 000 000 000 if resistor is connected to vss .


    this is my Proteus simulation file in attachment
     
    • 11.zip
      File size:
      66.2 KB
      Views:
      24
  2. ayhampic

    Thread Starter New Member

    Aug 12, 2011
    19
    0
    has anyone tried to use SPI between two Pics ? If so have you used a pull up resistors ?
     
  3. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    I have experience with using SPI communications and the thing that caused me problems was my failure to read the receivers in all of the devices following each transmission. That failure created overrun errors on all of the receivers. You also have to do dummy transmits from the master to any slave device that has information to send the master since the master is the source of the clock for the slave.

    hgmjr
     
  4. ayhampic

    Thread Starter New Member

    Aug 12, 2011
    19
    0
    thnks, but did you use the pull up resistor ? and which pic did you work on ?
     
  5. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    I did not need to use any pull up resistors. As for the processor used, it was an ARM from ATMEL. The part number was AT91SAM7X-256.

    SPI is a standard communication protocol. As such, the actions that I described are generic to the SPI standard and will apply to any microcontroller implementation of SPI.

    hgmjr
     
  6. ayhampic

    Thread Starter New Member

    Aug 12, 2011
    19
    0
    weird ?? please how could you explain my getting of 255 or 000 all the time ?
     
  7. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    At issue is the language you are using. It looks like PASCAL. I have not programmed with PASCAL in a long time.

    hgmjr
     
  8. ayhampic

    Thread Starter New Member

    Aug 12, 2011
    19
    0
    Its basic , and i have include the hex file in the attachments
     
  9. stahta01

    Member

    Jun 9, 2011
    133
    21
    Based on the data, I would guess your receive clock rate is 64 times the transmit code rate.

    Based on the value of SSPCON1 and the info in comments; it is only 16 times times the rate.

    This assumes the FOSC is the same in both.

    Tim S.
     
  10. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    The listing in your first post appears to be incomplete. I don't see the source code for hserout for example.

    hgmjr
     
  11. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Do you have a circuit diagram that you can scan and post here? I would be interested in seeing how you have the two PICs connected.

    hgmjr
     
  12. ayhampic

    Thread Starter New Member

    Aug 12, 2011
    19
    0
    HI,

    Actually in protues my circuit works well, and here is some pictures from sim :

    i have a doubt if I receive 255 = FF = 11111111 OR 0 ALL THE TIME maybe that means when the clock changes 01010101 the SDA is either 0 or 1 during the whole clock time ??
     
    • spi.zip
      File size:
      41.9 KB
      Views:
      18
  13. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    I suggest you take a careful look at your pinouts. What device package is your breadboarded device? It is possible that you are wiring the actual package (say the 40-pin DIP) while refering to the 44-pin PLCC pinout or visa-versa?

    hgmjr
     
  14. ayhampic

    Thread Starter New Member

    Aug 12, 2011
    19
    0
    HI,
    eventually , i have solved my problem , the issue was in the usart module which shouldn't be enabled with the spi . I have disabled the USART and wired a LCD , and the results were great .

    BUT THERE IS A NEW PROBLEM, always the first byte of transaction is zero an the rest of bytes are correct . I looked on the net its a common problem but it is solvable but i don't know how ?

    If we have experienced the spi before between two microcontroller have you faced this problem ? if so please how did you solve it ?

    regards and thanks a lot for your help >
     
  15. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    I think what you are seeing is the fact that the SPI communication link between two devices is really just daisy chaining of two shift registers. The first 8 data bits clocked from the master device to the slave device ends up clocking basically an undefined byte that was in the slave devices shift register.

    hgmjr
     
  16. ayhampic

    Thread Starter New Member

    Aug 12, 2011
    19
    0
    So receiving a first byte as 0 is normal ????
     
  17. ayhampic

    Thread Starter New Member

    Aug 12, 2011
    19
    0
    Like this :

    0aaaaabbbbbccccc then it disappears
    aaaaaabbbbbccccc
     
  18. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    It may be all zeroes or it could be all ones. It depends on what the slave device's transmit register's default state is when it is empty.

    hgmjr
     
  19. ayhampic

    Thread Starter New Member

    Aug 12, 2011
    19
    0
    NO ACTUALLY IT IS 0x0H OR 0xFFH AND THIS IS A PROBLEM AND I HAVE SEEN IT IN PROTEUS TOO AND IN REAL . I 'm sure that there is a way to solve it but I don't know how ? by the way do you know any good communication protocol between pics ? would USART BE GOOD FOR 1 MASTER AND MORE THAN 2 PICS ?
    REGARDS
     
  20. ayhampic

    Thread Starter New Member

    Aug 12, 2011
    19
    0
    slave load the buffer before sending it
     
Loading...