Skip to main content
Associate II
August 29, 2024
Solved

CCR Values Show on STM32F334R8 MCU but readCaptureValue Returns Zero

 

Hello everyone,

I'm working on a project using the STM32F334R8 MCU to capture two rising edge signals with the intent of obtaining the CNT value at each signal's rising edge. My calculations and data are meant to be processed through an external interrupt and transmitted to a computer. I am using a 72 MHz clock with Timer 2, specifically channel 1 and 4.

In my debug testing, everything initializes correctly—both input capture and external interrupts start properly, and both CCR1 and CCR4 registers display values. However, when I attempt to retrieve these values using readcapture, the function consistently returns zero. I have also tried accessing the register values directly without using the API, but the results remain the same.

Here are some key points:

  • MCU: STM32F334R8
  • Timer Clock: 72 MHz
  • Timer used: Timer 2 (Channel 1 and 4)
  • Issue: CCR displays values, but HAL_TIM_ReadCaptureValue() returns zero.

Has anyone encountered a similar issue or can provide insights on why this discrepancy occurs and how to fix it? Any advice or suggestions would be greatly appreciated.

Thank you in advance for your help!

 

My code and debug screenshots are as follows.

  uint32_t captureValue1; 
  uint32_t captureValue4; 
  uint16_t a; 
  uint16_t b; 
  uint16_t c; 
  uint32_t diff;
 
 
 void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
 {
if(htim->Channel==HAL_TIM_ACTIVE_CHANNEL_1) // Ref Signal
{
a++;
//uint32_t captureValue1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1); 
uint32_t captureValue1 = htim->Instance->CCR1;
 
}
else if(htim->Channel==HAL_TIM_ACTIVE_CHANNEL_4)
{
b++;
//uint32_t captureValue4 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_4); 
uint32_t captureValue4 = htim->Instance->CCR4;
}
 
 }
 
 void HAL_GPIO_EXTI_Callback(uint16_t GPIO_PIN)
 {
if(GPIO_PIN==GPIO_PIN_6)
{
c++;
uint32_t diff=captureValue4-captureValue1; 
char buffer[10];
int len= sprintf(buffer,"%u\r\n", diff); 
  HAL_UART_Transmit(&huart2, (uint8_t*)buffer, len, HAL_MAX_DELAY); 
}
 }

foto.jpg

This topic has been closed for replies.
TDK
TDKBest answer
August 29, 2024

> uint32_t captureValue1 = htim->Instance->CCR1;

This creates a new local variable called "captureValue1", which immediately goes out of scope. You probably want to modify the global variable, which is done like this:

captureValue1 = htim->Instance->CCR1;

 

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

3 replies

ST Technical Moderator
August 29, 2024

Hello @Kannn1chtpennen 

 

You can refer to this example in STM32CubeF3 firmware. 

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Saket_Om
TDK
TDKBest answer
August 29, 2024

> uint32_t captureValue1 = htim->Instance->CCR1;

This creates a new local variable called "captureValue1", which immediately goes out of scope. You probably want to modify the global variable, which is done like this:

captureValue1 = htim->Instance->CCR1;

 

"If you feel a post has answered your question, please click ""Accept as Solution""."
Associate II
August 29, 2024

Thank you TDK,

 

Just as you mentioned, I forgot to set my variable as a global variable. Thank you for your reply