Code:
Main:
' Read current time/date
HIGH ext_pwr ' Temporarily turn on power to SRF02 for I2C bus use
PAUSE 5 ' Delay to let I2C bus stabilize after SRF02 turn on
I2CREAD SDA, SCL, RTCdevice, SecReg,[sec,MINs,hr,day,date,mon,yr]
'PAUSE 20
LOW ext_pwr ' Turn off power to the SRF02 at end of routine.
' This statement will distort the I2C bus & ext_pwr must be set
' HIGH again before any attempts are made to use the I2C bus.
' Store current date/time in EEPROM
WRITE 242, Mon
WRITE 243, Date
WRITE 244, Yr
WRITE 245, hr
WRITE 246, MINs
Write 247, sec
IF LCD_Flag = 1 Then ' This code block for testing only
'Clr LCD & display date/time
PAUSE 20
LCDOut $fe,1' Clear Display
LCDOUT $fe,Line1,Hex2 mon,"/",hex2 date,"/", hex2 yr
LCDOUT $fe,Line2,hex2 hr,":",hex2 MINs,":",hex2 sec
Pause 200
ENDIF
' BufferOut (PIC to PC) Data Structure
' BufferOut(0) = 0, must always be zero as valid Report ID
' BufferOut(1) = DeviceID1 or MSB of Device Serial Number
' BufferOut(2) = DeviceID2 or LSB of Device Serial Number
' BufferOut(3) = mon
' BufferOut(4) = date
' BufferOut(5) = yr
' BufferOut(6) = hr (Pic hours time)
' BufferOut(7) = MINs (Pic minutes time)
' BufferOut(8) = sec (Pic seconds time)
' BufferOut(9) = stamp
' BufferOut(10) = rng0
' BufferOut(11) = rng1
' BufferOut(12) = BATTLOW (Low Battery Flag)
' BufferOut(13) = n/a ..0
' BufferOut(14) = n/a ..0
' BufferOut(15) = n/a ..0
' BufferOut(15) must be zero for sending data buffers (1) thru (12)
' ...else must send a text string with Buffer(15) = 1
FOR X = 0 to 1000 ; Check for incomming USB data while waiting
@ ON_USBRX_GOSUB _HandleRX
PAUSE 1
New_PORTB2 = PORTB.2 ; Check PORTB.2 pin for change
IF New_PORTb2 != Old_PORTB2 THEN ' PortB.2 changed
Old_PORTb2 = New_PORTb2
ARRAYWRITE USBTXBuffer, ["USBDemo"]
GOSUB WaitToSend ' Send data set about one/sec
' Send device ID to PC
' Send current Date/Time to PC
' Send each EEPROM stored date stamped range measurement to PC
' Send current BATTLOW indicator to PC
ARRAYWRITE USBTXBuffer,[0,deviceID1,deviceID2,Mon,date,Yr, _
hr,MINs,sec,stamp,rng0,rng1,BATTLOW,0,0,0]
GOSUB WaitToSend
ENDIF
NEXT X
IF J >= 237 THEN ' If reached maximum EEPROM usage location...
J = 16 ' ..reset pointer to start of range data
Endif
READ j, stamp ' Read date stamp of measurment
READ J+1, rng0 ' Read range measurement from EEPROM
READ J+2, rng1
Pause 10
J = J + 3
' Send Data to PC
' Send device ID to PC
' Send current Date/Time to PC
' Send each EEPROM stored range measurement to PC
' Send current BATTLOW indicator to PC
ARRAYWRITE USBTXBuffer,[0,deviceID1,deviceID2,mon,date,yr, _
hr,MINs,sec,stamp,rng0,rng1,BATTLOW,0,0,0]
GOSUB SendIfReady
' If FMReset = 1, reset EEPROM memory index for new logging epoch
GOTO Main
Code:
Public Sub OnRead(ByVal pHandle As Long)
'
' There's some incomming data here :o)
' read it and have fun.
' Don't forget Bufferin(0) is the ReportID
'
Dim Index As Byte
Dim strSerialNumMSB As String
Dim strSerialNumLSB As String
Dim strSerialNum As String
Dim intSerialNum As Integer
Dim lngSerialNum As Long
Dim strMon As String
Dim strDay As String
Dim strYr As String
Dim strHr As String
Dim strMINs As String
Dim strSecs As String
Dim MyInt As Integer
Dim MyStr As String
Dim strTime As String
Dim intDateStamp As Integer
Dim intFillLevel As Integer
Dim intRangeMax As Integer
Dim intRange0 As Integer
Dim intRange1 As Integer
' ReadBuffer Structure:
' ' Read data from FillMonitor via USB
' BufferIn(0) = Report id => always 0
' BufferIn(1) = DeviceID1, MSB of SerialNum ' first data item, etc etc
' BufferIn(2) = DeviceID2, LSB of SerialNum ' first data item, etc etc
' BufferIn(3) = Month
' BufferIn(4) = Date
' BufferIn(5) = Year
' BufferIn(6) = Time_Hr (PicHr)
' BufferIn(7) = Time_MINs (PicMINs)
' BufferIn(8) = Time_Secs (PicSecs)
' BufferIn(9) = intDateStamp
' BufferIn(10) = RangeLSB
' BufferIn(11) = RangeMSB
' BufferIn(12) = BATTLOW flag for low battery (PORTA.5)
' BufferIn(13) = n/a = 0
' BufferIn(14) = n/a = 0
' BufferIn(15) = n/a = 0
' BufferIn(15) = must be zero for reading buffers (1) thru (12)
' else BufferIn is a text string when In(15)= 1
' When m_blnFlagRX is set and both BufferIn(0) = 0 and BufferIn(15) = 0 then,
' we display the PIC transmitted data: the device ID, the PIC clock date/time, range, & Battery state
' ....else when BufferIn(15) = 1 we receive a text string to be displayed continuously.
If hidRead(pHandle, BufferIn(0)) Then ' valid ReportID?
' first byte is the report ID, e.g. BufferIn(0)
' the other bytes are the data payload from the USB PIC
If BufferIn(15) = 0 And m_blnFlagRX = True Then ' There is incoming data set from PIC
'\\ Read & display inbound data from USB PIC
'\\ Display DeviceID as Serial Number
strSerialNumMSB = Hex$(BufferIn(2))
strSerialNumLSB = Hex$(BufferIn(1))
strSerialNum = strSerialNumMSB & strSerialNumLSB
intSerialNum = CLng("&H" & strSerialNum)
lSerialNum.Caption = intSerialNum
Bookmarks