console.s
changeset 13 a18180e37d57
parent 11 49c5eeee4cb7
child 14 1ebd5fc73d86
equal deleted inserted replaced
12:af46bd59b2b8 13:a18180e37d57
    29 		ldi			@0, high(@2)
    29 		ldi			@0, high(@2)
    30 		ldi			@1, low(@2)
    30 		ldi			@1, low(@2)
    31 .endm
    31 .endm
    32 
    32 
    33 ;; Interrupt Vector
    33 ;; Interrupt Vector
    34 .org 0x00
    34 .org 0x0000
    35         rjmp        Main
    35         rjmp        Main
       
    36 
       
    37 ;; Libraries
       
    38 .include "div.inc"			; Division routines
    36 
    39 
    37 ;; Serial
    40 ;; Serial
    38 .set SERIAL_BAUD = 103		; 9.6k @ 16Mhz
    41 .set SERIAL_BAUD = 103		; 9.6k @ 16Mhz
    39 
    42 
    40 ;; Initialize the UART for 
    43 ;; Initialize the UART for 
    88 	; Done
    91 	; Done
    89 		ret
    92 		ret
    90 
    93 
    91 ;; Write nul-terminated string to serial port
    94 ;; Write nul-terminated string to serial port
    92 ; Input string in Z
    95 ; Input string in Z
    93 Serial_Write:
    96 Serial_Write_s:
    94 	; Load byte to r16
    97 	; Load byte to r16
    95 		lpm			r16, Z+
    98 		lpm			r16, Z+
    96 
    99 
    97 	; Quit if nul
   100 	; Quit if nul
    98 		tst			r16
   101 		tst			r16
    99 		breq		_serial_write_end
   102 		breq		_serial_writes_end
   100 
   103 
   101 	; Write it
   104 	; Write it
   102 		rcall		Serial_Send
   105 		rcall		Serial_Send
   103 
   106 
   104 	; Continue
   107 	; Continue
   105 		rjmp		Serial_Write
   108 		rjmp		Serial_Write_s
   106 
   109 
   107 _serial_write_end:
   110 _serial_writes_end:
   108 		ret
   111 		ret
       
   112 
       
   113 ;; Write char to serial port
       
   114 ; Input byte in r16
       
   115 Serial_Write_c:
       
   116 		; ASCII offset for '0'
       
   117 		ldi			r17, 48
       
   118 		mov			r4, r17	
       
   119 	
       
   120 	; Convert
       
   121 		ldi			r17, 100
       
   122 		call		div8u
       
   123 		add			r16, r4
       
   124 		rcall		Serial_Send
       
   125 		mov			r16, r15
       
   126 
       
   127 		ldi			r17, 10
       
   128 		call		div8u
       
   129 		add			r16, r4
       
   130 		rcall		Serial_Send
       
   131 		mov			r16, r15
       
   132 
       
   133 		add			r16, r4
       
   134 		rcall		Serial_Send
       
   135 		
       
   136 	ret
   109 
   137 
   110 ;; Program
   138 ;; Program
   111 message:	.db "Hello World", 13, 10, 0
   139 message:	.db "Hello World", 13, 10, 0
   112 
   140 
   113 Main:
   141 Main:
   123 
   151 
   124 ; Main program
   152 ; Main program
   125 		ldi			ZH, high(message * 2)
   153 		ldi			ZH, high(message * 2)
   126 		ldi			ZL, low(message * 2)
   154 		ldi			ZL, low(message * 2)
   127 		
   155 		
   128 		rcall		Serial_Write
   156 		rcall		Serial_Write_s
   129 
   157 
   130 ; Echo out
   158 ; Echo out
   131 		sbi			DDRB, PORTB5
   159 		sbi			DDRB, PORTB5
   132 		ldi			r20, 0
   160 		ldi			r20, 0
       
   161 		ldi			r21, 0
   133 
   162 
   134 Main_Echo:
   163 Main_Echo:
       
   164 		; blink LED
   135 		out			PORTB, r20
   165 		out			PORTB, r20
   136 
   166 		
       
   167 		; read
   137 		rcall		Serial_Recv
   168 		rcall		Serial_Recv
       
   169 		push		r16
       
   170 		
       
   171 		; running counter
       
   172 		inc			r21
       
   173 		mov			r16, r21
       
   174 		rcall		Serial_Write_c
       
   175 		
       
   176 		; ' '
       
   177 		ldi			r16, 32
   138 		rcall		Serial_Send
   178 		rcall		Serial_Send
   139 
   179 		
       
   180 		pop			r16
       
   181 		rcall		Serial_Send
       
   182 		
       
   183 		; '\r\n'
       
   184 		ldi			r16, 13
       
   185 		rcall		Serial_Send
       
   186 		ldi			r16, 10
       
   187 		rcall		Serial_Send
       
   188 		
       
   189 		; toggle
   140 		com			r20
   190 		com			r20
   141 
   191 		
       
   192 		; continue
   142 		rjmp		Main_Echo
   193 		rjmp		Main_Echo
   143 
   194