|
本文為超級終端的應(yīng)用總結(jié)文章,實驗基于51單片機,對ARM和其它MPU同樣適用。在WINDOWS XP或是WINDOWS 2000操作系統(tǒng)下,在“「開始」菜單\程序\附件\通訊”目錄下可以看到“超級終端”。這是WINDOWS自帶的通信終端工具,我們用它的串口通信功能。在“文件/屬性/連接到(選項卡)/連接時使用(項)”里設(shè)置 COM1(串口接啥就選啥),在“設(shè)置...”里設(shè)置為“19200 8 無 1 無”。最后點擊“呼叫/呼叫”行完事了。好吧,沒有介紹其它設(shè)備之前您是可以用超級終端自慰的(自己和自己玩)。將COM1串口的2、3引腳短接(接錯了把電腦搞炸了別找我),即是將發(fā)出的數(shù)據(jù)接收回來。在超級終端框里輸入信息同樣顯示出來,好像在用TXT記事本或是QQ聊天對話框一樣。如果您這樣玩的很開心那就不用再看下一節(jié)了,直到您還想知道更有趣的知識。
單片機平臺建立
斷開串口COM1的2、3引腳吧,應(yīng)該和單片機互動起來才有應(yīng)用的價值。之前,我們要有一塊擁有串口功能的單片機實驗板或是嵌入目標(biāo)板,否則請您檢查一下IE的地址欄,看看您是不是錯輸入了網(wǎng)址來到我的網(wǎng)站,希望我的地址和黃色小說的地址字符并不接近。在百度里輸入“單片機串口電路”,答案會擠進您的眼睛里,那我就不費口水了。全當(dāng)您有了我們所需要的設(shè)備,在51的FLASH中寫入下面的程序。
#include <AT89X51.h>
#include <string.h>
//BAUD SET == 19200 8 NOR 1 NOR
//主程序
void main(){
unsigned char dat;
SCON = 0x50; //SCON: serail mode 1, 8-bit UART, enable ucvr
TMOD |= 0x20; //TMOD: timer 1, mode 2, 8-bit reload
PCON |= 0x80; //SMOD=1;
TH1 = 0xFD; //Baud:9600 fosc=11.0592MHz
IE |= 0x90; //Enable Serial Interrupt
TR1 = 1; // timer 1 run
while(1){
if ( RI ){
dat = SBUF; //接收數(shù)據(jù) SBUF 為單片機的接收發(fā)送緩沖寄存器
RI = 0;
SBUF = dat; //發(fā)送數(shù)據(jù)
}
}
}
有趣的功能
向串口打印字符吧,應(yīng)該是有趣的事了。您可以在超級終端上顯示出操作界面。還可以把關(guān)鍵的字眼改變顏色、加亮或是閃爍顯示,還可以用\n(換行)、\r(歸位)、\x00(十六進制輸入)來設(shè)置一個美麗的圖景。下面的程序里我用到上面提過的功能,有興趣研究一下是一件好事,不然就看下一節(jié)。
#i nclude <AT89X51.h>
#i nclude <string.h>
void send_char_com(unsigned char ch);
void send_string_com(unsigned char *str);
void init_serialcomm(void);
void Delay(unsigned int a);
//串口初始化
void init_serialcomm(void) {//btl == 19200
SCON = 0x50; //SCON: serail mode 1, 8-bit UART, enable ucvr
TMOD |= 0x20; //TMOD: timer 1, mode 2, 8-bit reload
PCON |= 0x80; //SMOD=1;
TH1 = 0xFD; //Baud:9600 fosc=11.0592MHz
IE |= 0x90; //Enable Serial Interrupt
TR1 = 1; // timer 1 run
}
//串口接收中斷函數(shù)
void serial () interrupt 4 using 3 {
if(RI){
unsigned char ch;
RI = 0;
send_string_com("\x0c");
//send_char_com(0x0c);
x_welcome (); //顯示歡迎畫面
x_menu ( );
}
}
//向串口發(fā)送一個字符
void send_char_com(unsigned char ch) {
SBUF=ch;
while(TI == 0);
TI=0;
}
//向串口發(fā)送一個字符串,長度不限。
//應(yīng)用:send_string_com("d9887321$");
void send_string_com(unsigned char *str){
while(*str != '\0'){
send_char_com(*str);
*str++;
}
*str = 0;
}
//歡迎畫面(啟動時顯示)
void x_welcome ( void ){
send_string_com("\x0c\n\r\033[1;34m");
send_string_com("歡迎使用串口控制平臺 ");
send_string_com("\n\rby DoYoung V1.0 2006.11.11");
}
//
void x_menu ( void ){
send_string_com("\n\n\r\033[1;31mUART> \033[1;30m");
send_string_com("請輸入功能序號: ");
send_string_com("\n\r\033[1;31mdoyoung> \033[1;30m");
}
void x_run ( void ){
send_string_com("\b\x5c");
Delay(200);
send_string_com("\b|");
Delay(200);
send_string_com("\b/");
Delay(200);
send_string_com("\b-");
Delay(200);
}
void Delay(unsigned int a){
unsigned char i;
while( --a != 0){
for(i = 0; i < 125; i++); //一個 ; 表示空語句,CPU空轉(zhuǎn)。
} //i 從0加到125,CPU大概就耗時1毫秒
}
//主程序
main(){
init_serialcomm(); //初始化串口
x_welcome (); //顯示歡迎畫面
x_menu ();
while(1){
x_run ( ) ;
}
}
ASC II碼表和轉(zhuǎn)義字符
我們輸入的顯示字符都是ASC II的標(biāo)準(zhǔn)。對于一些特殊的指令,ASC II則用轉(zhuǎn)義字符更為方便。
Decimal Octal Hex Binary Value
10 08 16 02
------- ----- *--- ------ -----
000 000 000 00000000 NUL (Null char.)
001 001 001 00000001 SOH (Start of Header)發(fā)送文件首
002 002 002 00000010 STX (Start of Text)文本開始
003 003 003 00000011 ETX (End of Text)文本尾
004 004 004 00000100 EOT (End of Transmission)發(fā)送結(jié)束
005 005 005 00000101 ENQ (Enquiry)
006 006 006 00000110 ACK (Acknowledgment)確認(rèn)
007 007 007 00000111 BEL (Bell)蜂鳴
008 010 008 00001000 BS (Backspace)退格
009 011 009 00001001 HT (Horizontal Tab)
010 012 00A 00001010 LF (Line Feed)換行
011 013 00B 00001011 VT (Vertical Tab)
012 014 00C 00001100 FF (Form Feed)換頁/清屏
013 015 00D 00001101 CR (Carriage Return)回車
014 016 00E 00001110 SO (Shift Out)SHIFT 松開
015 017 00F 00001111 SI (Shift In)按下
016 020 010 00010000 DLE (Data Link Escape)清除
017 021 011 00010001 DC1 (XON) (Device Control 1)
018 022 012 00010010 DC2 (Device Control 2)
019 023 013 00010011 DC3 (XOFF)(Device Control 3)
020 024 014 00010100 DC4 (Device Control 4)
021 025 015 00010101 NAK (Negative Acknowledgement)
022 026 016 00010110 SYN (Synchronous Idle)
023 027 017 00010111 ETB (End of Trans. Block)
024 030 018 00011000 CAN (Cancel)
025 031 019 00011001 EM (End of Medium)
026 032 01A 00011010 SUB (Substitute)
027 033 01B 00011011 ESC (Escape)退出
028 034 01C 00011100 FS (File Separator)
029 035 01D 00011101 GS (Group Separator)
030 036 01E 00011110 RS (Request to Send/Record Separator)
031 037 01F 00011111 US (Unit Separator)
032 040 020 00100000 SP (Space)空格
033 041 021 00100001 ! (exclamation mark)
034 042 022 00100010 " (double quote)
035 043 023 00100011 # (number sign)
036 044 024 00100100 $ (dollar sign)
037 045 025 00100101 % (percent)
038 046 026 00100110 & (ampersand)
039 047 027 00100111 ' (single quote)
040 050 028 00101000 ( (left/opening parenthesis)
041 051 029 00101001 ) (right/closing parenthesis)
042 052 02A 00101010 * (asterisk)
043 053 02B 00101011 + (plus)
044 054 02C 00101100 , (comma)
045 055 02D 00101101 - (minus or dash)
046 056 02E 00101110 . (dot)
047 057 02F 00101111 / (forward slash)
048 060 030 00110000 0
049 061 031 00110001 1
050 062 032 00110010 2
051 063 033 00110011 3
052 064 034 00110100 4
053 065 035 00110101 5
054 066 036 00110110 6
055 067 037 00110111 7
056 070 038 00111000 8
057 071 039 00111001 9
058 072 03A 00111010 : (colon)
059 073 03B 00111011 ; (semi-colon)
060 074 03C 00111100 < (less than)
061 075 03D 00111101 = (equal sign)
062 076 03E 00111110 > (greater than)
063 077 03F 00111111 ? (question mark)
064 100 040 01000000 @ (AT symbol)
065 101 041 01000001 A
066 102 042 01000010 B
067 103 043 01000011 C
068 104 044 01000100 D
069 105 045 01000101 E
070 106 046 01000110 F
071 107 047 01000111 G
072 110 048 01001000 H
073 111 049 01001001 I
074 112 04A 01001010 J
075 113 04B 01001011 K
076 114 04C 01001100 L
077 115 04D 01001101 M
078 116 04E 01001110 N
079 117 04F 01001111 O
080 120 050 01010000 P
081 121 051 01010001 Q
082 122 052 01010010 R
083 123 053 01010011 S
084 124 054 01010100 T
085 125 055 01010101 U
086 126 056 01010110 V
087 127 057 01010111 W
088 130 058 01011000 X
089 131 059 01011001 Y
090 132 05A 01011010 Z
091 133 05B 01011011 [ (left/opening bracket)
092 134 05C 01011100 \ (back slash)
093 135 05D 01011101 ] (right/closing bracket)
094 136 05E 01011110 ^ (caret/cirumflex)
095 137 05F 01011111 _ (underscore)
096 140 060 01100000 `
097 141 061 01100001 a
098 142 062 01100010 b
099 143 063 01100011 c
100 144 064 01100100 d
101 145 065 01100101 e
102 146 066 01100110 f
103 147 067 01100111 g
104 150 068 01101000 h
105 151 069 01101001 i
106 152 06A 01101010 j
107 153 06B 01101011 k
108 154 06C 01101100 l
109 155 06D 01101101 m
110 156 06E 01101110 n
111 157 06F 01101111 o
112 160 070 01110000 p
113 161 071 01110001 q
114 162 072 01110010 r
115 163 073 01110011 s
116 164 074 01110100 t
117 165 075 01110101 u
118 166 076 01110110 v
119 167 077 01110111 w
120 170 078 01111000 x
121 171 079 01111001 y
122 172 07A 01111010 z
123 173 07B 01111011 { (left/opening brace)
124 174 07C 01111100 | (vertical bar)
125 175 07D 01111101 } (right/closing brace)
126 176 07E 01111110 ~ (tilde)
127 177 07F 01111111 DEL (delete)
前32個控制字符的詳細解釋(英文):
NUL (null)
SOH (start of heading)
STX (start of text)
ETX (end of text)
EOT (end of transmission) - Not the same as
ETBENQ (enquiry)ACK (acknowledge)
BEL (bell) - Caused teletype machines to ring a bell.
Causes a beep in many common terminals and terminal emulation programs.
BS (backspace) - Moves the cursor (or print head) move backwards (left) one space.
TAB (horizontal tab) - Moves the cursor (or print head) right to the next tab stop.
The spacing of tab stops is dependent on the output device, but is often either 8 or 10.
LF (NL line feed, new line) - Moves the cursor (or print head) to a new line. On Unix systems, moves to a new line AND all the way to the left.
VT (vertical tab)
FF (form feed) - Advances paper to the top of the next page (if the output device is a printer).
CR (carriage return) - Moves the cursor all the way to the left, but does not advance to the next line.
SO (shift out) - Switches output device to alternate character set.
SI (shift in) - Switches output device back to default character set.DLE (data link escape)
DC1 (device control 1)
DC2 (device control 2)
DC3 (device control 3)
DC4 (device control 4)
NAK (negative acknowledge)
SYN (synchronous idle)
ETB (end of transmission block) - Not the same as
EOTCAN (cancel)EM (end of medium)
SUB (substitute)ESC (escape)
FS (file separator)
GS (group separator)
RS (record separator)
US (unit separator)
常用的轉(zhuǎn)義字符
在windows自帶的超級終端中,如何清屏?
--通過發(fā)送0x0C(12)即可實現(xiàn)清屏。
--在“輸入字符串”內(nèi)容中輸入 \x0c\0 :字符串結(jié)束標(biāo)志;
--- 在上面的程序里找到 while(*str != '\0') ,您就會知道 \0 的功能了。它可以表示字符串的結(jié)束,您可以不用擔(dān)心指針會溢出。
\n :換行(asc II碼為10);
--- 一般和 \r 配合使用以代表回車。因為 \n 只是表示換行而并不讓光標(biāo)回到行首, \r 是實現(xiàn)了回行首的功能。
\t :橫向跳格;
\b :退格;
\r :回車(ascⅱ碼為13);
\f :走紙換頁;
\\ :字符\(ascⅱ碼為92);
--- \ 是轉(zhuǎn)義字符的表示符,當(dāng)只想打一個 \ 時就可以打兩個。
\' :單引號;
'\"':雙引號;
--- ' 在C里有特殊用法,所以這個表示只打印這個符號。
\d05:用8進制表示字符;
\x1c:用16進制表示字符
--- 有一些功能沒有轉(zhuǎn)義字符,我們就可以用這個指令直接表示指令。\x1c 相當(dāng)于發(fā)送 0x1c 對應(yīng)的ASC碼功能。
關(guān)于顏色
在 ANSI 兼容終端里,可以用彩色顯示文本而不僅僅是黑白。但是我們自己編寫的程序能否輸出彩色的字符呢?當(dāng)然答案是肯定的。下面的語句就輸出高亮的黑色背景的綠色字。
printf("\033[1;40;32m good!!! \033[0m Hello,NSFocus \n");
\033 聲明了轉(zhuǎn)義序列的開始,然后是 [ 開始定義顏色。后面的 1 定義了高亮顯示字符。然后是背景顏色,這里面是40,表示黑色背景。接著是前景顏色,這里面是32,表示綠色。我們用 \033[0m 關(guān)閉轉(zhuǎn)義序列, \033[0m 是終端默認(rèn)顏色。通過上面的介紹,就知道了如何輸出彩色字符了。因此,我就不再多說了。下面是對于彩色字符顏色的一些定義。
前景 背景 顏色
---------------------------------------
30 40 黑色
31 41 紅色
32 42 綠色
33 43 黃色
34 44 藍色
35 45 紫紅色
36 46 青藍色
37 47 白色
代碼 意義
-------------------------
0 終端默認(rèn)設(shè)置(黑底白字)
1 高亮顯示
4 使用下劃線
5 閃爍
7 反白顯示
8 不可見
尾聲
在嵌入式系統(tǒng)開發(fā)上,超級終端是相當(dāng)有用的家伙,許多DEBUG程序都是用串口的,這里了解的更多的是ASC II碼的一些特點。而了解這一些,使我們有許多想象。我們可以用超級終端來代替VB語言寫的上位機程序,電腦上不用安裝,只要有串口就行。可以實現(xiàn)數(shù)據(jù)采集的電腦顯示,或是用電腦串口控制下位機的設(shè)備。任君想來!文中如有錯誤歡迎批評、指正。一切為學(xué),多多交流。
|
|