## **ASCIl表**
~~~
本例向你展示先进的串口输出功能。程序会在Arduino IDE的串口监视器上输出一个字符表,表中每个字符会还会带有它的十进制、十六进制、八进制和二进制ASCII码。
想要进一步了解ASCII,请看asciitable.com
~~~
## **所需硬件**
Arduino板或Genuino板
连接线
## **电路**
![图片来自官网](http://img.blog.csdn.net/20160511163640639)
不需要额外电路,但是板子必须通过串口线或USB线连接到电脑。
## **代码**
程序在setup()函数中建立串口连接,然后逐行输出ASCII表,直到最后一个ASCII字符被显示。
当这个操作结束后,它会进入loop函数中的while死循环,之后就不做任何事了。注意:关闭或打开Arduino IDE上的串口监视器都会**重置**(**reset**)Arduino板。程序会从头开始运行。
~~~
/*
ASCIl表
用所有可能的格式输出byte:
* 原始二进制值
* 对应的十进制、十六进制、八进制和二进制码
想了解更多ASCII请看:
http://www.asciitable.com
http://en.wikipedia.org/wiki/ASCII
电路搭建: 不需要任何其他电路
代码是公开的.
*/
void setup() {
//初始化串口,并且等待串口准备好:
Serial.begin(9600);
while (!Serial) {
//等待串口初始化完毕
}
// 打印表头
Serial.println("ASCII表 ~ 字符和其映射");
}
// 第一个可被打印的ASCII字符是 '!' 对应数字33:
int thisByte = 33;
// 你也可以直接用ASCII字符直接给int赋值
// 比如,'!'和数字33是一样的,因此你也可以这样写:
//int thisByte = '!';
void loop() {
// 打印byte原始值, 换句话说,就是这个byte的原始二进制值。串口监视器将会将所有的byte用ASCII表对应解释。因此第一个数字33将会以'!'显示
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.print(thisByte, HEX);
Serial.print(", oct: ");
// 用八进制显示(基数为8):
Serial.print(thisByte, OCT);
Serial.print(", bin: ");
// 用二进制显示(基数为2),然后换行:
Serial.println(thisByte, BIN);
// 如果输出到最后一个可被打印的字符, '~' 或者它的对应的数字126就停下:
if (thisByte == 126) {
// 你也可以这样写:
//if (thisByte == '~')
// 这个循环是死循环,不会做任何事
while (true) {
continue;
}
}
// 继续下一个字符
thisByte++;
}
~~~
输出如下:
~~~
ASCII Table ~ Character Map
!, dec: 33, hex: 21, oct: 41, bin: 100001
", dec: 34, hex: 22, oct: 42, bin: 100010
#, dec: 35, hex: 23, oct: 43, bin: 100011
$, dec: 36, hex: 24, oct: 44, bin: 100100
%, dec: 37, hex: 25, oct: 45, bin: 100101
&, dec: 38, hex: 26, oct: 46, bin: 100110
', dec: 39, hex: 27, oct: 47, bin: 100111
(, dec: 40, hex: 28, oct: 50, bin: 101000
), dec: 41, hex: 29, oct: 51, bin: 101001
*, dec: 42, hex: 2A, oct: 52, bin: 101010
+, dec: 43, hex: 2B, oct: 53, bin: 101011
,, dec: 44, hex: 2C, oct: 54, bin: 101100
-, dec: 45, hex: 2D, oct: 55, bin: 101101
., dec: 46, hex: 2E, oct: 56, bin: 101110
/, dec: 47, hex: 2F, oct: 57, bin: 101111
0, dec: 48, hex: 30, oct: 60, bin: 110000
1, dec: 49, hex: 31, oct: 61, bin: 110001
2, dec: 50, hex: 32, oct: 62, bin: 110010
3, dec: 51, hex: 33, oct: 63, bin: 110011
4, dec: 52, hex: 34, oct: 64, bin: 110100
5, dec: 53, hex: 35, oct: 65, bin: 110101
6, dec: 54, hex: 36, oct: 66, bin: 110110
7, dec: 55, hex: 37, oct: 67, bin: 110111
8, dec: 56, hex: 38, oct: 70, bin: 111000
9, dec: 57, hex: 39, oct: 71, bin: 111001
:, dec: 58, hex: 3A, oct: 72, bin: 111010
;, dec: 59, hex: 3B, oct: 73, bin: 111011
<, dec: 60, hex: 3C, oct: 74, bin: 111100
=, dec: 61, hex: 3D, oct: 75, bin: 111101
>, dec: 62, hex: 3E, oct: 76, bin: 111110
?, dec: 63, hex: 3F, oct: 77, bin: 111111
@, dec: 64, hex: 40, oct: 100, bin: 1000000
A, dec: 65, hex: 41, oct: 101, bin: 1000001
B, dec: 66, hex: 42, oct: 102, bin: 1000010
C, dec: 67, hex: 43, oct: 103, bin: 1000011
D, dec: 68, hex: 44, oct: 104, bin: 1000100
E, dec: 69, hex: 45, oct: 105, bin: 1000101
...
~~~
### **相关资料**
[自加操作符, ++](https://www.arduino.cc/en/Reference/Increment)
[while()](https://www.arduino.cc/en/Reference/While)
[serial()](https://www.arduino.cc/en/Reference/Serial)
- 说明
- 系统示例文件目录结构及说明
- 01.Basics
- AnalogReadSerial
- BareMinimum
- Blink
- DigitalReadSerial
- Fade
- ReadAnalogVoltage
- 02.Digital
- BlinkWithoutDelay
- Button
- Debounce
- DigitalInputPullup
- StateChangeDetection
- toneKeyboard
- toneMelody
- toneMultiple
- tonePitchFollower
- 03.Analog
- AnalogInOutSerial
- AnalogInput
- AnalogWriteMega
- Calibration
- Fading
- Smoothing
- 04.Communication
- ASCIITable
- Dimmer
- Graph
- Midi
- MultiSerial
- PhysicalPixel
- ReadASCIIString
- SerialCallResponse
- SerialCallResponseASCII
- SerialEvent
- SerialPassthrough
- VirtualColorMixer
- 05.Control
- Arrays
- ForLoopIteration
- IfStatementConditional
- switchCase
- switchCase2
- WhileStatementConditional
- 06.Sensors
- ADXL3xx
- Knock
- Memsic2125
- Ping
- 07.Display
- barGraph
- RowColumnScanning
- 08.Strings
- CharacterAnalysis
- StringAdditionOperator
- StringAppendOperator
- StringCaseChanges
- StringCharacters
- StringComparisonOperators
- StringConstructors
- StringIndexOf
- StringLength
- StringLengthTrim
- StringReplace
- StringStartsWithEndsWith
- StringSubstring
- StringToInt
- 09.USB
- Keyboard
- KeyboardLogout
- KeyboardMessage
- KeyboardReprogram
- KeyboardSerial
- KeyboardAndMouseControl
- Mouse
- ButtonMouseControl
- JoystickMouseControl
- 10.StarterKit_BasicKit (与特定硬件相关,暂无)
- p02_SpaceshipInterface
- p03_LoveOMeter
- p04_ColorMixingLamp
- p05_ServoMoodIndicator
- p06_LightTheremin
- p07_Keyboard
- p08_DigitalHourglass
- p09_MotorizedPinwheel
- p10_Zoetrope
- p11_CrystalBall
- p12_KnockLock
- p13_TouchSensorLamp
- p14_TweakTheArduinoLogo
- p15_HackingButtons
- 11.ArduinoISP(暂无)
- ArduinoISP