Hi,
I'm not sure this will give enough performance to guarantee a granularity of 5us but it MIGHT. It's not tested but it does compile, perhaps you can use it as a starting point.
Code:
T1CON = %00000000 'TMR1 clocked by FOsc/4, prescaler 1:1 = 125ns per tick
@Timer1 = TMR1L ' Create alias to TMR1 in order to access it as a WORD
Timer1 VAR WORD EXT ' Let PBP know of its existance
TMR1IF VAR PIR1.0 ' Alias to TMR1 Interrupt flag, set on overlfow
Pulse1 VAR WORD ' Holding variables for pulsewidths
Pulse2 VAR WORD
Pulse3 VAR WORD
Pulse4 VAR WORD
PulseCount VAR BYTE ' Keep track of how many pulses we've grabbed.
'--------------------------------------------------------------------------
Setup:
Pulse1 = 0 ' Clear all pulsewidhts
Pulse2 = 0
Pulse3 = 0
Pulse4 = 0
PulseCount = 0 ' Clear pulsecounter
T1CON.0 = 0 ' Timer 1 OFF
Timer1 = 0 ' Clear TMR1
TMR1IF = 0 ' Clear interrupt flag
'--------------------------------------------------------------------------
WaitForLeadingEdge:
While !PortB.0 : WEND ' Wait for rising edge on PortB.0
T1CON.0 = 1 ' Start TMR1
'--------------------------------------------------------------------------
WaitForTrailingEdge:
IF !PortB.0 THEN ' First input is low
Pulse1 = Timer1 ' Grab timer value
PulseCount = PulseCount + 1
ENDIF
IF !PortB.1 THEN ' Second input is low
Pulse2 = Timer1 ' Grab timer value
PulseCount = PulseCount + 1
ENDIF
IF !PortB.2 THEN ' Third input is low
Pulse3 = Timer1 ' Grab timer value
PulseCount = PulseCount + 1
ENDIF
IF !PortB.3 THEN ' Fourth input is low
Pulse4 = Timer1 ' Grab timer value
PulseCount = PulseCount + 1
ENDIF
' Now check if we've got all four pulses.
IF PulseCount = 3 THEN WeAreDone
' Verify that the timer hasn't overflowed. At 125ns per tick the
' timer overflows after 8.192ms.
IF TMR1IF THEN ABORT
' Not all pulses grabbed and no overflow, continue waiting.
Goto WaitForTrailingEdge
'--------------------------------------------------------------------------
WeAreDone:
HSEROUT["Done.",13]
HSEROUT["Pulsewidth 1: ", #Pulse1,13]
HSEROUT["Pulsewidth 2: ", #Pulse2,13]
HSEROUT["Pulsewidth 3: ", #Pulse3,13]
HSEROUT["Pulsewidth 4: ", #Pulse4,13]
Goto Setup
Abort:
HSEROUT["ERROR! Timer overflow.",13]
GOTO Setup
/Henrik.
EDIT: Just saw your second post, don't know I understand the D/A approach. If you mean that you're going to convert the the pulsewidth to analog voltage and read that with the ADC I don't think it's going to help because the ADC in the PIC can only sample one input at the time anyway. (Some PICs have dual channel S/H circuits but most don't). In that case you might as well measure pulse 1 then measure pulse 2 then pulse 3 and so on.
Bookmarks