;	DS1620 thermometer
;	I2C 24lc01
;	Serial LCD
;	-2 degrees compensation
;	Tested on 22 feb 2006, seems to be OK

		device SX28l,turbo,stackx,optionx
		device oschs3
		freq	50000000
		reset	resetEntry

		LIST Q = 37

	global_org	=	$08
	bank0_org	=	$10
	bank1_org	=	$30
	bank2_org	=	$50
	bank3_org	=	$70
	bank4_org	=	$90
	bank5_org	=	$B0
	bank6_org	=	$D0
	bank7_org	=	$F0

		org	global_org		;global var

	bankglob	=	$

temp		ds	1
byte		ds	1
cmd		ds	1
hex		ds	1
string		ds	1	
var		ds	1
number_low	ds	1
number_high	ds	1

		org	bank0_org		;serial var
						
	bank0		=	$
	serial		=	$

tx_high		ds	1
tx_low		ds	1
tx_count	ds	1
tx_divide	ds	1
rx_byte		ds	1
rx_count	ds	1
rx_divide	ds	1
rx_flag		ds	1

		org	bank1_org		;blinking LED var

	bank1		=	$
	timer		=	$
	isrMultiplex	=	$

isrmulti	ds	1
						
timer_acc1_low	ds	1
timer_acc1_high	ds	1

		org	bank2_org		;ISR thread var

	bank2		=	$
;	isrMultiplex	=	$

;isrmulti	ds	1

		org	bank3_org		;ds1620 comm var

	bank3		=	$
	therm		=	$

dsdata		ds	1
bits		ds	1
flags		ds	1
temp1		ds	1
temp2		ds	1
temp3		ds	1
sign		=	flags.0
clk9		=	flags.1

		org	bank4_org		;converting ds1620 to degrees var

	bank4		=	$
	omzet		=	$

honderd		ds	1
tiental		ds	1
eenheden	ds	1
posneg		ds	1
backup1		ds	1
backup2		ds	1
flagsgraden	ds	1
halvegraad	=	flagsgraden.0

		org	bank5_org		;I2C var

	bank5		=	$
	i2c		=	$

bitsi2c		ds	1
datai2c		ds	1
i2cdata		ds	1
wordadress	ds	1
control		ds	1
direction	ds	1

		org	bank6_org		;Var for min,max,cur temp

	bank6		=	$
	RAMdegrees	=	$

mintemp		ds	1
minsign		ds	1
minhalf		ds	1
curtemp		ds	1
cursign		ds	1
curhalf		ds	1
maxtemp		ds	1
maxsign		ds	1
maxhalf		ds	1
;--------------------------------------------------------------------------------------------------------------
		id	'1620_I2C'		;ID
;--------------------------------------------------------------------------------------------------------------
	tx_pin_lcd	=	ra.1
	tx_pin		=	ra.3
	rx_pin		=	ra.2
	flashLed_pin	=	ra.0
	RST		=	rb.0
	CLK		=	rb.1
	DQ		=	rb.2
	DQin		=	4
	Dqout		=	0
	sda		=	rc.0
	scl		=	rc.1
	int_period	=	163
;--------------------------------------------------------------------------------------------------------------
INTERRUPT_ORG		equ	$0
RESET_ENTRY_ORG		equ	$1FB
SUBROUTINES_ORG		equ	$200		;jmp table must be in lower half for calling subs
PAGE3_ORG		equ	$300
SUBROUTINES2_ORG	equ	$400		;jmp table must be in lower half for calling subs
STRING_ORG		equ	$040;500
PAGE5_ORG		equ	$500
MAIN_PROGRAM_ORG	equ	$600
;---------------------------------------------------------------------------------------------------------------
		org	INTERRUPT_ORG

interrupt	bank	isrMultiplex
		inc	isrmulti
		mov	w,isrmulti
		jmp	pc+w
		jmp	isrthread1
		jmp	isrthread2

isrthread1	bank	serial
send		test	tx_count
		jz	receive
		dec	tx_divide
		jnz	noisr
		mov	tx_divide,#16			; 32 if ony 1 thread
		stc
		rr	tx_high
		rr	tx_low
		dec	tx_count
		movb	tx_pin_lcd,/tx_low.6		; for tx to LCD Inverse
		movb	tx_pin,tx_low.6			; for tx to RS232

receive		movb	c,rx_pin
		test	rx_count
		jnz	:rxbit
		mov	w,#9
		sc
		mov	rx_count,w
		mov	rx_divide,#24			; 48 if only 1 thread
:rxbit		djnz	rx_divide,noisr
		mov	rx_divide,#16			; 32 if only 1 thread
		dec	rx_count
		sz
		rr	rx_byte
		snz
		setb	rx_flag

		jmp	noisr

isrthread2	bank 	timer
		inc	timer_acc1_low
		snz	
		inc	timer_acc1_high
		movb	flashLed_pin,timer_acc1_high.6

		bank	isrMultiplex
		mov	isrmulti,#255
		
noisr		mov	w,#-int_period
		retiw
;-------------------------------------------------------------------------------------------------------------
		org	RESET_ENTRY_ORG

resetEntry	page	_resetEntry
		jmp	_resetEntry
;--------------------------------------------------------------------------------------------------------------
		org	SUBROUTINES_ORG
send_hex	jmp	@_send_hex
send_byte	jmp	@_send_byte
send_string	jmp	@_send_string
get_byte	jmp	@_get_byte
get_hex		jmp	@_get_hex
waitmsec	jmp	@_waitmsec
upper_case	jmp	@_upper_case
delayeeprom	jmp	@_delayeeprom
delayclk	jmp	@_delayclk
send_graden	jmp	@_send_graden
shin		jmp	@_shin
shout		jmp	@_shout
init_ds1620	jmp	@_init_ds1620
start_conv	jmp	@_start_conv
read_conf	jmp	@_read_conf
read_temp	jmp	@_read_temp
write_temp	jmp	@_write_temp
reset_flags	jmp	@_reset_flags
graden		jmp	@_graden
convert		jmp	@_convert
savemax		jmp	@_savemax
savemin		jmp	@_savemin

_send_hex	mov	hex,w				;Sending Hex routine						
		mov	w,<>hex
		call	:digit
		mov	w,hex
:digit		and	w,#$F
		mov	temp,w
		mov	w,#STRING_ORG>>8
		mov	m,w
		mov	w,#_hex
		clc
		add	w,temp
		iread
		mov	m,#$F				;restore mode

_send_byte	bank	serial				;Send_byte routine
:wait		test	tx_count
		jnz	:wait
		mov	tx_high,w
		clrb	tx_low.7
		mov	tx_count,#10
		retp

_send_string	mov	string,w			;Send_string routine
:nxtchar	mov	w,#STRING_ORG>>8
		mov	m,w
		mov	w,string
		iread
		test	w
		snz
		jmp	:endsend_string
		call	send_byte			
		inc	string
		jmp	:nxtchar
:endsend_string	;mov	w,#$1F
		;mov	m,w
		mov	m,#$F
		retp
			
_get_byte	bank	serial				;Get_Byte routine
		jnb	rx_flag,$
		mov	byte,rx_byte
		clrb	rx_flag
		retp

_get_hex	mov	var,#4				;Get_hex routine
:nexthex	call	get_byte
		mov	w,byte
		call	send_byte
		mov	w,<>byte
		mov	hex,w
		cjb	byte,#'0',:done
		cjbe	byte,#'9',:got
		call	upper_case
		cjb	byte,#'A',:done
		cja	byte,#'F',:done
		add	hex,#$90
:got		mov	temp,#4
:shift		rl	hex
		rl	number_low
		rl	number_high
		djnz	temp,:shift
		djnz	var,:nexthex
		retp	
:done		mov	cmd,#'X'
		retp

_waitmsec	mov	byte,w				;delay with w as param
		clr	temp
		clr	var
waitmsecc	djnz	temp,waitmsecc
		djnz	var,waitmsecc
		djnz	byte,waitmsecc
		retp

_upper_case	csae	byte,#'a'			;make uppercase with byte as param
		retp
		sub	byte,#'a'-'A'
		retp

_delayEEprom	bank	therm				;delay for eeprom ds1620
		clr	temp1
		clr	temp2
		mov	temp3,#3	
delay20ms	djnz	temp1,delay20ms
		djnz	temp2,delay20ms
		djnz	temp3,delay20ms
		retp

_delayclk	bank	therm				;delay for clk ds1620
		clr	temp1	
delayclko	djnz	temp1,delayclko
		retp

_send_graden	bank	omzet			;For sending the degrees
		mov	w,posneg
		call	@send_byte
		;bank	omzet
		;mov	w,honderd
		;call	@send_byte
		bank	omzet
		mov	w,tiental
		call	@send_byte
		bank	omzet
		mov	w,eenheden
		call	@send_byte
		mov	w,#46			;point
		call	@send_byte
		bank	omzet
		sb	halvegraad
		mov	w,#48
		snb	halvegraad
		mov	w,#53			
		call	@send_byte
		mov	w,#_degrees		;°C
		call	@send_string
		retp

_shin		bank	therm			;Shifting In routine
		mov	!rb,#Dqin
		mov	bits,#9
		clc
:nextshin	rr	dsdata
		clrb	clk
		call	@delayclk	
		movb	c,dq
		call	@delayclk
		setb	clk
		call	@delayclk
		call	@delayclk
		djnz	bits,:nextshin
		movb	sign,c
		;clrb	RST
		retp

_shout		bank	therm			;Shifting out routine
		mov	!rb,Dqout
		mov	bits,#9
		movb	c,sign
:begin		;setb	RST
:nextshout	rr	dsdata
		clrb	clk
		call	@delayclk
		movb	dq,c
		call	@delayclk
		mov	w,--bits
		snz	
		snb	clk9
		setb	clk
		call	@delayclk
		call	@delayclk
		djnz	bits,:nextshout		
		retp

_init_ds1620	bank	therm			;init ds1620
		clrb	clk9	
		clrb	sign	
		setb	clk	
		setb	rst
		mov	dsdata,#$0C
		call	@shout
		mov	dsdata,#$02
		call	@shout
		clrb	rst
		call	@delayeeprom
		retp

_start_conv	bank	therm			;start convertion ds1620
		clrb	clk9
		setb	clk
		setb	rst
		mov	dsdata,#$EE
		call	@shout
		clrb	rst
		call	@delayeeprom
		retp

_read_conf	bank	therm			;Read config of ds1620
		clrb	clk9
		setb	rst
		mov	dsdata,#$ac
		call	@shout
		call	@shin
		clrb	rst
		retp

_read_temp	bank	therm			;Read temp of ds1620
		clrb	clk9
		setb	clk
		setb	rst
		mov	dsdata,w
		call	@shout
		setb	clk9
		call	@shin
		clrb	rst
		retp

_write_temp	bank	therm			;Write temp to ds1620
		clrb	clk9	
		;clrb	sign	
		mov	sign,var
		setb	clk	
		setb	rst
		mov	dsdata,cmd
		call	@shout
		setb	clk9
		mov	dsdata,temp
		call	@shout
		clrb	rst
		call	@delayeeprom
		retp

_reset_flags	bank	therm			;Reset flags of ds1620
		clrb	clk9	
		clrb	sign	
		setb	clk	
		setb	rst
		mov	dsdata,#$0C
		call	@shout
		mov	dsdata,#$02
		call	@shout
		clrb	rst
		call	@delayeeprom
		retp

_graden		bank	therm			;Routine for adjusting temp,and + - sign 
		movb	sign,var.0		;mov	sign,var

		bank	omzet
		mov	backup1,temp
		;mov	backup1,w
		;****************************************************************
		setb	status.0		;set carry for subtraction borrow

		bank	omzet
		;sub	backup1,#6		;compensating for -3 graden
		sub	backup1,#4		;compensating for -2 graden

		sb	status.0		;check status
		jmp	vervolg1		;if c = 1 positieve graden
		jmp	vervolg2		;if c = 0 negatieve graden

vervolg1	bank	therm
		setb	sign			;to act like neg values	
		;*****************************************************************
vervolg2	bank	therm		
		sb	sign
		jmp	poss
		snb	sign
		jmp	negg

poss		bank	omzet			;Place + 
		mov	posneg,#43
		jmp	endgraden

negg		bank	omzet			;Place - , converting 2 compl
		mov	posneg,#45
		;*******************converting 2 compl*****************************
		;dec	backup1
		;not	backup1
		not	backup1
		inc	backup1
		;******************************************************************
endgraden	mov	temp,backup1
		mov	var,posneg
		retp
		
_convert	bank	omzet			;Making the hundert,dec,unit,half degrees
		clr	honderd
		clr	tiental	
		clr	eenheden
		mov	backup1,temp		

		setb	halvegraad		;Getting the half degrees bit out
		clc
		rr	backup1	
		sc
		clrb	halvegraad

		mov	backup2,backup1
hunderd		sub	backup1,#100
		sc
		jmp	decimal
		inc 	honderd
		mov	backup2,backup1
		jmp	hunderd
decimal		mov	backup1,backup2
decimall	sub	backup1,#10
		sc
		jmp	unit
		inc	tiental
		mov	backup2,backup1
		jmp	decimall
unit		mov	backup1,backup2
		mov	eenheden,backup1
		add	eenheden,#48
		add	tiental,#48
		add	honderd,#48
		retp

_savemax	bank	ramdegrees			;Saving max temp to 24LC01
		mov	maxtemp,curtemp
		mov	maxsign,cursign
		mov	number_low,cursign
		mov	number_high,curtemp
		bank	i2c
		mov	wordadress,#$05
		mov	datai2c,number_low
		call	@writeEE
		mov	wordadress,#$06
		mov	datai2c,number_high
		call	@writeEE
		retp

_savemin	bank	ramdegrees			;Saving min temp to 24LC01
		mov	mintemp,curtemp
		mov	minsign,cursign
		mov	number_low,cursign
		mov	number_high,curtemp
		bank	i2c
		mov	wordadress,#$01
		mov	datai2c,number_low
		call	@writeEE
		mov	wordadress,#$02
		mov	datai2c,number_high
		call	@writeEE
		retp
;----------------------------------------------------------------------------------------------------------
		org	subroutines2_org

i2cstart	jmp	@_i2cstart
i2cstop		jmp	@_i2cstop
i2csendbyte	jmp	@_i2csendbyte
i2cack		jmp	@_i2cack
i2cnack		jmp	@_i2cnack
i2creceivebyte	jmp	@_i2creceivebyte
wait2_5us	jmp	@_wait2_5us
wait5ms		jmp	@_wait5ms
WriteEE 	jmp	@_WriteEE
ReadEE		jmp	@_ReadEE

_i2cstart	bank	i2c		;sda high to low when scl is high;  
		mov	fsr,#direction
		;set both line high
		setb	ind.0		;sda
		setb	ind.1		;scl
		mode	$0F
		mov	!rc,ind
		;generate a clocklow 2*delay
		clrb	ind.1		;scl
		mov	!rc,ind
		clrb	scl
		call	@wait2_5us	
		call	@wait2_5us	
		;generate a clockhigh 1*delay
		setb	ind.1		;scl
		mov	!rc,ind
		call	@wait2_5us	
		;make a high to low of sda
		clrb	ind.0		;sda
		mov	!rc,ind
		clrb	sda
		call	@wait2_5us
		;make both lines ready for next clock;also used for following subroutines
prep_nextscl	clrb	ind.1		;scl
		mov	!rc,ind
		clrb	scl
		retp

_i2cstop	bank	i2c		;sda low to high when scl is high; 
		mov	fsr,#direction
		;generate a clocklow 2*delay
		clrb	ind.0		;sda
		clrb	ind.1		;scl
		mov	!rc,ind
		clrb	scl
		clrb	sda
		call	@wait2_5us	
		call	@wait2_5us	
		;generate a clockhigh 1*delay
		setb	ind.1		;scl
		mov	!rc,ind
		call	@wait2_5us
		;make a low to high of sda
		setb	ind.0		;sda
		mov	!rc,ind
		call	@wait2_5us
		;set both line high,see above setb instructions	
		retp

_i2csendbyte	bank	i2c		;data changing when clk is low stable when clk is high
		mov	fsr,#direction
		mov	bitsi2c,#8
cont		clrb	ind.1		;scl
		mov	!rc,ind
		clrb	scl
		call	@wait2_5us
		rl	i2cdata
		movb	ind.0,c
		mov	!rc,ind
		clrb	sda
		call	@wait2_5us
		setb	ind.1		;scl
		mov	!rc,ind
		call	@wait2_5us
		call	@wait2_5us
		djnz	bitsi2c,cont
		jmp	@prep_nextscl
;		clrb	ind.1		;scl
;		mov	!rc,ind
;		clrb	scl
;		retp

_i2cack		bank	i2c		;sda low when scl is high respondse ;from slave
		mov	fsr,#direction
		clrb	ind.1		;scl
		mov	!rc,ind
		clrb	scl
		call	@wait2_5us
		call	@wait2_5us
above_i2cack	setb	ind.1		;scl
		mov	!rc,ind
		call	@wait2_5us
		call	@wait2_5us
		jmp	@prep_nextscl
;		clrb	ind.1		;scl
;		mov	!rc,ind
;		clrb	scl
;		retp

_i2cnack	bank	i2c		;sda keep high when scl is high ;from master
		mov	fsr,#direction
		clrb	ind.1		;scl
		mov	!rc,ind
		clrb	scl
		call	@wait2_5us
		setb	ind.0		;sda
		mov	!rc,ind
		call	@wait2_5us
		jmp	@above_i2cack
;		setb	ind.1		;scl
;		mov	!rc,ind
;		call	@wait2_5us
;		call	@wait2_5us
;		;clrb	ind.0		;sda
;		clrb	ind.1		;scl
;		mov	!rc,ind
;		clrb	scl
;		;clrb	sda
;		retp
	
_i2creceivebyte	bank	i2c		;sda stable when scl is high
		mov	fsr,#direction
		setb	ind.0		;sda
		mov	!rc,ind
		mov	bitsi2c,#8
		clr	i2cdata
contt		
		clrb	ind.1		;scl
		mov	!rc,ind
		clrb	scl
		call	@wait2_5us
		call	@wait2_5us
		setb	ind.1		;scl
		mov	!rc,ind
		call	@wait2_5us
		movb	c,sda
		rl	i2cdata
		call	@wait2_5us
		djnz	bitsi2c,contt
		jmp	@prep_nextscl
;		clrb	ind.1		;scl
;		mov	!rc,ind
;		clrb	scl
;		retp

_wait2_5us	mov	temp,#31		;waiting delay for clock of i2c
wait2_5uss	djnz	temp,wait2_5uss
		retp

_wait5ms	mov	temp,#255		;waiting delay for programming data in eeprom
		mov	byte,#255
wait5mss	djnz	temp,wait5mss
		djnz	byte,wait5mss
		retp

;write a byte:-> start,control,ack,wordadress,ack,data,ack,stop	
;See datasheet of 24lc01
_WriteEE	bank	i2c
		mov	control,#$A0
		;bank	i2c
		call	@i2cstart
		;bank	i2c
		mov	i2cdata,control
		call	@i2csendbyte
		call	@i2cack
		;bank	i2c
		mov	i2cdata,wordadress
		call	@i2csendbyte
		call	@i2cack
		;bank	i2c
		mov	i2cdata,datai2c
		call	@i2csendbyte
		call	@i2cack
		call	@i2cstop
		call	@wait5ms
		retp

;read a byte:-> start,control,ack,wordadress,ack,start,control,ack,data,nack,stop
;See datasheet of 24lc01
_readEE		bank	i2c
		mov	control,#$A0
		;bank	i2c
		call	@i2cstart
		;bank	i2c
		mov	i2cdata,control
		call	@i2csendbyte
		call	@i2cack
		;bank	i2c
		mov	i2cdata,wordadress
		call	@i2csendbyte
		call	@i2cack
		;bank	i2c
		mov	control,#$A1
		;bank	i2c
		call	@i2cstart
		;bank	i2c
		mov	i2cdata,control
		call	@i2csendbyte
		call	@i2cack
		;bank	i2c
		call	@i2creceivebyte
		call	@i2cnack
		call	@i2cstop
		retp

;----------------------------------------------------------------------------------------------------------
		org	STRING_ORG
		
_resetLcd	dw	254,2,254,1,254,128,0		;LCD instructies
_line1		dw	254,128,0
_line2		dw	254,192,0
_Blank		dw	'                ',0
_hex		dw	'0123456789ABCDEF'
_degrees	dw	'',223,'C',0
_Reading	dw	'Reading Temp ...',0
_readingmin	dw	'Min: ',0
_readingmax	dw	'Max: ',0	
_readingcur	dw	'Cur: ',0
_chr0		dw	254,64,10,10,10,10,10,14,31,31,0
_chr1		dw	254,72,10,10,10,14,14,14,31,31,0
_chr2		dw	254,80,10,14,14,14,14,14,31,31,0
_aniloc		dw	254,207,0
;----------------------------------------------------------------------------------------------------------
		ORG	MAIN_PROGRAM_ORG

_resetEntry	mov	ra,#%1000
		mov	!ra,#%0100
		mov	rb,#%0000

		bank	i2c
		mov	fsr,#direction	;placed content of adress direction into fsr
		mov	ind,#%00000011	;make only the i2c lines as
		mov	!rc,ind		;input

		clr	fsr
:clsram		setb	fsr.4
		clr	ind
		ijnz	fsr,:clsram
		
		mov	!option,#%10011111
		
		bank	serial
		mov	tx_divide,#16			;16was32

		mov	w,#150				;LCD Startup time 750ms
		call	@waitmsec
		mov	w,#_resetLcd
		call	@send_string
		
		mov	w,#_chr0			;LOading animation
		call	@send_string
		mov	w,#_chr1
		call	@send_string
		mov	w,#_chr2
		call	@send_string

		mov	w,#_resetlcd
		call	@send_string
		
		;enable next lines if new DS1620 is used, only once for setting up device
		;bank 	therm		
		call	@init_ds1620
		;mov	var,#$00		;pos or neg
		;mov	cmd,#$01		;low or high register
		;mov	temp,#$FA		;temp
		;call	@write_temp
		;mov	var,#$01
		;mov	cmd,#$02
		;mov	temp,#$92
		;call	@write_temp
		
		call	@start_conv

		mov	w,#_Reading
		call	@send_string

		mov	cmd,#$FF		;for animation

		mov	w,#$AA			;Dummy Read
		call	@read_temp		;Dummy Read
		mov	w,#150			;Dummy read delay
		call	@waitmsec		;need 750ms for conversion
	
		mov	w,#$AA			;initializing all max,min values
		call	@read_temp
		bank	therm
		mov	temp,dsdata
		clr	var
		movb	var.0,sign

		call	@graden
		
		bank	ramdegrees
		mov	curtemp,temp
		mov	cursign,var		
		;mov	mintemp,temp
		;mov	minsign,var		
		;mov	maxtemp,temp
		;mov	maxsign,var

		bank	i2c
		mov	wordadress,#$00
		call	@readEE
		mov	number_low,i2cdata
		cje	number_low,#$00,:readminmax

		bank	ramdegrees
		mov	number_low,cursign
		mov	number_high,curtemp
		bank	i2c
		mov	wordadress,#$00
		mov	datai2c,#$00
		call	@writeEE
		mov	wordadress,#$01
		mov	datai2c,number_low
		call	@writeEE
		mov	wordadress,#$02
		mov	datai2c,number_high
		call	@writeEE
		mov	wordadress,#$05
		mov	datai2c,number_low
		call	@writeEE
		mov	wordadress,#$06
		mov	datai2c,number_high
		call	@writeEE
		
:readminmax	bank	i2c
		mov	wordadress,#$01
		call	@readEE
		mov	number_low,i2cdata
		mov	wordadress,#$02
		call	@readEE
		mov	number_high,i2cdata

		bank	ramdegrees
		mov	minsign,number_low
		mov	mintemp,number_high

		bank	i2c
		mov	wordadress,#$05
		call	@readEE
		mov	number_low,i2cdata
		mov	wordadress,#$06
		call	@readEE
		mov	number_high,i2cdata

		bank	ramdegrees
		mov	maxsign,number_low
		mov	maxtemp,number_high
:mainloop
		mov	w,#$AA
;		mov	w,#$A1			;reading out of max and min ds1620
;		mov	w,#$A2
		call	@read_temp
		mov	temp,dsdata
		clr	var
		mov	var,sign
;		mov	temp,#$CE		;manual input of degrees for testing
;		mov	var,#$1
		call	@graden
		
		bank	ramdegrees
		mov	curtemp,temp
		mov	cursign,var

		stc
		cje	cursign,#43,:pos
		stc
		cje	cursign,#45,:neg
		jmp	:exit

:pos		stc
		cjne	maxsign,minsign,:notequal
		stc
		cje	maxsign,#43,:betweenpos
		stc
		cje	maxsign,#45,:fromnegtopos
		jmp	:exit

:betweenpos	stc
		cja	curtemp,maxtemp,:hogerpos
		stc
		cjb	curtemp,mintemp,:lagerpos
		jmp	:exit
:hogerpos	call	@savemax
		
		jmp	:exit
:lagerpos	call	@savemin
		jmp	:exit

:fromnegtopos	call	@savemax
		jmp	:exit

:notequal	stc
		cje	cursign,#43,:checkgroter
		stc
		cje	cursign,#45,:checkkleiner
		jmp	:exit

:checkgroter	stc
		cja	curtemp,maxtemp,:hogerpos
		jmp	:exit
:checkkleiner	stc
		cja	curtemp,mintemp,:hogerneg
		jmp	:exit

:neg		stc
		cjne	maxsign,minsign,:notequal
		stc
		cje	maxsign,#43,:frompostoneg
		stc
		cje	maxsign,#45,:betweenneg
		jmp	:exit

:betweenneg	stc
		cja	curtemp,mintemp,:hogerneg
		stc
		cjb	curtemp,maxtemp,:lagerneg
		jmp	:exit
:hogerneg	call	@savemin
		jmp	:exit
:lagerneg	call	@savemax
		jmp	:exit

:frompostoneg	call	@savemin
		jmp	:exit
	
:exit
		mov	w,#_aniloc
		call	@send_string
		inc	cmd
		and	cmd,#$00
		mov	w,cmd
		call	@send_byte
		mov	w,#_line2
		call	@send_string
		mov	w,#_readingmin
		call	@send_string
		bank	ramdegrees
		mov	temp,mintemp
		mov	var,minsign
		bank	omzet
		mov	posneg,var
		call	@convert
		call	@send_graden

		mov	w,#150
		call	@waitmsec

		mov	w,#_aniloc
		call	@send_string
		inc	cmd
		mov	w,cmd
		call	@send_byte
		mov	w,#_line2
		call	@send_string
		mov	w,#_readingcur
		call	@send_string
		bank	ramdegrees
		mov	temp,curtemp
		mov	var,cursign
		bank	omzet
		mov	posneg,var
		call	@convert
		call	@send_graden

		mov	w,#150
		call	@waitmsec

		mov	w,#_aniloc
		call	@send_string
		inc	cmd
		mov	w,cmd
		call	@send_byte
		mov	w,#_line2
		call	@send_string
		mov	w,#_readingmax
		call	@send_string
		bank	ramdegrees
		mov	temp,maxtemp
		mov	var,maxsign
		bank	omzet
		mov	posneg,var
		call	@convert
		call	@send_graden
		
		mov	w,#150
		call	@waitmsec

		jmp	:mainloop

		END		