I'm working on a project measuring high pulse and then during the low pulse, send the value by USB to a VB app.
The problem is that when the period (High and Low pulse) is shorter than aprox 7ms I get strange data from the pic.

I decided to measure how long time a USBOut and USBSERVICE takes and here is what I got:

USBSERVICE = 4.08us
USBOut 8 Byte = 12.4us
USBOut 16 Byte = 15.7us
USBOut 32 Byte = 22.4us
USBOut 64 Byte = 35.7us

So the USB transfer should not affect my measurements of the pulse width.
I use Timer3 to measure parts of my program.
The part of code I measure in the pic code below takes 15.5us.

I use a Basic Stamp to create a squarewave to my pic CCP1 input.
The program looks like this:
Code:
Main:
  IF IN15 THEN
  HIGH 6
  PAUSE 3
  LOW 6
  PAUSE 4
  ENDIF
  GOTO Main
  END
In this case I get correct readings sent to the pc.
But if I change Pause 4 to Pause 3 the data variates a lot.
Also the Timer3 values in the pic code below variates a lot.
Wich means that that part of code takes different times to execute from time to time.

If I change the Basic Stamp program to this; the data is correct:
Code:
Main:
  IF IN15 THEN
  HIGH 6
  PAUSE 0
  LOW 6
  PAUSE 7
  ENDIF
  GOTO Main
  END
And also this works:
Code:
Main:
  IF IN15 THEN
  HIGH 6
  PAUSE 7
  LOW 6
  PAUSE 0
  ENDIF
  GOTO Main
  END
But as soon the period is under 7ms I get strange values to my vb app.

Please help me find what is wrong with my code.
I use a Pic18F4550 20Mhz, MicroCode Studio Plus and Pbp 2.46.

Thanks in advance,
Stefan

Code:
   
'********************************************
'Pulse width measuring
'********************************************
DEFINE OSC 48          
DEFINE LOADER_USED 1
DEFINE RESET_ORG 800h         ' For Microchip USB Bootloader
DEFINE INTERRUPT_ORG 808h   ' For Microchip USB Bootloader

USBBufferSizeMax   con 8        ' maximum buffer size
USBBufferSizeTX    con 8         ' input 
USBBufferSizeRX    con 8         ' output
USBBuffer       Var Byte[USBBufferSizeMax] 
USBBufferCount  Var Byte 

Counts1          Var Word       ' Holds Timer1 counts
Counts1Of       Var Byte       ' Holds the Overflow counts
Overflow         var byte
RisingEdge       Var Bit
Preset            Var Bit              
SendBytes       Var Bit
SampleNo        var byte
NewSample      var byte
CCP1IE            var PIE1.2 
CCP1IF           VAR PIR1.2
TMR1ON         VAR T1CON.0
TRM1IF           var PIR1.0
TMR1IE           VAR PIE1.0
IrOnRising        var BYTE
IrOnFalling       var BYTE
x                    var byte
y                    var byte
Led                VAR PortB.5

'Timer3 declarations
TMR3ON          var T3CON.0
TMR3IF            var PIR2.1
TMR3IE            var PIE2.1
TMR3VALUE      var WORD
TMR3OverF       var byte


USBINIT
    ADCON1 = 15     ' Set PORTA, PORTE and PORTB to digital.
    TRISD.1 = 0       'Port D Bit 1 as Output
    TRISB.5 = 0       'Port B Bit6 as Output for LED
    TRISC.2 = 1       'Port C Bit2 CCP1 as Input
    IrOnFalling = 4    '0100 = Capture mode: every falling edge
    IrOnRising = 5     '0101 = Capture mode: every rising edge
    T1CON.4 = 1      'Timer1 Prescaler Bit4
    T1CON.5 = 1      'Timer1 Prescaler Bit5
    Counts1 = 0
    TMR1H = 0
    TMR1L = 0
    CCP1CON = IrOnRising     '0101 = Capture mode: every rising edge
    CCP1IE = 1                   'CCP1IE Interrupt Disabled
    SampleNo = 1
    Counts1Of = 0
    Overflow = 0
    Low Led

'Timer3 inits
    TMR3OverF = 0
    TMR3IE = 1
    TMR3IF = 0    
    T3CON.4 = 0
    T3CON.5 = 0

    
ProgramStart:
   USBSERVICE                               ' keep connection alive

        if TRM1IF THEN                      'If Timer1 Overflows
            Overflow = Overflow + 1      'Incr Overflow counter
            TRM1IF = 0                       'Reset Flag
        ENDIF 

    IF CCP1IF = 0 THEN ProgramStart     'Return if no Interrupt

        IF CCP1CON = IrOnRising THEN
            CCP1IF = 0                       'Reset Flag
            CCP1IE = 0                       'CCP1IE Interrupt Disabled
            CCP1CON = IrOnFalling        'Capture mode: every falling edge
            CCP1IE = 1                       'CCP1IE Interrupt Enabled
            TMR1H = 0                   
            TMR1L = 0
            TMR1IE = 1                       'Enable overflow flag
            TMR1ON = 1                      'Start Timer 1
'            high Led
            goto ProgramStart
        ENDIF

        IF CCP1CON = IrOnFalling THEN
            TMR3ON = 1                        'Using Timer3 for measuring
            CCP1IF = 0                          'Reset Flag
            Counts1.HighByte = CCPR1H   'Save Values
            Counts1.LowByte = CCPR1L
            Counts1Of = Overflow        
            Overflow = 0                       'Reset Overflow counter
            CCP1IE = 0                         'CCP1IE Interrupt Disabled
            CCP1CON = IrOnRising           'Capture mode: every rising edge
            CCP1IE = 1                         'CCP1IE Interrupt Disabled
            'TMR1ON = 0                       'Stop Timer 1
            TMR1H = 0
            TMR1L = 0
            if SampleNo = 1 then            'SampleNo used by VB app.
               SampleNo = 2
            else
               SampleNo = 1
            endif
            USBBuffer(0) = Counts1.LowByte
            Counts1.LowByte = 0
            USBBuffer(1) = Counts1.HighByte
            Counts1.HighByte = 0
            USBBuffer(2) = Counts1Of
            USBBuffer(3) = SampleNo
            USBBuffer(4) = TMR3VALUE.highbyte
            USBBuffer(5) = TMR3VALUE.lowbyte
            'low Led
            Gosub DoUSBOut
            TMR3ON = 0                      'Using Timer3 for measuring                
            TMR3VALUE.highbyte = TMR3H
            TMR3VALUE.lowbyte = TMR3L
            TMR3H = 0 
            TMR3L = 0
            goto ProgramStart
        ENDIF
        
        
GOTO ProgramStart

' ************************************************************
' * receive data from the USB bus                            *
' ************************************************************
DoUSBIn:
   USBBufferCount = USBBufferSizeRX              ' RX buffer size
    x=x+1
    if x > y THEN Skipping
   USBService                                    ' keep connection alive
   USBIn 1, USBBuffer, USBBufferCount, DoUSBIn   ' read data, if available
    Skipping:
    x=0
   return
  
' ************************************************************
' * wait for USB interface to attach                         *
' ************************************************************
DoUSBOut:
   USBBufferCount = USBBufferSizeTX              ' TX buffer size
   USBService                                    ' keep connection alive
   USBOut 1, USBBuffer, USBBufferCount, DoUSBOut ' if bus available, transmit data
   return
     
 End