cancel
Showing results for 
Search instead for 
Did you mean: 

Can a Timer Capture Compare be skipped if needed?

BTrem.1
Senior II

I'm using a Timer (TIM8) in a capture compare application for measuring timing between signals. In some cases I don't need the value so I have been skipping the read of the CCRx register. I'm using a HAL command as the code below shows:

The HAL_TIM_ReadCapturedValue() simply reads the CCRx register.

I'm counting on the Timer to continue running and the next IRQ will capture a timer value that would be the same whether or not the previous IRQ occurred. Is this a valid assumption?

I realize the overflow flag will likely be set when I skip a read of the CCRx register but does this have any unforeseen consequences?

	if( htim->Instance == TIM8)
	{
		if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
		{
			// toggle ref for viewing in ST GUI
			g_nFrefSignal = !g_nFrefSignal ;
 
			// received REF edge which is 'PU', update state machine,
			switch (p_mPFD->State)
			{
				case COAST:
					p_mPFD->State = ACCEL;
					buffer1[0] = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1) ;
					(p_mPFD->pu_cnt)++;
					break;
				case DECEL:
					p_mPFD->State = COAST;
					buffer1[0] = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1) ;
					(p_mPFD->pu_cnt)++;
					break;
				case ACCEL:
					p_mPFD->State = ACCEL;
					p_mPFD->pu_cnt = 0;
					break;
				default:
					p_mPFD->State = COAST; // should never get here
			}
                        // capture Fref period for FRF command
			TIM8_Current++;
		}

Thanks,

Brian

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

The flag is cleared within HAL_TIM_IRQHandler, so you don't need to read the value in order to clear the interrupt flag. Not reading the value shouldn't cause any issues.

Note this assumes you're using the HAL generated interrupt code. If you're doing your own thing, you need to clear the flag or read the counter in order to clear the flag, otherwise the interrupt will fire continuously.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

2 REPLIES 2
TDK
Guru

The flag is cleared within HAL_TIM_IRQHandler, so you don't need to read the value in order to clear the interrupt flag. Not reading the value shouldn't cause any issues.

Note this assumes you're using the HAL generated interrupt code. If you're doing your own thing, you need to clear the flag or read the counter in order to clear the flag, otherwise the interrupt will fire continuously.

If you feel a post has answered your question, please click "Accept as Solution".

Thanks TDK,

My code Interrupt handler calls  HAL_TIM_IRQHandler(&htim8), and the code snippet I showed above is in the HAL_TIM_IC_CaptureCallback callback function.

Regards,

Brian