Advertisement
DaelonSuzuka

Untitled

Jun 4th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.98 KB | None | 0 0
  1. uint16_t get_period(void)
  2. {
  3.     uint8_t go;
  4.     uint8_t timeout;
  5.     uint16_t retval;
  6.  
  7.     // Note: with 32 Mhz clock and /8 prescaler, the frequency
  8.     // of the measured signal can be computed as such:
  9.     // Freq (in MHz) = 16384 / value returned by get_period.
  10.     //
  11.  
  12.     // For other clock freqs, add proper setup code
  13.  
  14.     timer1_stop();
  15.     timer1_clear();
  16.     timer1_start();
  17.    
  18.     PIR1bits.TMR1IF = 0;
  19.     PIE1 = 0x00;
  20.     PIE2 = 0x00;
  21.  
  22.     // Fix: DON'T just set INTCON=0; this prematurely clears TMR0IF, which is needed
  23.     // for correct blinkage timing. n3wdz 11-17-09
  24.     INTCONbits.PEIE = 0;
  25.     INTCONbits.GIE = 0; // No ints required!
  26.  
  27.     go = 1;
  28.     timeout = 0;
  29.  
  30.     // Re-wrote all comparisons to be == or != 0, to generate faster code
  31.     // Resulted in accuracy now of +/- 1 clock tick.
  32.  
  33.     while (go != 0)
  34.     {
  35.         if (FREQ_PIN != 0)
  36.         {           // Wait for high on freq pin
  37.             go = 0;
  38.         }
  39.         if (PIR1bits.TMR1IF != 0)
  40.         {       // Check for timer OVFL
  41.             return (0xffff);            // Return FAIL
  42.         }
  43.     }
  44.    
  45.     timer1_stop();
  46.     timer1_clear();
  47.     go = 1;
  48.     timer1_start();
  49.    
  50.     while (go != 0)
  51.     {
  52.         if (FREQ_PIN == 0)
  53.         {           // Wait for low on freq pin
  54.             go = 0;
  55.         }
  56.         if (PIR1bits.TMR1IF != 0)
  57.         {
  58.             return (0xffff);            // Return FAIL
  59.         }
  60.     }
  61.    
  62.     timer1_stop();
  63.     timer1_clear();
  64.     go = 1;
  65.     timer1_start();
  66.    
  67.     while (go != 0)
  68.     {
  69.         if (FREQ_PIN != 0)
  70.         {   // Wait for high on freq pin
  71.             timer1_stop();
  72.             go = 0;
  73.         }
  74.         if (PIR1bits.TMR1IF != 0)
  75.         {
  76.             return (0xffff);            // Return FAIL
  77.         }
  78.     }
  79.    
  80.     timer1_stop();
  81.     retval = (TMR1H << 8) | TMR1L;          // Get final timer val
  82.     RFPeriod = retval;                  // NOTE: If you call get_stable_period, set RFPeriod equal to its result when done
  83.     return (retval);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement