Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unsigned int get_period(void)
- {
- unsigned char go,timeout;
- unsigned int retval;
- // Note: with 32 Mhz clock and /8 prescaler, the frequency
- // of the measured signal can be computed as such:
- // Freq (in MHz) = 16384 / value returned by get_period.
- //
- // For other clock freqs, add proper setup code
- #ifdef CLOCK_32_MHZ
- T1CON = 0x30; // OSC/8 prescaler, turn timer off
- TMR1H = 0x00; // Zero the timer.
- TMR1L = 0x00;
- T1CON = 0x31; // start the timer (for detecting timeout)
- #endif
- PIR1bits.TMR1IF =0;
- PIE1 = 0x00;
- PIE2 = 0x00;
- // Fix: DON'T just set INTCON=0; this prematurely clears TMR0IF, which is needed
- // for correct blinkage timing. n3wdz 11-17-09
- INTCONbits.PEIE=0;
- INTCONbits.GIE=0; // No ints required!
- go=1;
- timeout=0;
- // Re-wrote all comparisons to be == or != 0, to generate faster code
- // Resulted in accuracy now of +/- 1 clock tick.
- while (go!=0) {
- if (FREQ_PIN != 0) { // Wait for high on freq pin
- go = 0;
- }
- if (PIR1bits.TMR1IF != 0) { // Check for timer OVFL
- return (0xffff); // Return FAIL
- }
- }
- T1CONbits.TMR1ON = 0; // Turn off timer
- TMR1H=0x00;
- TMR1L=0x00; // Clear timer
- go=1;
- T1CONbits.TMR1ON = 1; // Turn on timer
- while (go!=0) {
- if (FREQ_PIN == 0) { // Wait for low on freq pin
- go = 0;
- }
- if (PIR1bits.TMR1IF != 0) {
- return (0xffff); // Return FAIL
- }
- }
- T1CONbits.TMR1ON = 0; // Turn off timer
- TMR1H=0x00;
- TMR1L=0x00; // Clear timer
- go=1;
- T1CONbits.TMR1ON = 1; // Turn on timer
- while (go!=0) {
- if (FREQ_PIN != 0) { // Wait for high on freq pin
- T1CONbits.TMR1ON = 0; // Timer1 off
- go = 0;
- }
- if (PIR1bits.TMR1IF != 0) {
- return (0xffff); // Return FAIL
- }
- }
- T1CONbits.TMR1ON = 0; // Timer1 off
- retval=(TMR1H<<8) | TMR1L; // Get final timer val
- RFPeriod = retval; // NOTE: If you call get_stable_period, set RFPeriod equal to its result when done
- return (retval);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement