Advertisement
DaelonSuzuka

Untitled

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