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);
}
}
/*********************************************************************************************************************/