打印
[PCB电路]

分享一个五向开关更少IO的设计

[复制链接]
175|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
本帖最后由 电子烂人 于 2025-2-16 10:21 编辑

#申请原创#[url=home.php?mod=space&uid=760190]@21小跑堂 [/url]0.前言:

之前做小产品用到过很多五向开关按键这个料,一个五项开关就要5个IO口来驱动,很是浪费

(图源:立创商城)

正巧在ST的C0116-DK开发板上看到过一个非常“巧妙”的设计,拿来品鉴一下

(图源:ST文档MB1684)

1.硬件赏析
C0116-DK上面搭载的是一片C011F6,UFQFPN20的封装,注定了这个MCU绝对不能用5个IO单独驱动五向开关
DK开发板的工程师很清楚这一点,所以他们用了另一种办法:ADC监测每个IO上的电压值

(图源:ST文档MB1684)

每个IO所接的电阻值不同,电压故而不一样,很巧妙的避开了IO占用过多的问题

(图源:ST工程MB1684)

2.代码参考
这里写一段ADC检测IO的代码,仅供参考
以ADC1为例:
<font size="4">HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 50);
if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC))
{
ADC_Value = HAL_ADC_GetValue(&hadc1);
}
HAL_Delay(1000);</font>

之后只需用一个ELSE IF语句,来判定当前按下的IO是哪个即可:
<ul style="font-style: italic; list-style: decimal; color: rgb(56, 58, 66); font-family: &quot;DejaVu Sans Mono&quot;, &quot;Ubuntu Mono&quot;, &quot;Anonymous Pro&quot;, &quot;Droid Sans Mono&quot;, Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, sans-serif; font-size: 14px; white-space-collapse: preserve; margin-left: 30px !important;"><li style="list-style: decimal-leading-zero; line-height: 14px; word-break: break-all; font-size: 12px; color: inherit; margin-left: 0px !important; padding: 5px !important; font-style: normal !important; border-left: 1px solid rgb(221, 221, 221) !important;"><span class="hljs-keyword" style="color: rgb(166, 38, 164);">if</span> ((ADC_Value >= <span class="hljs-number" style="color: rgb(152, 104, 1);">0</span>)&&(ADC_Value < <span class="hljs-number" style="color: rgb(152, 104, 1);">1000</span>))
</li><li style="list-style: decimal-leading-zero; line-height: 14px; word-break: break-all; font-size: 12px; margin-left: 0px !important; padding: 5px !important; font-style: normal !important; border-left: 1px solid rgb(221, 221, 221) !important;">    {
</li><li style="list-style: decimal-leading-zero; line-height: 14px; word-break: break-all; font-size: 12px; color: inherit; margin-left: 0px !important; padding: 5px !important; font-style: normal !important; border-left: 1px solid rgb(221, 221, 221) !important;">            io=1;
</li><li style="list-style: decimal-leading-zero; line-height: 14px; word-break: break-all; font-size: 12px; margin-left: 0px !important; padding: 5px !important; font-style: normal !important; border-left: 1px solid rgb(221, 221, 221) !important;">    }
</li><li style="list-style: decimal-leading-zero; line-height: 14px; word-break: break-all; font-size: 12px; color: inherit; margin-left: 0px !important; padding: 5px !important; font-style: normal !important; border-left: 1px solid rgb(221, 221, 221) !important;">    <span class="hljs-keyword" style="color: rgb(166, 38, 164);">else</span> <span class="hljs-keyword" style="color: rgb(166, 38, 164);">if</span> ((ADC_Value >= <span class="hljs-number" style="color: rgb(152, 104, 1);">1000</span>) &&(ADC_Value < <span class="hljs-number" style="color: rgb(152, 104, 1);">2000</span>))
</li><li style="list-style: decimal-leading-zero; line-height: 14px; word-break: break-all; font-size: 12px; margin-left: 0px !important; padding: 5px !important; font-style: normal !important; border-left: 1px solid rgb(221, 221, 221) !important;">    {
</li><li style="list-style: decimal-leading-zero; line-height: 14px; word-break: break-all; font-size: 12px; color: inherit; margin-left: 0px !important; padding: 5px !important; font-style: normal !important; border-left: 1px solid rgb(221, 221, 221) !important;">            io=2;
</li><li style="list-style: decimal-leading-zero; line-height: 14px; word-break: break-all; font-size: 12px; margin-left: 0px !important; padding: 5px !important; font-style: normal !important; border-left: 1px solid rgb(221, 221, 221) !important;">    }
</li><li style="list-style: decimal-leading-zero; line-height: 14px; word-break: break-all; font-size: 12px; color: inherit; margin-left: 0px !important; padding: 5px !important; font-style: normal !important; border-left: 1px solid rgb(221, 221, 221) !important;">    <span class="hljs-keyword" style="color: rgb(166, 38, 164);">else</span> <span class="hljs-keyword" style="color: rgb(166, 38, 164);">if</span> ((ADC_Value >= <span class="hljs-number" style="color: rgb(152, 104, 1);">2000</span>)&& (ADC_Value < <span class="hljs-number" style="color: rgb(152, 104, 1);">3000</span>))
</li><li style="list-style: decimal-leading-zero; line-height: 14px; word-break: break-all; font-size: 12px; margin-left: 0px !important; padding: 5px !important; font-style: normal !important; border-left: 1px solid rgb(221, 221, 221) !important;">    {
</li><li style="list-style: decimal-leading-zero; line-height: 14px; word-break: break-all; font-size: 12px; color: inherit; margin-left: 0px !important; padding: 5px !important; font-style: normal !important; border-left: 1px solid rgb(221, 221, 221) !important;">            io=3;
</li><li style="list-style: decimal-leading-zero; line-height: 14px; word-break: break-all; font-size: 12px; margin-left: 0px !important; padding: 5px !important; font-style: normal !important; border-left: 1px solid rgb(221, 221, 221) !important;">    }
</li><li style="list-style: decimal-leading-zero; line-height: 14px; word-break: break-all; font-size: 12px; color: inherit; margin-left: 0px !important; padding: 5px !important; font-style: normal !important; border-left: 1px solid rgb(221, 221, 221) !important;">    <span class="hljs-keyword" style="color: rgb(166, 38, 164);">else</span> <span class="hljs-keyword" style="color: rgb(166, 38, 164);">if</span> ((ADC_Value >= <span class="hljs-number" style="color: rgb(152, 104, 1);">3000</span>)&&( ADC_Value < <span class="hljs-number" style="color: rgb(152, 104, 1);">4000</span>))
</li><li style="list-style: decimal-leading-zero; line-height: 14px; word-break: break-all; font-size: 12px; margin-left: 0px !important; padding: 5px !important; font-style: normal !important; border-left: 1px solid rgb(221, 221, 221) !important;">    {</li><li style="list-style: decimal-leading-zero; line-height: 14px; word-break: break-all; font-size: 12px; margin-left: 0px !important; padding: 5px !important; font-style: normal !important; border-left: 1px solid rgb(221, 221, 221) !important;">            io=4;
</li><li style="list-style: decimal-leading-zero; line-height: 14px; word-break: break-all; font-size: 12px; color: inherit; margin-left: 0px !important; padding: 5px !important; font-style: normal !important; border-left: 1px solid rgb(221, 221, 221) !important;">    }</li></ul>
本人代码水平不高,勿喷。。。
3.总结
其实很多时候做的工作都是在做优化,用减法把项目做到预算范围内,这种用更小封装、更少IO去做更多功能的例子还有很多,欢迎各位朋友分享。

1656867b14479ae621.png (30.88 KB )

1656867b14479ae621.png

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

14

主题

62

帖子

0

粉丝