I have a data logger built round a 18M2, which I have been trying to enhance by adding some code to allow me to set a start date and time via a serial connection.
I have developed the code which works as expected in the Program Editor, but when I blasted it to a chip it returns incorrect values. To try to locate the problem I have stripped the code back to the essential parts that set the values to be tested, and found that the problem is that these are scrambled on a real device, unlike the simulated program.
I must be doing something wrong, but I can not see what.
My test rig is a AXE091 Development Board, with a 18M2 inserted, a RTC and battery (time set correctly), Leg 16 is held high, and leg 18 is pulled high via a 10K, but taken low to trigger the Start Time routine.
The curent D&T is read from the RTC every second or so and sent to the terminal as part of the debugging, this works great. The Start time loaded is compared with this and once the current values exceed the values set the logging starts. I have removed this from the code while debugging.
The code follows, I would be grateful if someone could tell me what I am doing wrong.
Regards
Dave P
I have developed the code which works as expected in the Program Editor, but when I blasted it to a chip it returns incorrect values. To try to locate the problem I have stripped the code back to the essential parts that set the values to be tested, and found that the problem is that these are scrambled on a real device, unlike the simulated program.
I must be doing something wrong, but I can not see what.
My test rig is a AXE091 Development Board, with a 18M2 inserted, a RTC and battery (time set correctly), Leg 16 is held high, and leg 18 is pulled high via a 10K, but taken low to trigger the Start Time routine.
The curent D&T is read from the RTC every second or so and sent to the terminal as part of the debugging, this works great. The Start time loaded is compared with this and once the current values exceed the values set the logging starts. I have removed this from the code while debugging.
The code follows, I would be grateful if someone could tell me what I am doing wrong.
Regards
Dave P
Code:
' Data Capture Unit
' Uses the Programming port for Terminal Access and to set log start time.
' Uses PicAxe 18M2
;
#picaxe 18m2
symbol Mem_i2c_addr = $A0 ' Address of the i2c memory
symbol RTC = $D0 ' Address of RTC
symbol Day = b0 ' Stores current data from the RTC and used to send ASCII back to Terminal
symbol Date = b1 ' Stores current data from the RTC and used to send ASCII back to Terminal
symbol Month = b2 ' Stores current data from the RTC and used to send ASCII back to Terminal
symbol Year = b3 ' Stores current data from the RTC and used to send ASCII back to Terminal
symbol Hours = b4 ' Stores current data from the RTC and used to send ASCII back to Terminal
symbol Mins = b5 ' Stores current data from the RTC and used to send ASCII back to Terminal
symbol Secs = b6 ' Stores current data from the RTC and used to send ASCII back to Terminal
symbol control = b7 ' Stores current data from the RTC and used to send ASCII back to Terminal
symbol temp = b8 ' Temporary store
symbol result = b9 ' Temporary store
symbol value = b10 ' Temporary store
symbol StartDate = b11 ' Capture start date and time
symbol StartMonth = b12 ' Capture start date and time
symbol StartYear = b13 ' Capture start date and time
symbol StartHours = b14 ' Capture start date and time
symbol StartMins = b15 ' Capture start date and time
symbol StartSecs = b16 ' Capture start date and time
symbol lsb = b17 ' Temporary store
symbol msb = b18 ' Temporary store
symbol ResultIndex = b19 ' Temporary store
symbol EventCount = w10 ' Number of readings taken
symbol EndCount = w11 ' No of readings to take
symbol SetClk = pinC.1 ' Pin 18 - Pulled up to V+, take low to set Start Time
symbol Trigger = pinC.0 ' Pin 17 - Pulled up to V+, take low to trigger a data recording
symbol Terminal = pinC.7 ' Pin 16 - Pulled up to V+, If Low send output data raw via Wireless, if high send data as ASCII (for a terminal)
symbol Wireless = pinC.2 ' Pin 18 - Serial out to wireless module
sleep 2
init:
disconnect ' Allow the programming port to be used for Terminal traffic to set the start time
Main: ' Main program starts here
if SetClk = 0 then gosub SetStartTime ' If True go to set Set Start-Time routine
i2cslave %11010000, i2cslow, i2cbyte ' Initialise the RTC
readi2c 0, (Secs,Mins,Hours,Day,Date,Month,Year,Control) 'Read the current data from the RTC
'-----------------------------
'Comparison and call to logging program goes here
'-----------------------------
if Terminal = 1 then gosub SendASCII ' Send the current date and time via Terminal
if Terminal = 0 then gosub SendRaw ' Send Current date and time via Wireless link.
sleep 1
goto Main
SendASCII: 'This routine converts the BCD values and sends it to the Terminal as ASCII
sertxd(" Date: ")
let temp = date & %00110000 / 16
sertxd(#temp)
let temp = date & %00001111
sertxd(#temp)
sertxd("/")
let temp = month & %00010000 / 16
sertxd(#temp)
let temp = month & %00001111
sertxd(#temp)
sertxd("/")
let temp = year & %11110000 / 16
sertxd(#temp)
let temp = year & %00001111
sertxd(#temp)
sertxd(" Time: ")
let temp = hours & %00110000 / 16
sertxd(#temp)
let temp = hours & %00001111
sertxd(#temp)
sertxd(":")
let temp = mins & %01110000 / 16
sertxd(#temp)
let temp = mins & %00001111
sertxd(#temp)
sertxd(":")
let temp = secs & %01110000 / 16
sertxd(#temp)
let temp = secs & %00001111
sertxd(#temp)
sertxd(13,10)
return
SetStartTime: 'Load the start variables with the data
sertxd("Enter the Logging Start Time using the prompts")
sertxd(13,10)
sertxd("Enter the Day >")
sertxd(13,10)
serrxd Value
gosub ConvertBytetoBCD
let StartDate = Result
sertxd("Enter the Month >")
sertxd(13,10)
serrxd Value
gosub ConvertBytetoBCD
let StartMonth = Result
sertxd("Enter the Year >")
sertxd(13,10)
serrxd Value
gosub ConvertBytetoBCD
let StartYear = Result
sertxd("Enter the Hours >")
sertxd(13,10)
serrxd Value
gosub ConvertBytetoBCD
let StartHours = Result
sertxd("Enter the Minutes >")
sertxd(13,10)
serrxd Value
gosub ConvertBytetoBCD
let StartMins = Result
sertxd("Enter the Seconds >")
sertxd(13,10)
serrxd Value
gosub ConvertBytetoBCD
let StartSecs = Result
'Code to enter the number of readings to take placed here..
' Load the Start Variables into the TX Variables to read back.
let Date = StartDate
let Month = StartMonth
let Year = StartYear
let Hours = StartHours
let Mins = StartMins
let Secs = StartSecs
' Read back the Start Variables
sertxd("You have stored the following start time:")
sertxd(13,10)
gosub SendASCII
WaitKeyUp:
if SetClk = 0 then WaitKeyUp ' Wait until the key is released to start
Return
ConvertBytetoBCD:
msb = 0
lsb = 0
do until Value < 10 'count the most significant bits
inc msb
Value = Value - 10
loop
lsb = Value 'least significant bits = remaining
Result = msb * 16 + lsb 'convert to bcd notation
return
SendRaw: 'Sub-Routine to send data via Wireless Module
'Code removed while testing
Return
TakeReading: 'Take Readings,store in EEPROM, then send using Terminal Flag
'Code removed while testing
Return
Last edited: