Hope someone can help... I'm looking to calculate frequency of captured signal period using fixed-point arithmetic rather than my currently implemented and working Float, as I understand this would be a less resource intensive approach; I'm using the frequency counter to measure audio frequencies. I'm a beginner to PIC programming and very new to the concept of fixed-point, but would appreciate any guidance.
I'm using the PIC18F13K22 with Internal Clock at 4Mhz (other internal clock options: 16Mhz, 8Mhz, etc), in Capture mode, using Timer 3, at 1 Mhz (Fosc/4); therefore each captured count period is 1E-6 Sec. For example: an input frequency of 125Hz = 1E6 / 125 = 8,000 counts.
My code snippet below, using C and XC8 compiler:
volatile char buffer[5];
volatile uint16_t t1;
volatile uint16_t t2;
volatile uint16_t period;
volatile float freq;
void main() {
t1 = 0x0;
t2 = 0x0;
period = 0x0;
freq = 0.0;
int status;
char *text;
// Init
sys_init();
capture_init();
LCD_Initialize();
LCDPutStr("Frequency:");
LCDGoto(0,1);
LCDPutStr("0");
LCDGoto(14,1);
LCDPutStr("Hz");
while(1) {
TMR3L = 0x00;
TMR3H = 0x00;
T3CONbits.TMR3ON = 1; // Timer3 on
while(PIR1bits.CCP1IF==0); // Wait for first rising edge
PIR1bits.CCP1IF = 0;
t1 = CCPR1;
while(PIR1bits.CCP1IF==0); // Wait for second rising edge
PIR1bits.CCP1IF = 0;
t2 = CCPR1;
T3CONbits.TMR3ON = 0; // Timer3 off
if (t1 < t2) {
period = t2 - t1;
freq = 1000000 / period;
text=ftoa(freq,&status);
LCDGoto(0,1);
LCDPutStr(text);
}
}
}
Currently the variable freq
is defined as a Double
data type. Looking to take as efficient approach as possible, and hence interested in calculating and representing frequency in fixed-point; also keen to ensure as accurate as possible whilst maintaining use of internal clock and Capture method.
Thanks, Alex
period
? – the busybee1000000 / period
results in a value of typeint
. – dbush