;SL100 + Maple systems HMI520T

***********************************
* Maple Systems Touch Screen Demo *
***********************************

* Simply echoes the state of the SPLat inputs 0 - 7 to the touch screen for display, echoes the touch
* screen toggle push buttons to the SPLat outputs 0 - 7 and sends analog value from the analog input 
* on the SPLat controller to the touch screen for display on a moving needle style meter display. SPLat 
* board runs as a MODBUS master and therefore must have a firmware capable of MODBUS master operation.

* We have not managed to get the Maple systems HMI520T to run as a MODBUS master with the SPLat controller
* as the slave, and Maple systems have been less than responsive to our questions regarding this.

******************
* System Equates *
******************

;Comms Equates:

SlaveAddr	equ		1			;slave address
MsgeGap		equ		0			;inter message gap (3.5 character times)
MsgeTimeOut	equ		20			;message timeout time 20 x 10ms = 200ms

BaudRate	equ		5			;0 - 300
							;1 - 600
							;2 - 1200
							;3 - 2400
							;4 - 4800
							;5 - 9600
							;6 - 19200
							;7 - 38400

;Memory Equates:

InputData	mequ		0			;input data to be sent to touch screem
OutputData	mequ		10			;output data sent from touch screen
AnData		mequ		20			;analog data to send to touch screen

***********
* Program *
***********

start		gosub		IOTaskInit		;initialise the IO task
		gosub		CommsInit		;initialise communications task
		runtasksforever


************
* I/O Task * 
************

* The folowing task is resonsible for reading the inputs 0 - 7 and
* making them available in memory to be sent to the touch screen by
* MODBUS. It also sends the contents of memory location "OutputData"
* to the outputs and reads the value of the analogue input and stores
* it into location "AnData" for transmission to the touch screen via
* MODBUS.

* Initialisation *

IOTaskInit	launchtask	IOTask		;launch button scanning task
		return

* Task *

IOTask		yieldtask
		inputfm		0		;get latest input data
		store		InputData

		recall		OutputData	;update SPLat outputs
		loadx		255
		outputm		0

		aninA				;get latest analogue data
		store		AnData
		goto		IOTask


******************
* Comms routines *
******************

* The following task initialises MODBUS master comms and startes the MODBUS script running.

* Initialisation *

CommsInit	launchtask	Comms
		return

* Task *

Comms		yieldtask
		ComTestStartTimer			;wait for protocol change lockout timer to expire
		goift		Comms

		comsetccb	MM			;initialise communications parameters and MODBUS master
		comrunscript	MMscr			;run the MODBUS master script
		killtask				;we're done


***************
* NVEM memory *
***************

	nvem0

* Master Comms control block *

MM	nv0byte		2		;0  select MODBUS master
	nv0byte		0		;1  device address
	nv0byte		0		;2  comms profile 8,n,1
	nv0byte		BaudRate	;3  baud rate
	nv0byte		0		;4  ** not used **
	nv0byte		0		;5  ** not used **
	nv0byte		0		;6  ** not used **
	nv0byte		0		;7  ** not used **
	nv0byte		0		;8  ** not used **
	nv0byte		MsgeGap		;9  inter message gap
	nv0byte		MsgeTimeOut	;10 message timeout time
	nv0byte		0		;11 retry count
	nv0byte		0		;12 ** not used **
	nv0byte		0		;13 ** not used **
	nv0byte		0		;14 ** not used **
	nv0byte		0		;15 ** not used **
	nv0byte		0		;16 ** not used **
	nv0byte		0		;17 ** not used **
	nv0byte		0		;18 ** not used **
	nv0byte		0		;19 ** not used **

* MODBUS Master script *

	nv0byte		0

MMscr	nv0byte		5,SlaveAddr,#0,#0		;Fn 5 - force single coil, SPLat semaphore 0 --> coil 1 in slave (inputs --> touch screen)
	nv0byte		5,SlaveAddr,#1,#1		;Fn 5 - force single coil, SPLat semaphore 1 --> coil 2 in slave
	nv0byte		5,SlaveAddr,#2,#2		;Fn 5 - force single coil, SPLat semaphore 2 --> coil 2 in slave
	nv0byte		5,SlaveAddr,#3,#3		;Fn 5 - force single coil, SPLat semaphore 3 --> coil 3 in slave
	nv0byte		5,SlaveAddr,#4,#4		;Fn 5 - force single coil, SPLat semaphore 4 --> coil 4 in slave
	nv0byte		5,SlaveAddr,#5,#5		;Fn 5 - force single coil, SPLat semaphore 5 --> coil 5 in slave
	nv0byte		5,SlaveAddr,#6,#6		;Fn 5 - force single coil, SPLat semaphore 6 --> coil 6 in slave
	nv0byte		5,SlaveAddr,#7,#7		;Fn 5 - force single coil, SPLat semaphore 7 --> coil 7 in slave

	nv0byte		1,SlaveAddr,#8,8,#80		;Fn 1 - read multiple coils, slave coils 8 --> 15 maps to SPLat byte at RAM location 10
							;reads touch screen output data

	nv0byte		16,SlaveAddr,#0,1,#20,0		;Fn 16 - write holding register, SPLat RAM 20 --> holding register 0 in slave
							;sends 8 bit analog value to touch screen

	nv0byte		128				;GOTO MMscr
	nv0ptr		MMscr
