Skip to main content
SYoun.1
Associate
March 1, 2020
Question

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

  • March 1, 2020
  • 4 replies
  • 2070 views
int16_t GetTim4Encoder(){
 Tim4Encoder = __HAL_TIM_GET_COUNTER(&htim4);
 //__HAL_TIM_SET_COUNTER(&htim4,0);
 return Tim4Encoder;
}

0690X00000DYNJaQAP.png

This topic has been closed for replies.

4 replies

waclawek.jan
Super User
March 1, 2020

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

JW

SYoun.1
SYoun.1Author
Associate
March 1, 2020

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

waclawek.jan
Super User
March 1, 2020

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

SYoun.1
SYoun.1Author
Associate
March 1, 2020

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
March 2, 2020

The counter counts from zero to ARR and then wraps back to zero. If you turn the decoder in other direction, if first wraps to ARR and then goes to zero.

When you use integers, you just interpret the value such that it's -ARR/2 to ARR/2. It's easy, if your counter is 16-bit and you use int16 as the data type. It automatically matches. That's the beauty of 2's complement. If your ARR is 0x7F, the counter increments to 0x7F (127) and then goes to zero.

turboscrew
Senior III
March 1, 2020

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

SYoun.1
SYoun.1Author
Associate
March 2, 2020

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.

turboscrew
Senior III
March 2, 2020

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

turboscrew
Senior III
March 1, 2020

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

If you are using CubeMX, do it there.

SYoun.1
SYoun.1Author
Associate
March 2, 2020

Thank you, I will try it.

turboscrew
Senior III
March 2, 2020

That's 'counter period'-field in CubeMX.