I wrote a “_delay_PIT0_ms()” function using PIT0,
When I try to generate delay of 1 mSec using this function, Some unexpected delay gets generated.
**This unexpected delay is generated when I try to read CVAL but It gives Proper output when I read TIF flag instead of CVAL.
Why Reading CVAL is creating problem? What’s the reason behind it?
I want to read CVAL for Speed calculation of motor. So please help me through this and please explain what the problem with “CVAL” reading is?
and How to Configure Clock to PIT module? (In my case it's 64MHz by default)
/************************************************************************************************************************/
void _delay_PIT0_ms(vuint32_t d)
{ //Maximum Delay can be 67100 miliSeconds
if (d<=67100)
{
PIT.PITMCR.B.FRZ = 1; //FRZ = 1 for Timers stopped in debug mode
PIT.PITMCR.B.MDIS = 0; //MDIS = 0 to enable clock for PITs
PIT.CH[0].TCTRL.B.TEN = 0; //TEN = 0 for timer disabled.
PIT.CH[0].LDVAL.R = (d*64000) - 1; //(64000 = 1mSec)
PIT.CH[0].TCTRL.B.TEN = 1; //TEN = 1 for timer active
while((PIT.CH[0].CVAL.B.TVL) != 0){}; //Wait till d seconds (i.e. till current timer value reaches to 0) \
//Gives unexpected delay
//while(PIT.CH[0].TFLG.B.TIF != 1){}; //Works properly
PIT.CH[0].TFLG.B.TIF = 1; //Clear Timer Interrupt Flag
PIT.CH[0].TCTRL.B.TEN = 0;//TEN = 0 for timer disabled.
}
}
int32_t main(void)
{
int32_t i = 0;
InitHW();
Init_eMIOS();
Init_ADC();
Init_EIRQ();
Init_BLDC_6x2_3Ph();
Switch_PWM_DutyCycle(INITIAL_A_VAL);
//Init_Interrupts();
while(1)
{
LED1_TOGGLE;
_delay_PIT0_ms(1);
LED1_TOGGLE;
_delay_PIT0_ms(1);
}
}
/*********************************************************************************************************************/
PetrS can you please help me through this sir?
Hi,
Martin was correct, you should use the channel flag to determine the time interval is gone.
Using the CVAL value, the condition could not be always fulfilled. You can easily missed some if using
while((PIT.CH[0].CVAL.B.TVL) != 0){}; statement.
Similarly while((PIT.CH[0].CVAL.B.TVL) <= 25){}; is little bit useless, it will stays in loop for very short time.
Try to use while((PIT.CH[0].CVAL.B.TVL) > 25){}; but still using channel flag polling you get the best result.
BR, Petr
Hi,
I searched in the reference manual and there is no notice about reading CVAL. But I recommend you to read TIF flag. This is correct way, how to check that timeout was reached.
In the second case, when you check CVAL, it seems the PIT counted is too fast and the condition in while loop is still true (you miss the moment when CVAL is equal to 0) and this causes the problem with delay.
Regards,
Martin
Initially, I also thought the same that somehow due to speed mismatch While() is not getting executed properly.
Then I tried this condition also while((PIT.CH[0].CVAL.B.TVL) <= 25){};
But still it is not working. This means speed mismatch should not be the case.
I need to read CVAL value to calculate speed of the motor by reading CVAL values in different interrupts.
Here, It is mentioned that we can read the CVAL value, But I want to know what is correct way to read CVAL?