matrix.inc
changeset 34 4646abd073fb
parent 33 0d0309787be3
child 36 06e1e554acbb
equal deleted inserted replaced
33:0d0309787be3 34:4646abd073fb
     4 .set MATRIX_DDR = DDRB
     4 .set MATRIX_DDR = DDRB
     5 .set MATRIX_PORT = PORTB
     5 .set MATRIX_PORT = PORTB
     6 .set MATRIX_OE = PORTB1		; Output Enable, active low, externally pulled high
     6 .set MATRIX_OE = PORTB1		; Output Enable, active low, externally pulled high
     7 
     7 
     8 .dseg
     8 .dseg
     9 .set MATRIX_COLS = 8		; number of columns
     9 .set MATRIX_COLS = 8                        ; number of physical columns (XXX: fixed to 8)
    10 
    10 .set MATRIX_BUF_COLS = 16		            ; number of columns in frame buffer
    11 matrix_colbit:	.byte 1					; column bit
    11 
    12 matrix_rowbuf:	.byte MATRIX_COLS		; row bitmask by column
    12 matrix_colbit:	    .byte 1					; scan column bit
       
    13 matrix_colbuf:	    .byte MATRIX_BUF_COLS	; row bits by column
       
    14 matrix_colshift:    .byte 1                 ; left column offset
    13 
    15 
    14 .cseg
    16 .cseg
    15 
    17 
    16 ;; Normalize the outputs, enable the matrix, an set up buffers
    18 ;; Normalize the outputs, enable the matrix, an set up buffers
    17 Matrix_Init:
    19 Matrix_Init:
    33 
    35 
    34 	; init buffers
    36 	; init buffers
    35 		ldi			r16, 0b1
    37 		ldi			r16, 0b1
    36 		sts			matrix_colbit, r16
    38 		sts			matrix_colbit, r16
    37 
    39 
       
    40         ldi         r16, 0
       
    41         sts         matrix_colshift, r16
       
    42 
    38 		ldi			r16, 0
    43 		ldi			r16, 0
    39 		ldi			r17, MATRIX_COLS
    44 		ldi			r17, MATRIX_BUF_COLS
    40 		ldi			YL, low(matrix_rowbuf)
    45 		ldi			YL, low(matrix_colbuf)
    41 		ldi			YH, high(matrix_rowbuf)
    46 		ldi			YH, high(matrix_colbuf)
    42 
    47 
    43 m_init_mzero:
    48 m_init_mzero:
    44 		st			Y+, r16
    49 		st			Y+, r16
    45 		
    50 		
    46 		; loop until zero
    51 		; loop until zero
    55 		ret
    60 		ret
    56 
    61 
    57 ;; Scan the next column
    62 ;; Scan the next column
    58 ;;  Interrupt-driven
    63 ;;  Interrupt-driven
    59 Matrix_ScanCol:
    64 Matrix_ScanCol:
       
    65     ; Save registers
       
    66         push        r16
       
    67         push        r17
       
    68 
    60 	; Column bit
    69 	; Column bit
    61 		; load
    70 		; load
    62 		lds			r16, matrix_colbit
    71 		lds			r16, matrix_colbit
    63 	
    72 	
    64 		; start packet
    73 		; start packet
    80 		; count shifts
    89 		; count shifts
    81 		inc			r17
    90 		inc			r17
    82 		rjmp		m_sc_colidx
    91 		rjmp		m_sc_colidx
    83 
    92 
    84 m_sc_row:
    93 m_sc_row:
       
    94     ; Column shift
       
    95         ; load
       
    96         lds         r16, matrix_colshift
       
    97 
       
    98         ; add to col index
       
    99         add         r17, r16
       
   100 
    85 	; Row mask
   101 	; Row mask
    86 		; base
   102 		; base
    87 		ldi			XL, low(matrix_rowbuf)
   103 		ldi			XL, low(matrix_colbuf)
    88 		ldi			XH, high(matrix_rowbuf)
   104 		ldi			XH, high(matrix_colbuf)
    89 
   105 
    90 		; offset
   106 		; offset
    91 		ldi			r18, 0
   107 		ldi			r16, 0
    92 
   108 
    93 		add			XL, r17
   109 		add			XL, r17
    94 		adc			XH, r18
   110 		adc			XH, r16
    95 
   111 
    96 		; load
   112 		; load
    97 		ld			r16, X
   113 		ld			r16, X
    98 
   114 
    99 		; output
   115 		; output
   117 	; End of packet
   133 	; End of packet
   118 		rcall		SPI_Wait
   134 		rcall		SPI_Wait
   119         sbi         SPI_PORT, SPI_SS
   135         sbi         SPI_PORT, SPI_SS
   120 
   136 
   121 	; Done
   137 	; Done
       
   138         pop         r17
       
   139         pop         r16
       
   140 
   122 		ret
   141 		ret
   123 
   142 
   124 ;; Scan the matrix once in one go
   143 ;; Scan the matrix once in one go
   125 Matrix_ScanFull:
   144 Matrix_ScanFull:
   126 	; Row index
   145 	; Row index
   127 		ldi			ZL, low(matrix_rowbuf)
   146 		ldi			ZL, low(matrix_colbuf)
   128 		ldi			ZH, high(matrix_rowbuf)
   147 		ldi			ZH, high(matrix_colbuf)
   129 
   148 
   130 	; Column bit
   149 	; Column bit
   131 		ldi			r25, 0
   150 		ldi			r25, 0
   132 		sec								; set C
   151 		sec								; set C
   133 		
   152 		
   154 
   173 
   155 m_pulse_end:
   174 m_pulse_end:
   156 	; Done
   175 	; Done
   157 		ret
   176 		ret
   158 
   177 
       
   178 ;; Shift the matrix output one column to the right, looping around
       
   179 Matrix_ShiftRight:
       
   180     ; Decrement-loop current value
       
   181         ; current value
       
   182         lds         r16, matrix_colshift
       
   183 
       
   184         ; shift window left
       
   185         dec         r16
       
   186 
       
   187         ; test for underflow -> MSB/N set
       
   188         brpl        Matrix_ShiftSet
       
   189 
       
   190         ; reset window to right edge
       
   191         ldi         r16, MATRIX_BUF_COLS - MATRIX_COLS
       
   192 
       
   193     ;; Continue
       
   194 
       
   195 ;; Set the matrix output left shift
       
   196 ;; Input: r16
       
   197 Matrix_ShiftSet:
       
   198         ; store new value
       
   199         sts         matrix_colshift, r16
       
   200 
       
   201         ; done
       
   202         ret
       
   203