【Beetle ESP32 C6 Mini 】外设之 IO 与串口
<div class='showpostmsg'><p> 本期主要验证Beetle ESP32 C6 Mini 外设中 IO 与串口功能;</p><p> 在如下原理图中:</p>
<p> IO 口比较典型: </p>
<p> GPIO15 --> LED1, 高电平点亮</p>
<p> GPIO09 --> KEY, 低电平触发</p>
<p> UART 功能:</p>
<p> TX --> GPIO16</p>
<p> RX--> GPIO17</p>
<p> </p>
<p> </p>
<p> </p>
<p> 连接Beetle ESP32 C6 Mini 与USB 转UART 功能;</p>
<p> </p>
<p> </p>
<p>设置软件代码如下:</p>
<pre>
<code class="language-cpp">#include <Arduino.h>
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> </p>
<p>代码实现了 IO的输入输出, 中断, 串口的打印功能;</p>
<p> </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> </p>
<p> </p>
<p>如果代码变成如下;</p>
<pre>
<code>void setup() {
pinMode(6, OUTPUT);
}
void loop()
{
digitalWrite(6,1);
digitalWrite(6,0);
}
</code></pre>
<p> </p>
<p> </p>
<p>也就新一轮的loop 开始中间是有间隔的;这个和普通的main.c 函数有什么区别呢?</p>
<p>查阅原函数可以看见main.cpp 设计如下, 所有就会有以上区别;</p>
<p> </p>
<p> </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> <p>和普通的main.c 函数区别并不大</p>
页:
[1]