Arduino在串口监视器上输出字母表

程序会在Arduino IDE的串口监视器上输出一个字母表.

不需要额外电路,但是板子必须通过串口线或USB线连接到电脑。

代码

程序在setup()函数中建立串口连接,然后逐行输出a~z的字母I表,直到最后一个ASCII字符被显示,然后进入死循环。

注意:关闭或打开Arduino IDE上的串口监视器都会重置(reset)Arduino板。程序会从头开始运行。

void setup() {
  //初始化串口,并且等待串口准备好:
  Serial.begin(9600);
  while (!Serial) {
    //等待串口初始化完毕
  }

  // 打印表头
  Serial.println("26个英文字母");
}

// 第一个可被打印的ASCII字符是 'a' 对应数字97:
int thisByte = 97;  //int thisByte = 'a';

void loop() {
  // 打印byte原始值, 换句话说,就是这个byte的原始二进制值。串口监视器将会将所有的byte用ASCII表对应解释。因此第一个数字97将会以'a'显示
  Serial.write(thisByte);

  Serial.print(", dec: ");
  // 使用十进制的ASCII(基数为10)输出
  // 十进制是Serial.print() 和 Serial.println()的基本格式,因此不需要多加修饰符:
  Serial.print(thisByte);

  // 下列代码不加DEC参数也可,但是如果你有强迫症,非要加,当然也无碍:
  // Serial.print(thisByte, DEC);

  Serial.print(", hex: ");
  // 用十六进制显示(基数为16):
  Serial.println(thisByte, HEX);

  // 如果输出到最后一个可被打印的字符, '~' 或者它的对应的数字126就停下:
  if (thisByte == 'z') {     
   // 这个循环是死循环,不会做任何事
    while (true) {
      continue;
    }
  }
  // 继续下一个字符
  thisByte++;
}

输出如下:

//点击Arduino IDE左上角的“串口监视器”查看

26个英文字母
a, dec: 97, hex: 61
b, dec: 98, hex: 62
c, dec: 99, hex: 63
d, dec: 100, hex: 64
e, dec: 101, hex: 65
f, dec: 102, hex: 66
g, dec: 103, hex: 67
h, dec: 104, hex: 68
i, dec: 105, hex: 69
j, dec: 106, hex: 6A
k, dec: 107, hex: 6B
l, dec: 108, hex: 6C
m, dec: 109, hex: 6D
n, dec: 110, hex: 6E
o, dec: 111, hex: 6F
p, dec: 112, hex: 70
q, dec: 113, hex: 71
r, dec: 114, hex: 72
s, dec: 115, hex: 73
t, dec: 116, hex: 74
u, dec: 117, hex: 75
v, dec: 118, hex: 76
w, dec: 119, hex: 77
x, dec: 120, hex: 78
y, dec: 121, hex: 79
z, dec: 122, hex: 7A

板子上还有两个分别标为TX、RX的LED灯。

在通信上,

TX是发送 transmit,RX是接受 receive。

TXD就是发送数据Transmit Data,RXD是接受数据Receive Data.

 例如,在本程序,在输出的过程中TX会一直亮,直到发送结束。

参考连接:

1. https://www.kancloud.cn/yundantiankong/arduino_examples/431644

2. https://zhidao.baidu.com/question/1175332619995248339.html

原文地址:https://www.cnblogs.com/lfri/p/11606801.html