matrix.s
changeset 31 dfb246ecaf23
child 33 0d0309787be3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/matrix.s	Fri May 14 18:19:22 2010 +0300
@@ -0,0 +1,96 @@
+.nolist
+.include "m168def.inc"      ; Same family as 328P
+.list
+
+
+;; Interrupt Vector
+.org 0x00
+        rjmp        init
+
+;; Syntax
+.include "macros.inc"
+
+;; SPI
+.include "spi.inc"
+
+;; Utils
+.include "delay.inc"
+
+;; Initialize output buffers
+Main_Init:
+    ; Setup ENable port
+        sbi         PORTB, PORTB1 	    	; high -> disabled
+        sbi         DDRB, PORTB1    		; out
+ 
+	; all low
+		ldi			r16, 0
+
+		sts			spi_outbuf + 0, r16
+		sts			spi_outbuf + 1, r16
+
+	; write out
+		rcall		SPI_SendRecv
+
+	; enable
+		cbi			PORTB, PORTB1			; low -> enabled
+
+	; done
+		ret
+
+;; Scan through each pixel
+Main_Scan:
+	; init
+		ldi			r18, 1
+		ldi			r19, 1
+
+scan_loop:
+	; write out to SPI
+		sts			spi_outbuf + 0, r18		; rows
+		sts			spi_outbuf + 1, r19		; columns
+
+		rcall		SPI_SendRecv
+
+	; delay
+		ldi			r20, 5					; ~4M cycles
+		rcall		VarDelay
+
+	; rows
+		rol			r18						; next row
+		brcc		scan_loop				; refresh if we didn't overflow
+		
+		rol			r18						; shift back from C into r20.0
+
+	; cols
+		rol			r19						; next col
+		brcc		scan_loop				; refresh if we didn't overflow
+		
+		rol			r19						; shift back from C into r21.0
+
+	; one scan completed
+		ret
+
+Main:
+init:
+    ; Stack
+        ldi         r16, high(RAMEND)
+        ldi         r17, low(RAMEND)
+        out         SPH, r16
+        out         SPL, r17
+
+    ; Enable interrupts
+        sei
+
+    ; SPI
+        rcall       SPI_Init
+	
+
+    ; Run
+		rcall		Main_Init
+
+loop:
+		rcall		Main_Scan
+		rjmp		loop
+
+end:
+        rjmp        end
+