;*********************************************************************************
;	gpspeed-test-display	GpSpeed temperature test
;				Louise@RotaryRacer, Terry@RotaryRacer
;*********************************************************************************
;
; This tests writing information onto the LCD display.
;
; For: PICAXE-28X2 (PIC18F25K22)
;
; GpSpeed pins are:
;	B.1	LED
;	C.2	Motor PWM
;	C.1	Fan PWM
;	A.0	Analogue Input 0, I0, throttle
;	A.1	Analogue Input 1, I1, misc
;	A.2	Analogue Input 2, Voltage measurement
;	A.3	Analogue Input 3, Current measurement (Hall effect)
;	B.4	Analogue/Digital Input, I2
;
;	B.2	Analogue Input 8, Current measurement (Mosfet)
;	B.0	Error, error from Motor PWM
;	C.3	I2C Clk
;	C.4	I2c Dat
;	C.0	User 0, I2c Int
;	B.3	User 1
;	C.6	SER1 TX
;	C.7	SER1 RX
;

; Symbols for pin names and variables
symbol	pinLed		= B.1			; The LED pin

init:
	let adcsetup = %000000000001111		; Initialises ADC inputs
	gosub lcdInit					; Setup I2C hardware

mainLoop:
	readadc10 A.1,w8				; Reads the ADC temperature input
;	w8 = w8 * 24					; Scale to degreesC for TMP37
	w8 = w8 * 49					; Scale to degreesC for TMP36

	; Display on LCD if connected
	b2 = 0
	w0 = w8
	gosub lcdPutDecimalWord100

	; Print out on PixAxe terminal if enabled
	sertxd ("Temperature: ", #w8, 13, 10)	; Print a message with the temperature  value, followed by CR NL (A new line)

	toggle pinLed		; Turns LED on
	pause 1000		; Delays for 500ms
	goto mainLoop


;******************************************************************************
;	LCD Subroutines
;******************************************************************************

; Initialise the LCD display
lcdInit:
	pause 500
	hi2csetup i2cmaster,$C6,i2cslow,i2cbyte
	hi2cout 0,(254,1,255)
	hi2cout 0,(254,128,255)
	return

; Set the print position to the position defined in b2. Line 1 = +64
lcdSetPos:
	b2 = 128 + b2
	hi2cout 0,(254,b2,255)
	return

; Print a single chracher that is in b0 at the position defined in b2. Line 1 = +64
lcdPutChar:
	b2 = 128 + b2
	hi2cout 0,(254,b2,b0,255)
	pause 10
	return

;Print the value in b0 as a decimal at the position defined in b2. Line 1 = +64
lcdPutDecimalByte:
	b2 = 128 + b2
	b3 = b0 / 100 + 48
	b0 = b0 // 100;
	b4 = b0 / 10 + 48
	b0 = b0 // 10;
	b5 = b0 + 48
	
	hi2cout 0,(254,b2,b3,b4,b5,255)
	pause 10
	return

;Print the value in w0 as a decimal at the position defined in b2. Line 1 = +64
lcdPutDecimalWord:
	b2 = 128 + b2
	b3 = w0 / 10000 + 48
	w0 = w0 // 10000;
	b4 = w0 / 1000 + 48
	w0 = w0 // 1000;
	b5 = w0 / 100 + 48
	w0 = w0 // 100;
	b6 = w0 / 10 + 48
	w0 = w0 // 10;
	b7 = w0 + 48

	if b3 = "0" then
		b3 = " "
		if b4 = "0" then
			b4 = " "
			if b5 = "0" then
				b5 = " "
				if b6 = "0" then
					b6 = " "
				endif
			endif
		endif
	endif
		
	writei2c 0,(254,b2,b3,b4,b5,b6,b7,255)
;	pause 10

	return

;Print the value in w0 as a decimal scaled by 100 at the position defined in b2. Line 1 = +64
lcdPutDecimalWord100:
	b2 = 128 + b2
	b3 = w0 / 10000 + 48
	w0 = w0 // 10000;
	b4 = w0 / 1000 + 48
	w0 = w0 // 1000;
	b5 = w0 / 100 + 48
	w0 = w0 // 100;
	b6 = w0 / 10 + 48
	w0 = w0 // 10;
	b7 = w0 + 48

	if b3 = "0" then
		b3 = " "
		if b4 = "0" then
			b4 = " "
		endif
	endif
		
	writei2c 0,(254,b2,b3,b4,b5,".",b6,b7,255)
	pause 10
	return