📄 convert.asm
字号:
;************************************************
;* Conversion routines for AVR's *
;* *
;* Copyright 2001-2003 Wayne Peacock *
;* Wayne Peacock 2nd July 2001 *
;* Version 1.00 *
;************************************************
;* See Schematic for Hardware connections *
;* *
;* Disclaimer: *
;* The Author takes no responsibility for the *
;* use of this code. Use at your own risk! *
;* *
;* This code or part there of is licensed only *
;* for private use and NOT for commercial use. *
;* *
;* Please contact the author 'Wayne Peacock' *
;* <wpeacock@senet.com.au> before using the code*
;* or part there of in a commercial project. *
;************************************************
; Requires the following
.def unit = r22
.def tens = r23
.def hund = r24 ; Used as Temp in hexasc!
.def hex = r25
;************************************************
;* Converts hex value to two ascii values *
;* for LCD or RS232 etc.... *
;* Input: hex *
;* Output: unit *
;* tens *
;* Requires: hund (Temp) *
;************************************************
; Convert first nibble
hexasc:
mov unit, hex ; Copy value to work with
cbr unit, 0xF0 ; Remove high nibble
cpi unit, 0x0A ; Check if 0-9
brsh hexasc1
sbr unit, 0x30 ; Value must be 0-9
rjmp hexasc2
hexasc1:
ldi hund, 0x37 ; Value must be A-F
add unit,hund
; Convert second nibble
hexasc2:
mov tens, hex ; Copy value to work with
swap tens
cbr tens, 0xF0 ; Remove high nibble
cpi tens, 0x0A ; Check if 0-9
brsh hexasc3
sbr tens, 0x30 ; Value must be 0-9
ret
hexasc3:
ldi hund, 0x37 ; Value must be A-F
add tens,hund
ret
;************************************************
;* Converts two ascii values to one hex *
;* for user input (RS232 etc....) *
;* *
;* Input: unit *
;* tens (overwrites) *
;* Output: hex *
;************************************************
; Convert first ascii value
aschex:
mov hex, unit ; Move units into hex
subi hex,0x30 ;
cpi hex, 0x0a ; Check for A to F
brlo aschex1
subi hex, 0x07 ; Convert A-F to hex
; Convert second ascii value
aschex1:
subi tens, 0x30
cpi tens, 0x0a ; Check if A to F
brlo aschex2
subi tens, 0x07 ; Converts A-F to hex
aschex2:
swap tens
add hex, tens ; Add values together
ret
;************************************************
;* Converts hex value to decimal (3 ASCII *
;* values) for user output (RS232, LCD etc....) *
;* *
;* Input: hex *
;* Output: unit *
;* tens *
;* hund *
;************************************************
hexdec:
ldi unit,0x30 ; Units
ldi tens,0x30 ; Tens
ldi hund,0x30 ; Hundreds
cpi hex,0x64 ; Compare the quantity in Hex =>100(64 in Hex)
brsh hexdec1 ; Over 100!
cpi hex,0x0A ; Compare if we have 10 inside the number
brsh hexdec2 ; Tens to convert
add unit,hex ; Units only
ret
hexdec1:
subi hex,0x64 ; Get hundreds by subtracting 100
inc hund
cpi hex,0x64 ; Repeat until under 100
brsh hexdec1
cpi hex,0x0A ; Check if the result have some 10 inside.
brsh hexdec2
hexdec2:
subi hex,0x0A ; Get tens value by subtracting 10
inc tens
cpi hex,0x0A ; Repeat until under 10
brsh hexdec2
add unit,hex
ret
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -