18f4550 PROBLEM IN SPI

Thread Starter

ayhampic

Joined Aug 12, 2011
19
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 :

Rich (BB code):
Device 18F4550  
Xtal = 48
Declare Stack_Size = 10      
TRISB.0 = 1
TRISC.7 = 0
TRISB.1 = 0  'MASTER
'TRISB.1 = 1 'SLAVE
TRISA.5 = 1 'SLAVE
TXSTA.5=1 ' setting Transmit Enable Bit
RCSTA.7=1
RCSTA.4=1
Hserial_Baud 9600 ' Setting Baud rate
INTCON=0
INTCON2=0
INTCON3=0
PIE1=0
PIE2=0
Symbol WCOL  = SSPCON1.7 'Write Collision Detect bit
Symbol SSPOV = SSPCON1.6 'Receive Overflow (SLAVE)
Symbol SSPEN = SSPCON1.5 'MSSP Enable/DISABLE bit
Symbol CKP   = SSPCON1.4 'SCK Release Control bit
Symbol SSPM3 = SSPCON1.3 '0101:Slave ss disableed,0100:slave ss ena
Symbol SSPM2 = SSPCON1.2 '0011:master clock=tmr/2
Symbol SSPM1 = SSPCON1.1 '0010:master clock=fosc/64 ,0001: fosc/16
Symbol SSPM0 = SSPCON1.0 '0000:master clock=fosc/4

Symbol SMP = SSPSTAT.7  ' Sample bit (INSLAVE =0 )
Symbol CKE = SSPSTAT.6  ' SPI Clock Edge Select
Symbol BF  = SSPSTAT.0  ' Buffer Full Status flag

Symbol GIE_GIEH      INTCON.7   'GLOBAL Interrupts enable
Symbol PEIE_GIEL     INTCON.6   'SIDE DEVICES INTERRUPT ENABLE
Symbol SSPIF         PIR1.3     'Parallel rad/write finish FLAG
Symbol SSPIE         PIE1.3     'PARALLEL INTERRUPTS ENABLE
Symbol SSPIP         IPR1.3     'PARALEL PRIORITY
Symbol CS    =       PORTB.7    ' chip select pin
    
SSPCON1 = %00100010                    
    
Dim i As Byte                   ' loop index
Dim Byte_Out As Byte
Dim Byte_In As Byte
Dim DataOut[30] As Byte
Dim DataIn[30] As Byte
'//////////////////////////////////////////////////////////////////
DelayMS 500                     ' Wait to stabilize  
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
High CS                         ' unselect slave    
'//////////////////////////////////////////////////////////////////    
Str DataOut  = "give me the order hello",0  ' send to Slave
GIE_GIEH = 1
PEIE_GIEL = 1  
SSPIE = 1
On_Interrupt GoTo Global_SPI

i=0
Dim t As Byte
Main:              
     HSerOut [Str DataOut,13]
     For t = 0 To 3
      SSPIE=0
        Low CS                      ' enable Slave
      
        For i = 0 To 26
            'GoSub SPI_Function  [DataOut], DataIn
            SSPBUF =DataOut
            While SSPIF = 0 : Wend 'BF WORK
            SSPIF=0
            DataIn= SSPBUF
        Next
        High CS
        SSPIE=1                     ' disable Slave
        HSerOut ["master",13]
        For i = 0 To 20
            HSerOut [DataIn]
        Next i
        HSerOut [13]
        'i=0
        DelayMS 200
      Next t
      SSPIF=0 :i=0

   'HSerOut [Bin1 SSPIF, " ",Bin1 BF," ",Bin1 WCOL," ",Bin1 SSPOV, 13]
    

GoTo Main

SPI_Function:
    Pop SSPBUF                      ' pop directly to buffer
    While SSPIF = 0 : Wend          ' wait for end of transaction
    SSPIF = 0                       ' clear the SSP flag
    Return SSPBUF                   ' push directly from buffer
    
Global_SPI:
 
Context Restore
the slave code :
Rich (BB code):
Device 18F4550
Xtal = 48
'SKAVE SETTING
Input PORTA
Input  PORTB.0      'SDI INPUT
Output PORTC.7      'SDO OUTPUT
Input  PORTB.1      'SCK INPUT
Input PORTA.5       'SS INPUT
Declare Stack_Size = 10          

TXSTA.5=1 ' setting Transmit Enable Bit
RCSTA.7=1
RCSTA.4=1
Hserial_Baud 9600 ' Setting Baud rate

Symbol WCOL  = SSPCON1.7 'Write Collision Detect bit
Symbol SSPOV = SSPCON1.6 'Receive Overflow (SLAVE)
Symbol SSPEN = SSPCON1.5 'MSSP Enable/DISABLE bit
Symbol CKP   = SSPCON1.4 'SCK Release Control bit
Symbol SSPM3 = SSPCON1.3 '0101:Slave ss disableed,0100:slave ss ena
Symbol SSPM2 = SSPCON1.2 '0011:master clock=tmr/2
Symbol SSPM1 = SSPCON1.1 '0010:master clock=fosc/64 ,0001: fosc/16
Symbol SSPM0 = SSPCON1.0 '0000:master clock=fosc/4

Symbol SMP = SSPSTAT.7  ' Sample bit (INSLAVE =0 )
Symbol CKE = SSPSTAT.6  ' SPI Clock Edge Select
Symbol BF  = SSPSTAT.0  ' Buffer Full Status flag

Symbol GIE_GIEH      INTCON.7   'GLOBAL Interrupts enable
Symbol PEIE_GIEL     INTCON.6   'SIDE DEVICES INTERRUPT ENABLE
Symbol SSPIF         PIR1.3     'Parallel rad/write finish FLAG
Symbol SSPIE         PIE1.3     'PARALLEL INTERRUPTS ENABLE
Symbol SSPIP         IPR1.3     'PARALEL PRIORITY
Symbol CS    =       PORTA.5    ' chip select pin          
SSPCON1= 0b00000100    
Dim i As Byte                   '
Dim Byte_Out As Byte
Dim Byte_In As Byte
Dim DataOut[30] As Byte
Dim DataIn[30] As Byte
'//////////////////////////////////////////////////////////////
DelayMS 500                      
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
    
Str DataOut  = "AAAA BB CCC DDDD", 0 ' Slave  
GIE_GIEH = 1
PEIE_GIEL = 1  
SSPIE = 1
Dim t As Byte
On_Interrupt GoTo Global_SPI
Main:



GoTo Main
  

Global_SPI:

If SSPIF = 1 And BF = 1 Then
        i=i+1      
        DataIn = SSPBUF
        SSPBUF = DataOut
        While SSPIF = 0 : Wend
        SSPIF=0
        If i = 26 Then
        HSerOut ["slave",13]
        
        For i = 1 To 26
        HSerOut [DataIn]
        Next i
        HSerOut [13]
        i=0
        End If            
End If
Context Restore
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
 

Attachments

hgmjr

Joined Jan 28, 2005
9,027
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
 

Thread Starter

ayhampic

Joined Aug 12, 2011
19
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
thnks, but did you use the pull up resistor ? and which pic did you work on ?
 

hgmjr

Joined Jan 28, 2005
9,027
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
 

Thread Starter

ayhampic

Joined Aug 12, 2011
19
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
weird ?? please how could you explain my getting of 255 or 000 all the time ?
 

hgmjr

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

hgmjr
 

stahta01

Joined Jun 9, 2011
133
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.
 

hgmjr

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

hgmjr
 

hgmjr

Joined Jan 28, 2005
9,027
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
 

Thread Starter

ayhampic

Joined Aug 12, 2011
19
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 ??
 

Attachments

hgmjr

Joined Jan 28, 2005
9,027
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
 

Thread Starter

ayhampic

Joined Aug 12, 2011
19
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 >
 

hgmjr

Joined Jan 28, 2005
9,027
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
 

hgmjr

Joined Jan 28, 2005
9,027
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
 

Thread Starter

ayhampic

Joined Aug 12, 2011
19
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
 
Top