neddie
Full Member level 5
- Joined
- Feb 23, 2010
- Messages
- 251
- Helped
- 41
- Reputation
- 82
- Reaction score
- 35
- Trophy points
- 1,308
- Location
- South Africa
- Activity points
- 3,026
Hi to all.
I'm trying to do DTMF decoding on a pic24.
I've found a project that implements a Goertzel algorithm on a pic16:
ProjectProto: Goertzel Algorithm for PIC16F , and it follows the code described here
Goertzel algorithm - Wikipedia, the free encyclopedia
, so I'm using it a base to start from.
When I run it on a pic24 , I just get garbage out(constantly changing numbers , ranging from 0 to 300000+)
. I'm currently just trying to detect 1 tone , to get things going. I'm sampeling a sine wave 2V pk-pk on AN6 of the micro.I'm using 12 bit, not 8 , but the size of the variables should handle the change.
Some of the relevant code below. I'm sure I've missed or messed up something :0(
Any help would be appreciated.
Cheers
Ned
I'm trying to do DTMF decoding on a pic24.
I've found a project that implements a Goertzel algorithm on a pic16:
ProjectProto: Goertzel Algorithm for PIC16F , and it follows the code described here
Goertzel algorithm - Wikipedia, the free encyclopedia
, so I'm using it a base to start from.
When I run it on a pic24 , I just get garbage out(constantly changing numbers , ranging from 0 to 300000+)
. I'm currently just trying to detect 1 tone , to get things going. I'm sampeling a sine wave 2V pk-pk on AN6 of the micro.I'm using 12 bit, not 8 , but the size of the variables should handle the change.
Some of the relevant code below. I'm sure I've missed or messed up something :0(
Code:
#define ISR __attribute__((__interrupt__,auto_psv))
#define SAMPLING_RATE 2003
#define MAX_BINS 6
#define GOERTZEL_N 96
#define pi 3.141592654
#define threshold 1000
#define true 1
#define false 0
const double freqs[ MAX_BINS] = { 83, 110, 147, 196, 247, 330 };
unsigned int sample_count = 0;
unsigned char samples[GOERTZEL_N];
unsigned char sample_complete = false;
double prev1[ MAX_BINS ];
double prev2[ MAX_BINS ];
double magnitude[ MAX_BINS ];
double coeffs[ MAX_BINS ] ;
void initialise(void);
void calc_coeffs();
/***************************************************************
* MAIN FUNCTION
***************************************************************/
int main(void)
{
int i;
double val;
initialise();
calc_coeffs();
while(1)
{
if(sample_complete == true)
{
for(i = 0; i < MAX_BINS; i++) {
prev2[i] = 0.0;
prev1[i] = 0.0;
}
for(sample_count=0; sample_count < GOERTZEL_N; sample_count ++)
{
//Goertzel Algorithm
for ( i = 0; i < MAX_BINS; i++ )
{
val = coeffs[i] * prev1[i] - prev2[i] + (double)samples[sample_count];
prev2[i] = prev1[i];
prev1[i] = val;
}
}
for ( i=0; i<MAX_BINS; i++ ) {
magnitude[i] = (prev1[i] * prev1[i]) + (prev2[i] * prev2[i]) - (coeffs[i] * prev1[i] * prev2[i]);
}
sample_complete = false;
sample_count = 0;
TMR5 = 0;
IFS1bits.T5IF = 0;
IEC1bits.T5IE= 1; // enable TMR0 interrupt, start storing samples
}
}
}
/***************************************************************
* TIMER 5 INTERRUPT 125us
***************************************************************/
void ISR _T5Interrupt(void)
{
IFS1bits.T5IF = 0; //Clear Flag
AD1CON1bits.SAMP = 1; //start sample
while(AD1CON1bits.DONE == 0)
{
AD1CON1bits.DONE = 0;
samples[sample_count++] = ADC1BUF0; // Read 10 -bit ADC result
if (sample_count == GOERTZEL_N)
{
IEC1bits.T5IE= 0;
sample_complete = true;
}
AD1CON1bits.SAMP = 0;
}
}
void calc_coeffs()
{
unsigned int k, n;
for(n = 0; n < MAX_BINS; n++)
{
k = (unsigned int)(0.5 + (double)GOERTZEL_N * freqs[n] / SAMPLING_RATE);
coeffs[n] = 2.0 * cos(2.0 * pi * (double)k/GOERTZEL_N);
}
}
Any help would be appreciated.
Cheers
Ned