title: CCS PICC CCP settings
body: <p><i>(I'll gladly post code if someone can point out how to paste it in here without using the 4 space indentation system that doesn't work)</i></p>
  
  <p>Hello folks
  After ~9Hours racking by brains, I can't find an answer or find where my calculations are going wrong... but they are.
  I have a circuit built using a microchip 18F2550 microcontroller.
  I am using this circuit to measure the delay between 2 signals and am using the 2 CCP registers in capture mode.
  This all works and the result is sent to the PC (over USB serial) all dandy, but the results are wrong.
  I have to apply a gain of ~16000 to any results to get somewhere near the delay presented to the pins.</p>
  
  <p>I have the delay set in the line<br/>
  Timer1 is set as an internal<br/>
  Timer3 is disabled<br/>
  relevant interrupts are enabled<br/></p>
  
  <p>and the main routine runs continuously.
  When I get a rising edge detection on the CCP1 pin, the interrupt is configured to reset timer1 to zero as well as the overflow counter</p>
  
  <pre><code>    #INT_CCP1
      void ccp1_isr() // Captures the rising edge of CCP1 pin.
      { 
          if(timing==FALSE){ // only do this on the edge, any bouncing will reset timers etc.
              set_timer1(0);
              T1_Overflow = 0;
              Pulse_Time = 0;
              timing = 1;     // Set flag to indicate timing.
              output_high(BLUE_LED);
          }
      }
  </code></pre>
  
  <p>the <code>timing</code> flag ensures the times cannot be reset by another pulse on the CCP1 pin.
  Timer1 should then be reset and start counting as normal.  Every time it rolls around by 65535 (16bit device) another interrupt is fired after which the amount of overflows are incremented.</p>
  
  <pre><code>    #INT_TIMER1
      void isr()
      {
          T1_Overflow++;
      }
  </code></pre>
  
  <p>Finally, when the input pin on CCP2 goes high, the CCP_2 interrupt is triggered.  This captures the value of the CCP register (which is the value of Timer0 at the time the interrupt was fired) and the overflow register.</p>
  
  <pre><code>    #INT_CCP2
      void ccp2_isr()                 
      {   
          if(timing == TRUE){ // only output this when preceded by CCP1
              if(Count_Done == FALSE) // do this once only
              {
                  Count_Done = TRUE;  // and also flag to the main routine to output data to the terminal.
                  Pulse_time = CCP_2;
                  Pulse_Overflow = T1_Overflow;
                  measureCount++;     // increment the number of measures.
              }
              output_low(BLUE_LED);
              timing = FALSE;
          }
      }
  </code></pre>
  
  <p>CCP1 can now start responding to the inputs again.
  The idea of this is that every time I get a pulse of one input at CCP1 followed by CCP2, a string is sent to the terminal with a counter, the number of overflows and the time left in the timer.</p>
  
  <pre><code>    while(TRUE)                        // do forever while connected
      {       
         usb_task();                          // keep usb alive    
         if(Count_Done == TRUE)
         {
             printf(usb_cdc_putc, "%lu , %lu ,  %lu \r\n",measureCount, pulse_time, pulse_overflow);
             Count_Done = FALSE;
         }
  </code></pre>
  
  <p>so, I should get an output to the terminal of something like "1,61553,35" for a ~12ms delay between CCP1 and CCP2.
  The problem is that these are the results I am getting for a 200ms pulse provided to the circuit. (Verified twice)</p>
  
  <p>so where am I going wrong.
  I have a 48MHZ Clock with no prescaler which implies a cycle every 20ns.
  Divide by for 4 instructions per cycle for the clock which implies 5.2ns every cycle
  16 bit timer which implies rollover every 65535*5.2ns = 341us per rollover.
  when you do the calculations (0.000341*pulse_overflow)+pulse_time*(5.2*(10^-9))
  then the above data gives 0012.27ms and not the 200ms provided.</p>
  
  <p>Can anyone point out where I am going wrong with these calculations???</p>