[PIC] SPI assembly code 18f4550

Status
Not open for further replies.

asp87

Junior Member level 1
Joined
Feb 16, 2008
Messages
19
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,427
Hi,
I'm trying to interface 18f4550 with 25LC160A (serial ROM) using the PIC's hardware SPI module by polling method. It doesn't work. But it works when I implement SPI in software.

So here I am, trying to figure out how this thing works. Hence wrote codes for 18f4550 master---SPI--- 18f4550 slave communication. That doesn't work either. Below are the codes. Please point out the errors.

The Master will send 0x06 (b'0000 0110') to the slave forever.
The slave will check SSPSTAT, BF bit for '1'. If true, tests SSPBUF against the expected value 0x06. If true, turns ON an LED. Loops forever.

Master
Code:
[SIZE=1] LIST    P=18F4550 
     include    "p18f4550.inc"    
     errorlevel    -302,    -203,    -205,    -206    
     
     CONFIG    FOSC=    HS 
 cblock 0x000 
 
 d1 
 d2 
 d3 
 endc 
 
 select_ROM    macro 
     bcf PORTD,2 
 endm 
     
 deselect_ROM    macro 
     bsf PORTD, 2 
 endm 
 
     org    0x000   
     call    DelayHalfsec 
     call    DelayHalfsec 
     call    DelayHalfsec 
   
     clrf    LATA 
     clrf    LATB 
     clrf    LATC 
     clrf    LATD 
     clrf    LATE 
     movlw    0xFF 
     movwf    ADCON1 
     movlw    0x00 
     movwf    TRISA 
     movwf    TRISB 
     movwf    TRISC   ; RC7 is SDO
     movwf    TRISE 
     movlw    b'00000000' ;left pic master, RD2 chip select 
     movwf    TRISD 
     movlw    b'00000001'   ; RB0 is SDI, RB1 is SCK
     movwf    TRISB 
     movlw    b'10000000';cke bit 6 
     movwf    SSPSTAT 
     movlw    b'00100010';ckp bit 4 
     movwf    SSPCON1 
     
 Send_DT 
     movlw    b'00000110' ;data from Master to Slave 
     call    SPI_Write 
     call    DelayHalfsec 
     bra        Send_DT        ; keep sending 0x06 to slave 
 
 ;-------------------------- 
 SPI_Write 
     select_ROM    ; chip select is not used in this application 
     movwf SSPBUF ; 
 Char1 btfss SSPSTAT,BF 
     bra Char1 
     movf SSPBUF,W 
     deselect_ROM 
     return 
 
 ;------------------------------------ 
 DelayHalfsec ; 20MHz 
     movlw    0x15 
     movwf    d1 
     movlw    0x74 
     movwf    d2 
     movlw    0x06 
     movwf    d3 
 DelayHalfsec_0 
     decfsz    d1,    f 
     bra        $+4 
     decfsz    d2,    f 
     bra        $+4 
     decfsz    d3,    f 
     bra    DelayHalfsec_0 
     bra        $+2 
     bra        $+2 
     return 
 ;---------------- 
    END[/SIZE]

Slave:
Code:
  LIST    P=18F4550 
     include    "p18f4550.inc"    
     errorlevel    -302,    -203,    -205,    -206    
     
     CONFIG    FOSC=    HS 
 cblock 0x000 
 
 temp1 
 d1 
 d2 
 d3 
 endc 
 
     org    0x000     
     bra slave     
 
 slave 
     call    DelayHalfsec 
     clrf    LATA 
     clrf    LATB 
     clrf    LATC 
     clrf    LATD 
     clrf    LATE 
     movlw    0xFF 
     movwf    ADCON1 
     movlw    0x00 
     movwf    TRISA 
     movwf    TRISB  
     movwf    TRISC ; RC7 is SDO
     movwf    TRISE 
     movlw    b'00000100' ;right pic slave, RD2 chip select 
     movwf    TRISD 
     movlw    b'00000001' ; RB0 is SDI, RB1 is SCK
     movwf    TRISB 
     movlw    b'00000000';cke bit 6 
     movwf    SSPSTAT 
     movlw    b'00100010';ckp bit 4 
     movwf    SSPCON1 
 
 Receive_DT  
     btfss SSPSTAT, BF ; test BUFFER FULL bit 
     bra Receive_DT   ; if not full, wait 
     movff SSPBUF, temp1 
     movlw b'00000110' ; this is the value expected from master 
     xorwf temp1, w 
     btfsc STATUS, Z 
     bsf PORTD, 1  ; if expected data matches with actual data, turn ON LED 
     bra Receive_DT  ; else, loop forever 
 ;--------------------------- 
 DelayHalfsec ; 20MHz 
     movlw    0x15 
     movwf    d1 
     movlw    0x74 
     movwf    d2 
     movlw    0x06 
     movwf    d3 
 DelayHalfsec_0 
     decfsz    d1,    f 
     bra        $+4 
     decfsz    d2,    f 
     bra        $+4 
     decfsz    d3,    f 
     bra    DelayHalfsec_0 
     bra        $+2 
     bra        $+2 
     return 
 
 ;---- 
 
     END
 

Hi,

Its always good to specify the important Config parameters to ensure they are always correct.

Even with SPI the chips internal oscillator is more than good enough.


These tutorials/ notes might help you solve your problem.

https://ww1.microchip.com/downloads/en/devicedoc/spi.pdf

**broken link removed**

Code:
;******************************************************************************
;
;	Processor Type
;	==============

	LIST P=18F4550,r=hex,n=80,x=off,st=off
	errorlevel -302		; no bank warnings

	#include <P18F4550.INC>	
	

;******************************************************************************
;
;	Configuration bits 
;	==================
  
; specified here are changes to the .inc file defaults

 CONFIG FOSC=INTOSCIO_EC,  WDT=OFF, PWRT=ON, BOR=OFF, MCLRE=OFF, LPT1OSC=OFF, XINST=OFF
 CONFIG LVP=OFF, VREGEN=OFF, PBADEN=OFF, DEBUG=OFF

;******************************************************************************



Main		movlw	b'01000010'			; set internal osc to 4 mhz
			movwf	OSCCON
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…