Hello,
I'm trying to use the following setup:
Edge Trigger through event system to timer to measure frequency and then output to another timer output to recreate the signal input frequency divided by 4.
EIC->TC or TCC -> Divide Input Frequency by 4 -> Output divided frequency to another TC or TCC
I have the first portion somewhat working with a Atmel Start setup. The interrupt for TC4 is below. I can read into an array the values, but they seem to vary somewhat. By 1 or 2 every other read of the CC0 register. I never get a very constant value while inputting a 1kHz square wave into the micro. At some slightly higher frequencies(5kHz) I'll start to get values back that don't make sense like 5-100 from the CC0 register.
I am then outputting TC4 period to the TC0 CC0 register with an output pin enabled on the TC0 W0. I have that connected to a scope and can see the "bad" values on the scope that I am reading from the input TC4 Handler(These are the 5-100 counts I mentioned above).
So 2 things I'm looking to optimize/understand:
1.) What can I do to optimize the accuracy and precision of the period value that I'm reading from TC4 CC0 register.
2.) What are my limitations on frequency response with a sweep on the input. I'm looking to vary the input frequency every 40 micro seconds and output that exact input frequency divided by 4.
3.) Is there an example project for something similar to this. I have found a few projects that measure frequency, but not something that outputs that measured frequency.
void TC4_Handler(void){ //Read frequency
static uint32_t valueOfPeriod[500];
static uint32_t valueOfPeriodTest;
static uint32_t previousValueOfPeriod;
static uint16_t iterate = 0;
if ((TC4->COUNT32.INTFLAG.bit.MC0 == 1) && (TC4->COUNT32.INTFLAG.bit.ERR== 0) && (TC4->COUNT32.INTFLAG.bit.OVF== 0))
{
valueOfPeriod[iterate] = hri_tccount32_read_CC_reg(TC4, 0);
valueOfPeriodTest = valueOfPeriod[iterate];
if((valueOfPeriodTest<previousValueOfPeriod-1) || (valueOfPeriodTest>previousValueOfPeriod+1))
{
gpio_set_pin_level(LED_EN,0);
}
iterate++;
if(iterate>500)
{
iterate = 0;
}
PWM_1_WritePeriod(valueOfPeriodTest*4); //This sets the CC register for TC0 to valueOfPeriod/2
}
TC4->COUNT16.INTFLAG.bit.MC0 |= 0x01;
TC4->COUNT16.INTFLAG.bit.MC1 |= 0x01;
TC4->COUNT16.INTFLAG.bit.ERR |= 0x01;
TC4->COUNT16.INTFLAG.bit.OVF |= 0x01;
}
Any assistance would be appreciated. I can provide the initialization information of the timers, event system, or edge trigger if needed.