简体   繁体   English

STM32 - TIM2_ETR 引脚,连接到引脚 PA0(按钮),以奇怪的方式递增定时器

[英]STM32 - TIM2_ETR pin, connected to pin PA0 (button), incrementing the timer in strange way

I am trying to implement PWM LED dimming in 6 stages, where each stage in more bright, based on clicking button, which increments external pin, which serves the value to timer.我试图在 6 个阶段实现 PWM LED 调光,其中每个阶段都更亮,基于单击按钮,增加外部引脚,为定时器提供值。

I am facing a problem, that sometimes, value variable is too large than it should be and skips some levels of brightness.我面临一个问题,有时, value变量比它应该的大,并且跳过了某些级别的亮度。 For example, value increments: 1,2,3, then jumps to 6,7, etc.例如, value递增:1,2,3,然后跳转到 6,7 等。

Can anybody pinpoint where is the mistake I am making.任何人都可以查明我犯的错误在哪里。 Here is the code:这是代码:

//EDIT: code removed, because it is a school assignment //编辑:代码已删除,因为它是学校作业

This looks like contact bouncing.这看起来像是接触反弹。 When input is processed by the CPU, a simple way to solve it is to disable input for a certain duration after an event is detected.当 CPU 处理输入时,一个简单的解决方法是在检测到事件后在一定时间内禁用输入。 Since you directly control timer input from a button, you may not have much control.由于您直接从按钮控制计时器输入,因此您可能没有太多控制权。 However, I would experiment with the ETF field of the SMCR register (which in your case is likely set by the sClockSourceConfig.ClockFilter field) and the clock divisor CKD of the CR1 register (which seems like htim2.Init.ClockDivision in your code) (sorry, I am not familiar with STM libraries).但是,我会尝试使用 SMCR 寄存器的 ETF 字段(在您的情况下可能由 sClockSourceConfig.ClockFilter 字段设置)和 CR1 寄存器的时钟除数 CKD(在您的代码中看起来像 htim2.Init.ClockDivision) (抱歉,我不熟悉 STM 库)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM