Frequency Measurement using PIC16f887

Thread Starter

The_Fleertz

Joined Apr 23, 2012
8
Hey guys,
So I'm just trying to understand the capture module on the PIC16f887. I have some code which attempts to capture an 82.4 Hz frequency (this is going to be adapted later for a guitar tuner) and if the frequency captured is within 5 cents of that value it should set portb to 82 just for visual confirmation that it is working. My input to the CCP1 pin is a square wave from 0 to 6 volts at 82.4 Hz.

All I know is that it isn't working, however, the capture module has been working for other programs I've written so that isn't the issue.

Thanks for the help!

Rich (BB code):
#define  _LEGACY_HEADERS

#include <htc.h>

__CONFIG(INTIO & WDTDIS & PWRTDIS & BORDIS & BORDIS & LVPDIS & DEBUGEN & DUNPROTECT & UNPROTECT);

#define first_time	0
volatile int capreg_new, capreg_old,tau;
volatile char time=0;

void main(){
	ANSEL=0;					//This and next line initialize PORTB to digital
	ANSELH=0;					//
	PORTB=0;
	TRISB=0b00000000;			//PORTB all outputs for lights
	TRISC=0b11111111;			//PORTC inputs for CCP
//	CCP1CON=0b00000101;	//Capture initialize
	T1CON=0x01;				//Timer on
	PEIE=1;						//Enable peripheral interrupts
	GIE=1;						//Enable Global interrupts
	CCP1IF=0;
	CCP1IE=1;					//Need this?
	
	while(1) {
		CCP1IE=1;
		CCP1CON=0b00000111;	//Capture initialize
	}	
}
//*********** Interrupt Service Routine *************//
void interrupt isr(void){
	
	if(CCP1IF==1){
		CCP1IE=0;			//Turn off interrupt so it doesn't stick in isr
		if(first_time == time){
			CCP1CON=0x00;
			capreg_old=CCPR1H;
			capreg_old=capreg_old<<8|CCPR1L;
		}
			
	//	PORTB=PORTB++;
		if(first_time != time){
			CCP1CON=0x00;
			capreg_new=CCPR1H;
			capreg_new=capreg_new<<8|CCPR1L;
			tau=capreg_new-capreg_old;
				if(tau>12000 && tau<12271)		PORTB=82;
			capreg_old=capreg_new;
		}
		time=1;
		CCP1IF=0;
	}
}
 
Top