ASCIITable

はじめに

ASCII文字を、文字、10進数、16進数、8進数、2進数の形式でシリアルコンソールに表示します。

ArduinoとPCとをUSBケーブルで接続します。

プログラム

定義等

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
/*
  ASCII table

  Prints out byte values in all possible formats:
  - as raw binary values
  - as ASCII-encoded decimal, hex, octal, and binary values

  For more on ASCII, see http://www.asciitable.com and http://en.wikipedia.org/wiki/ASCII

  The circuit: No external hardware needed.

  created 2006
  by Nicholas Zambetti <http://www.zambetti.com>
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/ASCIITable
*/
 
33
34
35
36
37
// first visible ASCIIcharacter '!' is number 33:
int thisByte = 33;
// you can also write ASCII characters in single quotes.
// for example, '!' is the same as 33, so you could also use this:
// int thisByte = '!';

int型の変数thisByteを定義し、初期化します。シリアルコンソールに表示する文字を保持する変数です。

setup()

22
23
24
25
26
27
28
29
30
31
32
void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // prints title with ending line break
  Serial.println("ASCII Table ~ Character Map");
}
 

Serial.begin()を使って、シリアルポートを初期化します。その後、Serial.println()で文字列をシリアルコンソールに表示します。

Arduino Loenardでは、シリアルコンソールを開いたときにリセットがかからないようです。このため、シリアルポートを開いたときには、既に送信してしまったデータは表示されません。これを防ぐために、シリアルポートが接続されるまで待つことで対応しています。

loop()

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
void loop() {
  // prints value unaltered, i.e. the raw binary version of the byte.
  // The Serial Monitor interprets all bytes as ASCII, so 33, the first number,
  // will show up as '!'
  Serial.write(thisByte);

  Serial.print(", dec: ");
  // prints value as string as an ASCII-encoded decimal (base 10).
  // Decimal is the default format for Serial.print() and Serial.println(),
  // so no modifier is needed:
  Serial.print(thisByte);
  // But you can declare the modifier for decimal if you want to.
  // this also works if you uncomment it:

  // Serial.print(thisByte, DEC);


  Serial.print(", hex: ");
  // prints value as string in hexadecimal (base 16):
  Serial.print(thisByte, HEX);

  Serial.print(", oct: ");
  // prints value as string in octal (base 8);
  Serial.print(thisByte, OCT);

  Serial.print(", bin: ");
  // prints value as string in binary (base 2) also prints ending line break:
  Serial.println(thisByte, BIN);

  // if printed last visible character '~' or 126, stop:
  if (thisByte == 126) {    // you could also use if (thisByte == '~') {
    // This loop loops forever and does nothing
    while (true) {
      continue;
    }
  }
  // go on to the next character
  thisByte++;
}

Serial.print()を使って、thisByteを表示します。第2引数の指定で、表示形式が変わります。

loop()の最後で、if文を使いthisByteが126かどうかを調べて、126であれば、while文による無限ループに入ります。trueは0x01と定義されています。このため、while(1)と解釈されます。while文は、与えられた式が0以外のときに実行されるため、この場合は無限に実行されることになります。continue文は、繰り返し実行のループの残りを実行せず、ループの制御式の評価へと制御を移す働きがあります。今回の場合は、continue文を記述せずに、以下のように記述しても無限ループを作成することができます。

1
2
3
while(1) {
    ;
}

これにより、実際にはプログラムは動いていますが、見た目には止まります。

thisByteが126でなければ、増分演算子++を使ってthisByteを1増やします。

バージョン

Hardware:Arduino Uno
Software:Arduino 1.8.16

最終更新日

September 11, 2021

inserted by FC2 system