Arduino Uno 的 Rx 和 Tx 引脚不能用作通过 USB 进行串行通信的输入?

如何解决Arduino Uno 的 Rx 和 Tx 引脚不能用作通过 USB 进行串行通信的输入?

我目前正在开展一个项目,该项目涉及 LCD、传感器和键盘。

Arduino Uno 有 14 个输入/输出引脚(0-13,0 为 Rx,1 为 Tx)。

LCD 本身占用 5 个引脚(引脚 8-12)、传感器 1(引脚 7)和 keypad 7(引脚 0-6),这很好,因为我有 14 个潜在引脚。>

所以我下载了一个基本程序来快速测试键盘并调整必要的变量。我想通过串行通信在计算机上显示数字。第 1 行和第 4 行有效,第 2 行和第 3 行没有。键盘不会有问题,因为我测量了每种组合的电阻。然后我意识到保存第 2 行和第 3 行信息的引脚连接到 Arduino Uno(Rx 和 Tx)的引脚 0 和 1。

当串行通信处于活动状态时,是否可以使用引脚 0 和 1 作为输入?

如果是,那一定是代码:

/*     SparkFun Keypad Pinout:
       Rows and columns are connected as such:
       -------------
     R1  | 1 | 2 | 3 | - 5 (grün)
     R2  | 4 | 5 | 6 | - 0 (braun)
     R3  | 7 | 8 | 9 | - 1 (schwarz)
     R4  | * | 0 | # | - 3 (violett)
       -------------
           |C1 |C2 |C3
     4(blau) 6(gelb) 2(weiß)
    */
    // Pins 1-7 of the keypad connected to the Arduino respectively:
    int keypadPins[7] = {0,1,2,3,4,5,6};
    int keypadStatus;  // Used to monitor which buttons are pressed.
    int timeout;  // timeout variable used in loop
    
    void setup()
    {
      Serial.begin(9600);
      for (int i=0; i<7; i++)
      {
        pinMode(keypadPins[i],INPUT);  // Set all keypad pins as inputs
        digitalWrite(keypadPins[i],HIGH);  // pull all keypad pins high
      }
    }
    
    void loop()
    {
      keypadStatus = getKeypadStatus();  // read which buttons are pressed
      if (keypadStatus != 0)  // If a button is pressed go into here
      {
        sendKeyPress(keypadStatus);  // send the button over USB
        timeout = 2000;  // top of the repeat delay
        while ((getKeypadStatus() == keypadStatus) && (--timeout))  // Decrement timeout and check if key is being held down
          delayMicroseconds(1);
        while (getKeypadStatus() == keypadStatus)  // while the same button is held down
        {
          sendKeyPress(keypadStatus);  // continue to send the button over USB
          delay(50);  // 50ms repeat rate
        }
      }
    }
    
    /* sendKeyPress(int key): This function sends a single key over USB
       It requires an int,of which the 12 LSbs are used. Each bit in
       key represents a single button on the keypad.
       This function will only send a key press if a single button
       is being pressed */
    void sendKeyPress(int key)
    {
      switch(key)
      {
        case 1:  // 0x001
          Serial.print('1');  // Sends a keyboard '1'
          break;
        case 2:  // 0x002
          Serial.print('2');
          break;
        case 4:  // 0x004
          Serial.print('3');
          break;
        case 8:  // 0x008
          Serial.print('4');
          break;
        case 16:  // 0x010
          Serial.print('5');
          break;
        case 32:  // 0x020
          Serial.print('6');
          break;
        case 64:  // 0x040
          Serial.print('7');
          break;
        case 128:  // 0x080
          Serial.print('8');
          break;
        case 256:  // 0x100
          Serial.print('9');
          break;
        case 512:  // 0x200
          Serial.print('*');
          break;
        case 1024:  // 0x400
          Serial.print('0');  // Sends a keyboard '0'
          break;
        case 2048:  // 0x800
          Serial.print('\n');  // Sends the 'ENTER' key
          break;
      }
    }
    
    /* getKeypadStatus(): This function returns an int that represents
    the status of the 12-button keypad. Only the 12 LSb's of the return
    value hold any significange. Each bit represents the status of a single
    key on the button pad. '1' is bit 0,'2' is bit 1,'3' is bit 2,...,'#' is bit 11.
    
    This function doesn't work for multitouch.
    */
    int getKeypadStatus()
    {
      int rowPins[4] = {keypadPins[5],keypadPins[0],keypadPins[1],keypadPins[3]};  // row pins are 5,and 3 of the keypad
      int columnPins[3] = {keypadPins[4],keypadPins[6],keypadPins[2]};  // column pins are pins 2,and 6 of the keypad
      int keypadStatus = 0;  // this will be what's returned
      
      /* initialize all pins,inputs w/ pull-ups */
      for (int i=0; i<7; i++)
      {
        pinMode(keypadPins[i],INPUT);
        digitalWrite(keypadPins[i],HIGH);
      }
      
      for (int row=0; row<4; row++)
      {  // initial for loop to check all 4 rows
        pinMode(rowPins[row],OUTPUT);  // set the row pin as an output
        digitalWrite(rowPins[row],LOW);  // pull the row pins low
        for (int col=0; col<3; col++)
        {  // embedded for loop to check all 3 columns of each row
          if (!digitalRead(columnPins[col]))
          {
            keypadStatus |= 1 << ((row+1)*3 + (col+1) - 4);  // set the status bit of the keypad return value
          }
        }
        pinMode(rowPins[row],INPUT);  // reset the row pin as an input
        digitalWrite(rowPins[row],HIGH);  // pull the row pin high
      }
      
      return keypadStatus;
    }

解决方法

是否可以将 Pin 0 和 1 用作社交通信时的输入?

我从未使用过这部分,但我还是花了几分钟通过 RTFM ATmega328P p.71 找到相关文本

• TXD/PCINT17 – 端口 D,位 1 TXD,发送数据(USART 的数据输出引脚)。当启用 USART 发送器时,该引脚配置为 输出与 DDD1 的值无关。

• RXD/PCINT16 – 端口 D,位 0 RXD,接收数据(USART 的数据输入引脚)。当启用 USART 接收器时,此引脚配置为输入 无论 DDD0 的值如何。当 USART 强制该管脚为输入时,上拉仍然可以由 PORTD0 位。

因此,我建议您熟悉友好的 MCU 手册,而不是 Arduino this 和 Arduino that。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res