我正在尝试用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);
}
为什么第一种情况(通过引用)不正确?
1条答案
按热度按时间btqmn9zl1#
C语言中没有引用传递,所以C编译器不知道在参数传递上下文中如何处理
&
。您需要使用*
传递一个指针并取消引用以获得相同的效果。详细说明评论:你可以用这样的方式调用这个函数