cancel
Showing results for 
Search instead for 
Did you mean: 

Why the maximum value of Tim4Encoder is 127 rather than 32767?

SYoun.1
Associate II
int16_t GetTim4Encoder(){
    Tim4Encoder = __HAL_TIM_GET_COUNTER(&htim4);
    //__HAL_TIM_SET_COUNTER(&htim4,0);
    return Tim4Encoder;
}

0690X00000DYNJaQAP.png

13 REPLIES 13

What's the value of other TIM4 registers, mainly TIM4_ARR?

JW

Thank you for answering the question. I am a beginner of STM32 so I know little about the registers. This is SerialPrint() function.

void SerialPrint(uint32_t Tx){
	if(Tx == 0){
		TxMessages[0] = '0';
		HAL_UART_Transmit(&huart1 ,(uint8_t*)&TxMessages, 1, 0xFFFF);
	}
	else{
		tmp = abs(Tx);
		j = 0;
		length = 0;
		while(tmp != 0){
			TxMessagesTmp[j++] = tmp%10 + '0';
			tmp /= 10;
			length++;
		}
		if(Tx > 0){
			for(j = 0;j < length;j++){
				TxMessages[j] = TxMessagesTmp[length-j-1];
			}
			HAL_UART_Transmit(&huart1 ,(uint8_t*)&TxMessages, length, 0xFFFF);
		}
		else if(Tx < 0){
			TxMessages[0] = '-';
			for(j = 1;j < length + 1;j++){
				TxMessages[j] = TxMessagesTmp[length-j];
			}
			HAL_UART_Transmit(&huart1 ,(uint8_t*)&TxMessages, length + 1, 0xFFFF);
		}
	}
	HAL_UART_Transmit(&huart1 ,(uint8_t*)&Apart, 1, 0xFFFF);  //sizeof(TxMessages) has a problem
}

And this is main()

while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
	  SerialPrint(TIM4->ARR);
          HAL_UART_Transmit(&huart1 ,(uint8_t*)&Apart1, 2, 0xFFFF);
	  HAL_Delay(500);
  }

The value of ARR is pretty strange.0690X00000DYNTpQAP.png

hummm

Maybe try some other means of printout, or simplify your program to do nothing else just print TIM4->ARR?

Or, perhaps you may try to set up some in-circuit debugging, to observe the registers more directly.

JW

turboscrew
Senior III

Yep. Note, that you can't have negative number to print: SerialPrint(uint32_t Tx). Uint can't be negative.

And you might want to send 0x0D, 0x0A (='\r', '\n' = return, line feed) instead of the underscores?

(I guess the 'Apart' contains underscores.)

Maybe even print some string before the entity to tell what is what?

HAL_UART_Transmit(&huart1 ,(uint8_t*)"\r\nARR: ", 7, 0xFFFF);

SerialPrint(TIM4->ARR);

HAL_UART_Transmit(&huart1 ,(uint8_t*)"\r\n", 2, 0xFFFF);

OK, I will try it. I am just confused about the relationship between this issue and ARR. In other word, after I can observe the registers, how can I solve the problem?

turboscrew
Senior III

You could also initialize the ARR to 65535 and see if that makes the difference.

If you are using CubeMX, do it there.

Apart is '_' to separate data from each send. SerialPrint(...) used to be SerialPrint(uint16_t Tx) but I changed to uint32 to print the ARR.

Thank you, I will try it.

To have negative values, it should be int16 or int32.