Device = 18F452
Clock = 40
Config OSC = HSPLL // NEED TO REVISE
Include "USART.bas"
Include "convert.bas"
Dim
TMR1IE As PIE1.0, // TMR1 Interrupt Enable
TMR1IF As PIR1.0, // TMR1 Interrupt Flag
TMR1ON As T1CON.0, // TMR1 Count Enable
Timer1 As TMR1L.AsWord, // A quick way of creating a Word Alias
Signal1 As PORTC.0, // Define the signal pin
Signal2 As PORTC.1, // Define the second signal pin
//add second signal pin
Overflow As Byte, // Capture overflowsA
Time As LongWord // A variable for calculating total time
Const
TMR1StartVal = 8, // User defined TMR1 starting value
TMR1ReloadVal = 11, // The TMR1 reload value
nSperIncrement = 100 // Number of nS per TMR1 increment
Sub TMR1_Initialize()
TMR1ON = 0 // Disable TMR1
TMR1IE = 0 // Disable TMR1 Interrupts
TMR1IF = 0 // Clear the TMR1 Interrupt Flag
T1CON.1 = 0 // 1 = External clock from pin RC0/T1OSO/T1CKI (on the rising edge)
// 0 = Internal clock (FOSC/4)
'TRISC.0 = 1 // If External clock, then set clock as an input
'T1CON.2 = 1 // 1 = Do not synchronize external clock input
// 0 = Synchronize external clock input
// When T1CON.1 = 0;
// this bit is ignored.
T1CON.4 = 0 // 11 = 1:8 prescale value
T1CON.5 = 0 // 10 = 1:4 prescale value
// 01 = 1:2 prescale value...
// 00 = 1:1 prescale value
Timer1 = TMR1StartVal // Fill the Timer register with a starting value
End Sub
// Start Of Main Program...
SetBaudrate(br38400)
Overflow = 0 // Clear overflows
TMR1_Initialize // Setup TMR1
Input(Signal1) // Make Signal1 an input
Input(Signal2) // Make Signal2 an input
//input signal2---second sensor
While True
While Signal1 = 0 // Wait for a fresh cycle
Wend //
While Signal1 = 1 // CHECKS FOR SIGNAL 1
Wend //
TMR1ON = 1 // Begin clocking time
Repeat
If TMR1IF = 1 Then // Check for TMR1 overflow
TMR1ON = 0 // Disable TMR1
TMR1IF = 0 // Clear the TMR1 Interrupt
Timer1 = TMR1ReloadVal // Reload a new start value (includes non-counted cycles while disabled)
TMR1ON = 1 // Enable TMR1
Inc(Overflow) // Increment the overflow capture variable
EndIf
Until Signal2 = 1 // Repeat until the signal2 pin goes high
// change to until signal2 = 1
TMR1ON = 0 // Stop clocking time
// Calculate the total time in nS & display via USART
Time = (Timer1 + (Overflow * 65535)) * nSperIncrement
USART.Write(Convert.DecToStr(Time)," nS",13,10)
// Reset variables
Overflow = 0
Timer1 = TMR1StartVal
Wend