AnsweredAssumed Answered

Frequency n Period Calculation Of an Input Signal

Question asked by Guest on Mar 18, 2010
Latest reply on Mar 27, 2010 by Guest

Hello there,

              

I have written  code to measure the parameters (frequency, period, duty cycle)of an input signal fed from a function generator, using interrupts. I have used the Enhanced Capture timer for the input capture and PLL. I have run the program on HCS12X demokit  SK-S12XDP512A but the frequency and period calculation is going wrong... also, i have declared period as an unsigned long int and frequency as a float type...I have attatched my program. do verify and clarify my doubt.

 

 

#include <hidef.h>      /* common defines and macros */
#include <mc9s12xdp512.h>
#pragma LINK_INFO DERIVATIVE "mc9s12xdp512"

void init_PLL(void);
void init_ECT(void);
void period_frequency(void);

 

unsigned int r1;
unsigned int r2;
unsigned int r3;
unsigned int count;
long int period;
float    frequency;
unsigned long int freq;

 

void main(void)

{
 init_PLL();           

 init_ECT();          

 EnableInterrupts;
 for(;:smileywink:
 {
 }
}

 

void init_PLL(void)
{
 CLKSEL       &= 0x7F;   // Deselect PLL Clock so can change SYNR, REFDV, PLLCTL
 PLLCTL       &= 0xBF;   // Turn off PLL so can change freq
 SYNR          =    4;
 REFDV         =    0;
 PLLCTL        = 0xD1;   // 1101 0001 ; Enable auto tracking mode, turn on pll, clock monitor, self clock mode      
 CLKSEL_PLLSEL =    1;
}


void init_ECT(void)
{
 ICSYS     = 0x00;       // no input sharing, normal operation
 TSCR2     = 0x81;       // timer overflow interrupt enable, prescaler factor 2
 TIOS      = 0x00;       // set the channel to input capture
 TCTL4     = 0x10;       // capture rising edges
 TFLG1     = 0xFF;       // clear flags
 TIE       = 0x04;       // enable the timer interrupts
 TSCR1_TEN = 1;          // timer on
}

 

#pragma CODE_SEG __NEAR_SEG NON_BANKED
interrupt 10 void TIMCH2_ISR(void)
{
 TFLG1      =  0x04;                     //clear the flag
 r1         =   TC2;                    
 r2         = count;
 period     = (r1+(r2*0xFFFF)-r3)*0.1;  // calculates the period
 r3         = r2;
 count      = 0;                        
 frequency  = 1.0/period;               // frequency value in floating point
 freq       = (int) frequency;          //frequency value in integer
}
 

#pragma CODE_SEG __NEAR_SEG NON_BANKED       
interrupt 16 void TIMOVF_ISR(void)

 TFLG2_TOF = 1;           // clear the overflow flag
 count++;                 //increment the counter
}

 #pragma CODE_SEG DEFAULT

Outcomes