我可以将 TIMx ARR 值设置为以所选值初始化吗?
Can I set TIMx ARR value to initialize at chosen value?
所以我正在尝试使用旋转编码器来控制我的 STM32 项目上的菜单。我正在使用两个旋转编码器来控制屏幕的每一侧(拆分菜单)。
当我初始化负责对编码器脉冲进行计数的两个定时器的 ARR 寄存器时,它将寄存器初始化为 0 值,当我逆时针移动编码器时,寄存器溢出并达到最大值 65535 并且混乱以及我的代码如何计算制动器。
你们能告诉我是否有任何方法可以将 TIM->CNT 值设置为介于 0 和 65535 之间某处的自定义值吗?
这样我就可以很容易地检查值之间的差异,而不用担心数字的跳跃。
when I move the encoders counterclockwise the registers overflow and goes to maximum values of 65535 and messes with how my code calculates detents.
计数器是放入 32 位无符号寄存器的 16 位值。要获得正确的带符号值,请将其转换为 int16_t
.
int wheelposition = (int16_t)TIMx->CNT;
值 65535 (0xFFFF) 将被符号扩展为 0xFFFFFFFF,这在 32 位整数变量中被解释为 -1。但是你会遇到它从 -32768 溢出到 +32767 的问题。
如果您对两个位置读数的有符号差感兴趣,可以对无符号值进行减法,并将结果转换为int16_t
。
uint32_t oldposition, newposition;
int wheelmovement;
oldposition = TIMx->CNT;
/* wait a bit */
newposition = TIMx->CNT;
wheelmovement = (int16_t)(newposition - oldposition);
它会给你带符号的差异,考虑到 16 位溢出。
is any way to set the the TIM->CNT value to a custom value somewhere in the middle between 0 and 65535 ?
您可以简单地为 TIMx->CNT
分配任何值,它会从那里继续计数。
所以我正在尝试使用旋转编码器来控制我的 STM32 项目上的菜单。我正在使用两个旋转编码器来控制屏幕的每一侧(拆分菜单)。
当我初始化负责对编码器脉冲进行计数的两个定时器的 ARR 寄存器时,它将寄存器初始化为 0 值,当我逆时针移动编码器时,寄存器溢出并达到最大值 65535 并且混乱以及我的代码如何计算制动器。
你们能告诉我是否有任何方法可以将 TIM->CNT 值设置为介于 0 和 65535 之间某处的自定义值吗? 这样我就可以很容易地检查值之间的差异,而不用担心数字的跳跃。
when I move the encoders counterclockwise the registers overflow and goes to maximum values of 65535 and messes with how my code calculates detents.
计数器是放入 32 位无符号寄存器的 16 位值。要获得正确的带符号值,请将其转换为 int16_t
.
int wheelposition = (int16_t)TIMx->CNT;
值 65535 (0xFFFF) 将被符号扩展为 0xFFFFFFFF,这在 32 位整数变量中被解释为 -1。但是你会遇到它从 -32768 溢出到 +32767 的问题。
如果您对两个位置读数的有符号差感兴趣,可以对无符号值进行减法,并将结果转换为int16_t
。
uint32_t oldposition, newposition;
int wheelmovement;
oldposition = TIMx->CNT;
/* wait a bit */
newposition = TIMx->CNT;
wheelmovement = (int16_t)(newposition - oldposition);
它会给你带符号的差异,考虑到 16 位溢出。
is any way to set the the TIM->CNT value to a custom value somewhere in the middle between 0 and 65535 ?
您可以简单地为 TIMx->CNT
分配任何值,它会从那里继续计数。