DavidZH 发表于 2024-5-12 21:36

【Beetle ESP32 C6 Mini 】外设之 IO 与串口

<div class='showpostmsg'><p>&nbsp; &nbsp; 本期主要验证Beetle ESP32 C6 Mini&nbsp; 外设中 IO 与串口功能;</p>

<p>&nbsp; &nbsp; 在如下原理图中:</p>

<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IO 口比较典型:&nbsp;</p>

<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GPIO15 --&gt; LED1, 高电平点亮</p>

<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GPIO09 --&gt;&nbsp; KEY, 低电平触发</p>

<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UART 功能:</p>

<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TX --&gt; GPIO16</p>

<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RX--&gt;&nbsp; GPIO17</p>

<p>&nbsp;</p>

<p> &nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp; &nbsp;连接Beetle ESP32 C6 Mini 与USB 转UART 功能;</p>

<p> &nbsp;</p>

<p>&nbsp;</p>

<p>设置软件代码如下:</p>

<pre>
<code class="language-cpp">#include &lt;Arduino.h&gt;

struct Button {
const uint8_t PIN;
uint32_t numberKeyPresses;
bool pressed;
};

Button button1 = {9, 0, false};

#define LED    15
uint8_t stateLED = 0;


void ARDUINO_ISR_ATTR isr() {
button1.numberKeyPresses += 1;
button1.pressed = true;
}


void setup() {
pinMode(LED, OUTPUT);
pinMode(button1.PIN, INPUT_PULLUP);
attachInterrupt(button1.PIN, isr, FALLING);
Serial.begin(115200);

}

void loop() {


if (button1.pressed) {
    Serial.printf("Button 1 has been pressed %lu times\n\r", button1.numberKeyPresses);
    button1.pressed = false;
    stateLED = stateLED^1;
    digitalWrite(LED,stateLED);

}

}
</code></pre>

<p>最终输出:</p>

<p> &nbsp;</p>

<p>代码实现了 IO的输入输出, 中断, 串口的打印功能;</p>

<p>&nbsp;</p>

<p>单独验证IO口从1到0 输出的时间;</p>

<pre>
<code>void setup() {
pinMode(6, OUTPUT);
}

void loop()
{
digitalWrite(6,0);
digitalWrite(6,1);
}
</code></pre>

<p>此时输出波形; 高低电平的时间并不是一样长;</p>

<p> &nbsp;</p>

<p>&nbsp;</p>

<p>如果代码变成如下;</p>

<pre>
<code>void setup() {
pinMode(6, OUTPUT);
}

void loop()
{
digitalWrite(6,1);
digitalWrite(6,0);
}
</code></pre>

<p> &nbsp;</p>

<p>&nbsp;</p>

<p>也就新一轮的loop 开始中间是有间隔的;这个和普通的main.c 函数有什么区别呢?</p>

<p>查阅原函数可以看见main.cpp 设计如下, 所有就会有以上区别;</p>

<p> &nbsp;</p>

<p>&nbsp;</p>
</div><script>                                        var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;"   style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
                                       
                                        if(parseInt(discuz_uid)==0){
                                                                                                (function($){
                                                        var postHeight = getTextHeight(400);
                                                        $(".showpostmsg").html($(".showpostmsg").html());
                                                        $(".showpostmsg").after(loginstr);
                                                        $(".showpostmsg").css({height:postHeight,overflow:"hidden"});
                                                })(jQuery);
                                        }                </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>

Jacktang 发表于 2024-5-12 21:59

<p>和普通的main.c 函数区别并不大</p>
页: [1]
查看完整版本: 【Beetle ESP32 C6 Mini 】外设之 IO 与串口