Jiadong Yao
Member level 1
Hello,everyone.
i am doing the lab. but i have some problems now.
The requirement is
1. make a counter
2.Configure your PIC as a Master SPI.
3. Transmit the counter values via the SPI Master. Connect SDO directly to SDI of the same board. Check what happens.
4. Display the received 8-bit result on 8 LED’s. Contrary to what was done in the previous exercise, the LED’s must be driven by results received via SPI, not via direction connection to PORTB. Display the SPI data line on an oscilloscope
here is my code
Because RB0 is used as SDI, and RB1 is used as SCK, only RB2 to RB7 will be used to display the LEDs
This lab is quite strange. We use one pic as a master, but it connects itself.
Does that mean it is both master and slave ? then how does the CLK to be connected? (I just connect it to the ground, indeed, it will blink very fast. Fosc=125KHz)
But there is no LED on from RB2 to RB7. I think it means that the interrupt does not occur. How to solve the problem?
Help!
i am doing the lab. but i have some problems now.
The requirement is
1. make a counter
2.Configure your PIC as a Master SPI.
3. Transmit the counter values via the SPI Master. Connect SDO directly to SDI of the same board. Check what happens.
4. Display the received 8-bit result on 8 LED’s. Contrary to what was done in the previous exercise, the LED’s must be driven by results received via SPI, not via direction connection to PORTB. Display the SPI data line on an oscilloscope
here is my code
Code:
#include <p18F2550.inc>
counter equ 0x21
delay equ 0x22
delay1 equ 0x23
delay2 equ 0x24
delay3 equ 0x25
;***********************************************************
; Reset Vector
;***********************************************************
ORG 0x800 ; Reset Vector
; When debugging:0x000; when loading: 0x800
GOTO START
;***********************************************************
; Interrupt Vector
;***********************************************************
ORG 0x808 ; Interrupt Vector HIGH priority
GOTO inter ; When debugging:0x008; when loading: 0x808
; 808high 818low
;***********************************************************
; Program Code Starts Here
;***********************************************************
ORG 0x820 ;When debugging:0x020; when loading: 0x820
START
clrf PORTA ; Initialize PORTA by clearing output data latches
movlw 0x39 ; Value used to initialize data direction
movwf TRISA ; Set RA0,3,4 as inputs 0001 1001
movlw 0x0F ; Configure A/D for digital inputs 0000 1111
movwf ADCON1 ;
movlw 0x07 ; Configure comparators for digital input
movwf CMCON
clrf PORTB ; Initialize PORTB by clearing output data latches
movlw 0x01 ; Value used to initialize data direction
movwf TRISB ; Set PORTB as output
clrf PORTC ; Initialize PORTC by clearing output data latches
movlw 0x00 ; Value used to initialize data direction
movwf TRISC
bcf UCON,3 ; to be sure to disable USB module p.166 why disable USB module??
bsf UCFG,3 ; disable internal USB transceiver p.168
movlw 0x00
movwf SSPSTAT ;sampled at middle
;transition from Idle to active
;Idle state for clock is a low level
movlw 0x22 ;SPI Master mode, clock = FOSC/64
movwf SSPCON1
clrf counter ; clear registers
movlw 0xff
movwf delay
movwf delay1
movwf delay2
movwf delay3
movlw 0x12 ;125KHz
movwf OSCCON
main
btfsc PORTA,3 ;if RA3=0, skip, go to main
goto select ;if RA3=1, go to select
goto main
select
btfsc PORTA,4 ;check RA4, if it is equal 1, go to up
goto up ; else go to down
goto down
up ; up counter
movlw 0xff ;move the 255 to W
cpfslt counter ; compare counter with W, skip if less than 255
goto up1
goto up2
up1
clrf counter ; clear, counter = 0
movf counter,0 ;show on PORTB
btfsc PORTA,0 ;test RA0
goto main
goto SPIsend
up2
incf counter,1 ;increment
movf counter,0 ;show on PORTB
btfsc PORTA,0 ;test RA0
goto main
goto SPIsend
down ;down counter
movlw 0x00 ; move 0 to the W
cpfsgt counter ;compare counter with W, skip if great than 0
goto down1 ;
goto down2
down1
movlw 0xff ; set counter = 255
movwf counter
movf counter,0 ;show on PORTB
btfsc PORTA,0 ;test RA0
goto main
goto SPIsend
down2
decf counter,1
movf counter,0 ;show on PORTB
btfsc PORTA,0 ;test RA0
goto main
goto SPIsend
delay_loop
Decfsz delay,1
goto $+2
decfsz delay1,1
goto $+2
decfsz delay2,1
goto $+2
decfsz delay3,1
goto delay_loop
return
SPIsend
movwf SSPBUF
goto main
SPIread
bcf PIR1,SSPIF
MOVFF SSPBUF,PORTB
call delay_loop
RETURN
inter ;interrupt doesn't occur
BTFSC PIR1,SSPIF
call SPIread
RETFIE
END
Because RB0 is used as SDI, and RB1 is used as SCK, only RB2 to RB7 will be used to display the LEDs
This lab is quite strange. We use one pic as a master, but it connects itself.
Does that mean it is both master and slave ? then how does the CLK to be connected? (I just connect it to the ground, indeed, it will blink very fast. Fosc=125KHz)
But there is no LED on from RB2 to RB7. I think it means that the interrupt does not occur. How to solve the problem?
Help!