'AXE110 PICAXE-18X - New Datalogger Mission Program
'Automatically generated by Wizard

'LED will flash green as readings are taken.

'Data can be retrieved after mission (LED red) by Datalink tool (F9).
'Use Datalink options: Baud rate - 4800, Sensors - 2, Send G - Enabled

' *******************
' ***** Options *****
' *******************

'Title - PICAXE VSM
'Date  - 04/11/2007
'Time  - 17:38:43

'Options Selected
'Sensors:
'Sensor 0 - Light
'Sensor 1 - Not used
'Sensor 2 - Not used
'Sensor 7 - Temperature

'Memory:
'No of readings = 10
'1 x 24LC16B

'Outputs:
'Bi-colour LED
'SPE002 Piezo sounder
'AXE033 Serial LCD

'Logging Period:
'Pause command
'Hours:  0 Mins:  0 Secs:  5 

' ********************
' ***** Symbols  *****
' ********************

'Symbol definitions
symbol data0 = b0
symbol data1 = b1
symbol data2 = b2
symbol data7 = b3
symbol top_address = w2 '(b4 + b5)
symbol address = w3     '(b6 + b7)
symbol temp_word = w4   '(b8 + b9)
symbol temp_byte = b10
symbol hours = b11
symbol mins = b12
symbol secs = b13
symbol day = b0
symbol month = b1
symbol COM = 44         'comma
symbol RET = 13         'carriage return
symbol LFEED = 10          'line feed

'Preload sensor names and title into data memory
EEPROM 0,(0,0)
EEPROM 16,("Light")
EEPROM 32,("Sensor 1")
EEPROM 48,("Sensor 2")
EEPROM 64,("Temperature")
EEPROM 80,("PICAXE VSM")


' ***************************
' ***** Initialisation  *****
' ***************************



init:
	pause 200
	serout 6,N2400,(254,1) 'clear LCD
	pause 30

	let top_address = 5
	
	high 5 ' write protect eeprom
'reload the last address from data memory

	;read 0,b6
	;read 1,b7


' *********************************
' ***** Main loop - read data *****
' *********************************

'Now read each sensor
main:
	high 3 ' flash LED

	;serout 7,N4800,("Saving at address " ,#b6,RET,LFEED)
	pause 10

	readadc 0,data0
	readtemp 7,data7

	low 3 ' end of flash LED

	
	sound 0,(50,50) ' make a piezo beep


' *********************************
' ***** Display values on LCD *****
' *********************************

	serout 6,t2400,(254,128,"Light=",#data0,"   ")
	serout 6,t2400,(254,192,"Temp.=",#data7,"   ")
	'serout 7,N4800,("Saving data at address ",RET,LFEED)

' *****************************
' ***** Now Save the Data *****
' *****************************

'Now save the data
save_data:
	low 5 ' write enable eeprom

'Single 24LC16B. Input0 in block 0,4, input1 in block 1,5
'Single 24LC16B. Input2 in block 2,6, input7 in block 3,7

	if address > 511 then memory_full
	if address > 255 then save_data_high
	i2cslave %10100000, i2cslow, i2cbyte
	writei2c address,(data0)
	pause 10
	readi2c address,(temp_byte)
	if temp_byte <> data0 then ee_error

	i2cslave %10100110, i2cslow, i2cbyte
	writei2c address,(data7)
	pause 10
	readi2c address,(temp_byte)
	if temp_byte <> data7 then ee_error

	 goto inc_address:

save_data_high:
	temp_word = address - 256
	i2cslave %10101000, i2cslow, i2cbyte
	writei2c temp_word,(data0)
	pause 10
	readi2c temp_word,(temp_byte)
	if temp_byte <> data0 then ee_error

	i2cslave %10101110, i2cslow, i2cbyte
	writei2c temp_word,(data7)
	pause 10
	readi2c temp_word,(temp_byte)
	if temp_byte <> data7 then ee_error

'increment address and save in data memory
inc_address:
	pause 1
	'high 5 ' write protect eeprom

	let address = address + 1
	write 0,b6
	write 1,b7
	if address > top_address then memory_full


' ***********************
' ***** Time Delays *****
' ***********************

'Now do time delay
'Secs:
	pause 2000

'now do next reading
	goto main


' *************************
' ***** Error Routine *****
' *************************

'Memory error - flash LED red/green'
ee_error:
	high 2
	low 3
	pause 500
	low 2
	high 3
	pause 500
	goto ee_error

' ***********************
' ***** Memory Full *****
' ***********************

'Memory is full - LED red'
memory_full:
	high 2

' ****************************
' ***** Datalink Routine *****
' ****************************

'Datalink routine to transmit data
read_init:
	;high 5 ' write protect eeprom
	let address = 0

'Get top address from data memory (w2=b4+b5)

	'read 0,b4
	'read 1,b5

	serout 7,N4800,("Click here then press any key",RET,LFEED)
	pause 10

'Wait for G character sent from software
	serin 6, N4800, address
	let address = 0

'Transmit the titles
	serout 7,N4800,("Address",COM,"Light",COM,"Temperature",COM,"PICAXE VSM",RET,LFEED)
	pause 10

'Now read the data
read_data:
'Single 24LC16B. Input0 in block 0,4, input1 in block 1,5
'Single 24LC16B. Input2 in block 2,6, input7 in block 3,7

	if address > 511 then all_done
	if address > 255 then read_data_high
	i2cslave %10100000, i2cslow, i2cbyte
	readi2c address,(data0)

	i2cslave %10100110, i2cslow, i2cbyte
	readi2c address,(data7)

	 goto tx_data:

read_data_high:
	temp_word = address - 256
	i2cslave %10101000, i2cslow, i2cbyte
	readi2c temp_word,(data0)

	i2cslave %10101110, i2cslow, i2cbyte
	readi2c temp_word,(data7)

'transmit data 
tx_data:
	serout 7,N4800,(#address,COM,#data0,COM,#data7,RET,LFEED)
	pause 10
	
	let address = address + 1
	if address = top_address then all_done
	goto read_data

'finished so send RET
all_done:

	serout 7,N4800,("Upload complete",RET,LFEED)
	pause 10
	goto read_init



