Skip to main content
Visitor II
August 29, 2024
Solved

CCR Values Show on STM32F334R8 MCU but readCaptureValue Returns Zero

  • August 29, 2024
  • 2 replies
  • 953 views

 

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.
    Best answer by TDK

    > 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;

     

    2 replies

    Technical Moderator
    August 29, 2024

    Hello @Kannn1chtpennen 

     

    You can refer to this example in STM32CubeF3 firmware. 

    TDKAnswer
    Super User
    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;

     

    Visitor 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