Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;; RB7:0 LEDs for Diagnostics.
- ;;; RC2 - Latch Pin
- ;;; RC1 - Clock
- ;;; RC0 - Data
- #include <p16F883.inc>
- __CONFIG _CONFIG1, _DEBUG_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _LVP_OFF
- __CONFIG _CONFIG2, _WRT_OFF & _BOR21V
- org 0
- cblock 0x70
- nes_byte : 1
- nes_bits_to_read : 1
- delay_loop_inner_ctr : 1
- delay_loop_outer_ctr : 1
- temp : 1
- endc
- clrf delay_loop_inner_ctr
- clrf delay_loop_outer_ctr
- clrf nes_byte
- banksel PORTB
- clrf PORTB
- banksel TRISB
- clrf PORTB ; Set PORTB to Output
- banksel PORTC
- clrf PORTC
- banksel TRISC
- movlw b'00000001' ;; RC7:1 Output RC0 Intput
- movwf PORTC
- banksel ANSEL
- clrf ANSEL
- clrf ANSELH ; Disable analog buffers
- cycle:
- banksel PORTC
- bsf PORTC, 2 ; 1 us
- nop ; 2
- nop ; 3
- nop ; 4
- nop ; 5
- nop ; 6
- nop ; 7
- nop ; 8
- nop ; 9
- nop ; 10
- nop ; 11
- nop ; 12
- bcf PORTC, 2
- movlw d'8'
- movwf nes_bits_to_read
- read_pin:
- rrf PORTC, W ; 3
- rlf nes_byte, F ; 4
- nop ; 5
- nop ; 6
- bsf PORTC, 1 ; 7 ;; Set the clock pin high
- decfsz nes_bits_to_read, F ; 8
- goto $+2 ; 9
- goto done_reading ; 10
- nop ; 11
- bcf PORTC, 1 ; 12 ;; Set the clock pin low
- goto read_pin ; 1
- nop ; 2
- done_reading:
- ;;; The data we read is in the following form:
- ;;;
- ;;; +---+---+--------+-------+----+------+------+-------+
- ;;; | A | B | Select | Start | Up | Down | Left | Right |
- ;;; +---+---+--------+-------+----+------+------+-------+
- ;;; MSB<--------------------------------------------->LSB
- bcf PORTC, 1 ;; read_pin jumped here while it was done reading
- ;; but it did not set the clock low.
- ;;; We need to negate the value of the read data because
- ;;; the NES treats 0 as pressed and 1 as unpressed.
- comf nes_byte, F
- call reorder;
- ;;; The data is now in the following form:
- ;;;
- ;;; +-------+--------+---+---+-------+------+------+----+
- ;;; | Start | Select | B | A | Right | Left | Down | Up |
- ;;; +-------+--------+---+---+-------+------+------+----+
- ;;; MSB<--------------------------------------------->LSB
- ;;; Dump the data we read to PORTB for diagnostics with the LEDS
- banksel PORTB
- movf nes_byte, W
- movwf PORTB
- ;;; Sleep for a while. We've used ~110 cyles to get to here.
- ;;; If we sleep for another 16050 cycles that will give us around a 60Hz
- ;;; effective read rate from the controller.
- movlw d'20'
- call delay_loop
- goto cycle
- ;;; This loop delays (W + 1)*(771) + 3 cycles.
- delay_loop:
- movwf delay_loop_outer_ctr
- decfsz delay_loop_inner_ctr, F
- goto $-1
- decfsz delay_loop_outer_ctr, F
- goto $-3
- return
- reorder:
- clrw
- btfsc nes_byte, 3
- iorlw b'00000001'
- btfsc nes_byte, 2
- iorlw b'00000010'
- btfsc nes_byte, 1
- iorlw b'00000100'
- btfsc nes_byte, 0
- iorlw b'00001000'
- btfsc nes_byte, 6
- iorlw b'00010000'
- btfsc nes_byte, 7
- iorlw b'00100000'
- btfsc nes_byte, 5
- iorlw b'01000000'
- btfsc nes_byte, 4
- iorlw b'10000000'
- movwf nes_byte
- return
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement