Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MPASM 3.48 KB | None | 0 0
  1. ;;; RB7:0 LEDs for Diagnostics.
  2.  
  3. ;;; RC2 - Latch Pin
  4. ;;; RC1 - Clock
  5. ;;; RC0 - Data
  6.  
  7. #include <p16F883.inc>
  8.     __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
  9.     __CONFIG _CONFIG2, _WRT_OFF & _BOR21V
  10.  
  11.     org 0
  12.  
  13.     cblock 0x70
  14.     nes_byte : 1
  15.     nes_bits_to_read : 1
  16.     delay_loop_inner_ctr : 1
  17.     delay_loop_outer_ctr : 1
  18.     temp : 1
  19.     endc
  20.  
  21.     clrf delay_loop_inner_ctr
  22.     clrf delay_loop_outer_ctr
  23.     clrf nes_byte
  24.  
  25.     banksel PORTB
  26.     clrf PORTB
  27.  
  28.     banksel TRISB
  29.     clrf PORTB ; Set PORTB to Output
  30.  
  31.     banksel PORTC
  32.     clrf PORTC
  33.  
  34.     banksel TRISC
  35.     movlw b'00000001' ;; RC7:1 Output RC0 Intput
  36.     movwf PORTC
  37.  
  38.     banksel ANSEL
  39.     clrf ANSEL
  40.     clrf ANSELH ; Disable analog buffers
  41.  
  42. cycle:
  43.     banksel PORTC
  44.     bsf PORTC, 2 ; 1 us
  45.     nop          ; 2
  46.     nop          ; 3
  47.     nop          ; 4
  48.     nop          ; 5
  49.     nop          ; 6
  50.     nop          ; 7
  51.     nop          ; 8
  52.     nop          ; 9
  53.     nop          ; 10
  54.     nop          ; 11
  55.     nop          ; 12
  56.     bcf PORTC, 2
  57.     movlw d'8'
  58.     movwf nes_bits_to_read
  59.  
  60. read_pin:
  61.     rrf PORTC, W                ; 3
  62.     rlf nes_byte, F             ; 4
  63.     nop                         ; 5
  64.     nop                         ; 6
  65.     bsf PORTC, 1                ; 7  ;; Set the clock pin high
  66.     decfsz nes_bits_to_read, F  ; 8
  67.     goto $+2                    ; 9
  68.     goto done_reading           ; 10
  69.     nop                         ; 11
  70.     bcf PORTC, 1                ; 12  ;; Set the clock pin low
  71.     goto read_pin               ; 1
  72.     nop                         ; 2  
  73.  
  74. done_reading:
  75.     ;;; The data we read is in the following form:
  76.     ;;;
  77.     ;;; +---+---+--------+-------+----+------+------+-------+
  78.     ;;; | A | B | Select | Start | Up | Down | Left | Right |
  79.     ;;; +---+---+--------+-------+----+------+------+-------+
  80.     ;;; MSB<--------------------------------------------->LSB
  81.  
  82.  
  83.     bcf PORTC, 1             ;; read_pin jumped here while it was done reading
  84.                              ;; but it did not set the clock low.
  85.  
  86.     ;;; We need to negate the value of the read data because
  87.     ;;; the NES treats 0 as pressed and 1 as unpressed.
  88.     comf nes_byte, F
  89.  
  90.     call reorder;
  91.     ;;; The data is now in the following form:
  92.     ;;;
  93.     ;;; +-------+--------+---+---+-------+------+------+----+
  94.     ;;; | Start | Select | B | A | Right | Left | Down | Up |
  95.     ;;; +-------+--------+---+---+-------+------+------+----+
  96.     ;;; MSB<--------------------------------------------->LSB
  97.  
  98.  
  99.     ;;; Dump the data we read to PORTB for diagnostics with the LEDS
  100.     banksel PORTB
  101.     movf nes_byte, W
  102.     movwf PORTB
  103.    
  104.     ;;; Sleep for a while.  We've used ~110 cyles to get to here.
  105.     ;;; If we sleep for another 16050 cycles that will give us around a 60Hz
  106.     ;;; effective read rate from the controller.
  107.     movlw d'20'
  108.     call delay_loop
  109.     goto cycle
  110.    
  111. ;;; This loop delays (W + 1)*(771) + 3 cycles.
  112. delay_loop:
  113.     movwf delay_loop_outer_ctr
  114.     decfsz delay_loop_inner_ctr, F
  115.     goto $-1
  116.     decfsz delay_loop_outer_ctr, F
  117.     goto $-3
  118.     return
  119.  
  120. reorder:
  121.     clrw
  122.     btfsc nes_byte, 3
  123.     iorlw b'00000001'
  124.     btfsc nes_byte, 2
  125.     iorlw b'00000010'
  126.     btfsc nes_byte, 1
  127.     iorlw b'00000100'
  128.     btfsc nes_byte, 0
  129.     iorlw b'00001000'
  130.     btfsc nes_byte, 6
  131.     iorlw b'00010000'
  132.     btfsc nes_byte, 7
  133.     iorlw b'00100000'
  134.     btfsc nes_byte, 5
  135.     iorlw b'01000000'
  136.     btfsc nes_byte, 4
  137.     iorlw b'10000000'
  138.     movwf nes_byte
  139.     return
  140.  
  141.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement