CUBEide中的C++函数

dy1byipe  于 2023-04-29  发布在  其他
关注(0)|答案(1)|浏览(182)

我正在尝试用c语言为stm32板写一个函数。

float computeMotorSpeed(TIM_HandleTypeDef &htimer, uint32_t &PreviousCount ) {
    int32_t DiffCount = 0;
    uint32_t  CurrentCount = 0;
    CurrentCount = __HAL_TIM_GET_COUNTER(&htimer); /*evaluate increment of timer counter from previous count*/

    if (__HAL_TIM_IS_TIM_COUNTING_DOWN(&htimer)) {
        /* check for counter underflow */
        if (CurrentCount <= PreviousCount) {
            DiffCount = CurrentCount - PreviousCount;
        }
        else {
            DiffCount = -((TIM3_ARR_VALUE+1) - CurrentCount) - PreviousCount;
        }
    }
    else {
        /* check for counter overflow */
        if (CurrentCount >= PreviousCount) {
            DiffCount = CurrentCount - PreviousCount;
        }
        else {
            DiffCount = ((TIM3_ARR_VALUE+1) - PreviousCount) + CurrentCount;
        }
    }

    PreviousCount = CurrentCount;
    return (float) DiffCount*60/(TS*TIM3_ARR_VALUE); // rpm (round per minute);
}

但我有以下错误。
error: expected ';', ',' or ')' before '&' token
我试图通过引用传递值,但出现错误。然而,当我按如下方式传递值时,编译器不会显示错误。

float computeMotorSpeed(TIM_HandleTypeDef htimer, uint32_t PreviousCount ) {
    int32_t DiffCount = 0;
    uint32_t  CurrentCount = 0;
    CurrentCount = __HAL_TIM_GET_COUNTER(&htimer); /*evaluate increment of timer counter from previous count*/

    if (__HAL_TIM_IS_TIM_COUNTING_DOWN(&htimer)) {
        /* check for counter underflow */
        if (CurrentCount <= PreviousCount) {
            DiffCount = CurrentCount - PreviousCount;
        }
        else {
            DiffCount = -((TIM3_ARR_VALUE+1) - CurrentCount) - PreviousCount;
        }
    }
    else {
        /* check for counter overflow */
        if (CurrentCount >= PreviousCount) {
            DiffCount = CurrentCount - PreviousCount;
        }
        else {
            DiffCount = ((TIM3_ARR_VALUE+1) - PreviousCount) + CurrentCount;
        }
    }

    PreviousCount = CurrentCount;
    return (float) DiffCount*60/(TS*TIM3_ARR_VALUE); // rpm (round per minute);
}

为什么第一种情况(通过引用)不正确?

btqmn9zl

btqmn9zl1#

C语言中没有引用传递,所以C编译器不知道在参数传递上下文中如何处理&。您需要使用*传递一个指针并取消引用以获得相同的效果。详细说明评论:

// use * to denote pointers
float computeMotorSpeed(TIM_HandleTypeDef *htimer, uint32_t *PreviousCount ) {
   ...
   // don't "take the address" here anymore, the pointer is the address
   CurrentCount = __HAL_TIM_GET_COUNTER(htimer); /*evaluate increment of timer counter from previous count*/

   if (__HAL_TIM_IS_TIM_COUNTING_DOWN(htimer)) {
        /* check for counter underflow */
        // and you must dereference PreviousCount with * everywhere to access
        // its value
        if (CurrentCount <= *PreviousCount) {
            DiffCount = CurrentCount - *PreviousCount;
        }
   ...
   *PreviousCount = CurrentCount;
   return ...;
}

你可以用这样的方式调用这个函数

// here, & means "take the address of", as you used it in OP
float floatRet = computeMotorSpeed(&htimer, &PreviousCount);

相关问题