2020-03-01 03:33 AM
int16_t GetTim4Encoder(){
Tim4Encoder = __HAL_TIM_GET_COUNTER(&htim4);
//__HAL_TIM_SET_COUNTER(&htim4,0);
return Tim4Encoder;
}
2020-03-01 04:32 AM
What's the value of other TIM4 registers, mainly TIM4_ARR?
JW
2020-03-01 05:15 AM
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.
2020-03-01 07:02 AM
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
2020-03-01 08:10 AM
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);
2020-03-01 08:12 AM
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?
2020-03-01 08:31 AM
You could also initialize the ARR to 65535 and see if that makes the difference.
If you are using CubeMX, do it there.
2020-03-01 05:11 PM
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.
2020-03-01 05:42 PM
Thank you, I will try it.
2020-03-01 09:11 PM
To have negative values, it should be int16 or int32.