## **ASCIl表**
~~~
本例向你展示先進的串口輸出功能。程序會在Arduino IDE的串口監視器上輸出一個字符表,表中每個字符會還會帶有它的十進制、十六進制、八進制和二進制ASCII碼。
想要進一步了解ASCII,請看asciitable.com
~~~
## **所需硬件**
Arduino板或Genuino板?
連接線
## **電路**
?
不需要額外電路,但是板子必須通過串口線或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