|
本程序是《MSP430系列單片機系統(tǒng)工程設(shè)計與實踐》書里面的源碼,完整例程下載(包含工程文件 (例4.1.5))):http://www.zg4o1577.cn/bbs/dpj-46245-1.html
‘
關(guān)于本程序的詳細解說大家可以下載電子書點擊上圖即可
- /*
-
- scanf函數(shù)移植按鈕輸入的范例程序
- 說明:在《超級終端人機對話范例》程序中,利用串口作為標(biāo)準(zhǔn)終端輸入/輸出設(shè)備
- 連接PC機,再通過Windows自帶"超級終端"軟件將PC機模擬成一個標(biāo)準(zhǔn)終端設(shè)備,
- 將printf函數(shù)的結(jié)果打印在PC機屏幕上,也能通過scanf函數(shù)從PC機鍵盤向單片
- 機輸入數(shù)據(jù)。
- scanf函數(shù)具體從何種設(shè)備輸入上由getchar()函數(shù)決定。本例示范將
- getchar()函數(shù)改寫成為從按鈕讀入一個字符,即可從按鈕輸入數(shù)據(jù)到變量。
- 這樣可以很方便的借助scanf函數(shù)強大的格式化輸入功能輸入各種復(fù)雜的信息。
- 這種將不同功能的函數(shù)做成相同且簡潔的接口形式,是C語言編程的基本
- 思想之一,也是C語言具有很強的通用性和移植性的原因。
- MAGIC430學(xué)習(xí)板上只有3個按鈕,為了輸入10個數(shù)字和其他字符,用KEY1來
- 選擇字符,KEY2確認輸入一個字符,KEY3作為回車。在實際應(yīng)用中,也會廣泛
- 采用類似的方法節(jié)省按鈕數(shù)量。當(dāng)然,如果按鍵數(shù)量足夠,程序可以簡化。
- GetChar.c文件里寫有新的getchar()函數(shù),原來在UART.c的getchar函數(shù)(
- 向串口發(fā)送字符)被注釋掉了。
-
- //=========================================================================
- (C)西安電子科技大學(xué) 測控技術(shù)與儀器教研中心 編寫:謝楷 2008/02/23
-
- */
- //******************************************************************************
- //
- //
- // MSP430FE425
- // +-----------------+
- // | XIN|-
- // | | 32kHz
- // +-KEY1 選擇---|P1.5 XOUT|-
- // | | |
- // +-KEY2 確認---|P1.6 (TXD)P2.4|-----------> // ----> RXD(2)
- // | | | 300~115200 bps
- // +-KEY3 回車---|P1.7 (RXD)P2.5|<----------- // <---- TXD(3) PC(DB9)
- // | | |
- // GND | GND |------------------------GND(5)
- //
- //******************************************************************************
- #include "msp430x42x.h" /*430寄存器頭文件*/
- #include "stdio.h" /*標(biāo)準(zhǔn)IO設(shè)備頭文件(printf/scanf函數(shù)所需)*/
- #include "LCD_Display.h" /*LCD顯示函數(shù)庫頭文件*/
- #include "BasicTimer.h" /*BasicTimer定時器函數(shù)庫*/
- #include "UART.h" /*串口函數(shù)庫*/
- long int a,b;
- void main( void )
- {
- WDTCTL=WDTPW+WDTHOLD; //停止看門狗
- FLL_CTL0|=XCAP18PF; //配置晶振負載電容
- P1DIR|=BIT0+BIT1+BIT2+BIT3+BIT4;
- P2DIR|=BIT0+BIT1+BIT2+BIT3; //不用的IO設(shè)為輸出
- P1OUT=0;P2OUT=0; //以免不確定電平造成額外耗電
- BT_Init(16); //BT 1/8秒中斷一次
- UART_Init(4800,'n',8,1); //串口設(shè)置4800bps,無校驗,8位數(shù)據(jù),1位停止
- while(1)
- {
- printf("\n----------演示開始!------------\n\n");//打印到超級終端
- printf("請輸入一個數(shù)字A:"); //打印到超級終端
- scanf("%ld",&a); //從按鈕輸入一個數(shù)A
- printf("\n請輸入一個數(shù)字B:");//打印到超級終端
- scanf("%ld",&b); //從按鈕輸入一個數(shù)B
- printf("\n%ld+%ld=%ld\n",a,b,a+b); //將A+B的結(jié)果打印出來
- printf("\n----------演示結(jié)束!------------\n\n");//打印到超級終端
- getchar(); //按任意鍵重新開始
- }
- }
復(fù)制代碼
puchar.c
- #include "msp430x42x.h" /*單片機寄存器頭文件*/
- #include "ctype.h" /*isdigit函數(shù)需要該頭文件*/
- #include "LCD_Display.h" /*LCD函數(shù)庫頭文件*/
- char FirstChrFlag=1; //第一個字符標(biāo)志位
- /****************************************************************************
- * 名 稱:putchar()
- * 功 能:向LCD顯示屏輸出一個ASCII字符。
- * 入口參數(shù):ch: 待發(fā)送的字符
- * 出口參數(shù):發(fā)出的字符
- * 說 明: printf函數(shù)會調(diào)用該函數(shù)作為底層輸出。該函數(shù)將字符輸出到LCD上
- 因此printf的結(jié)果將顯示在LCD上。
- ****************************************************************************/
- int putchar(int ch)
- {
- if(FirstChrFlag) LCD_Clear(); //第一個字符到來的時候清除上一屏顯示內(nèi)容
- FirstChrFlag=0;
- if(ch=='\f') LCD_Clear(); //'\f'表示走紙翻頁,相當(dāng)于清除顯示
- if(isdigit(ch)) LCD_InsertChar(ch-0x30); //若字符是數(shù)字則顯示數(shù)字
- //數(shù)字和對應(yīng)ASCII字母之間差0x30 '1'=0x31 '2'=0x32... isdigit也是C語言標(biāo)準(zhǔn)函數(shù)
- else //否則,不是數(shù)字,是字母
- {
- switch(ch) //根據(jù)字母選擇程序分支
- {
- case 'A': case 'a': LCD_InsertChar(AA);break; //字符A
- case 'B': case 'b': LCD_InsertChar(BB);break; //字符B
- case 'C': case 'c': LCD_InsertChar(CC);break; //...
- case 'D': case 'd': LCD_InsertChar(DD);break;
- case 'E': case 'e': LCD_InsertChar(EE);break;
- case 'F': case 'f': LCD_InsertChar(FF);break;
- case 'G': case 'g': LCD_InsertChar(GG);break;
- case 'H': case 'h': LCD_InsertChar(HH);break;
- case 'I': case 'i': LCD_InsertChar(II);break;
- case 'J': case 'j': LCD_InsertChar(JJ);break;
- case 'K': case 'k': LCD_InsertChar(KK);break;
- case 'L': case 'l': LCD_InsertChar(LL);break;
- case 'M': case 'm': LCD_InsertChar(mm);break;
- case 'N': LCD_InsertChar(NN);break;
- case 'n': LCD_InsertChar(nn);break;
- case 'O': LCD_InsertChar(OO);break;
- case 'o': LCD_InsertChar(oo);break;
- case 'P': case 'p': LCD_InsertChar(PP);break;
- case 'Q': case 'q': LCD_InsertChar(QQ);break;
- case 'R': case 'r': LCD_InsertChar(rr);break;
- case 'S': case 's': LCD_InsertChar(SS);break;
- case 'T': case 't': LCD_InsertChar(tt);break;
- case 'U': case 'v': LCD_InsertChar(UU);break;
- case 'V': case 'u': LCD_InsertChar(VV);break;
- case 'W': case 'w': LCD_InsertChar(WW);break;
- case 'Y': case 'y': LCD_InsertChar(YY);break; //...
- case 'Z': case 'z': LCD_InsertChar(ZZ);break; //字符Z
- case '-': LCD_InsertChar(BR);break; //字符-
- case '`': LCD_InsertChar(DT);break; //字符`
- case ' ': LCD_InsertChar(SP);break; //空格
- case '.': LCDM1|=0x10; break; //小數(shù)點,直接顯示在右下角
- case '\n': case '\r': FirstChrFlag=1; break; //換行符的下一個字母將清屏
- default : LCD_InsertChar(SP);break;//顯示不出來的字母用空格替代
- }
- }
- return(ch); //返回顯示的字符(putchar函數(shù)標(biāo)準(zhǔn)格式要求返回顯示字符)
- }
- /****************************************************************************
- * 名 稱:putchar()
- * 功 能:向標(biāo)準(zhǔn)終端設(shè)備發(fā)送一字節(jié)數(shù)據(jù)(1個ASCII字符)
- * 入口參數(shù):ch: 待發(fā)送的字符
- * 出口參數(shù):發(fā)出的字符
- * 說 明: UART.c內(nèi)的putchar函數(shù)printf函數(shù),這里從串口輸出字符到PC機的超
- 級終端軟件上,printf的結(jié)果將打印到超級終端上。供對比。
- ****************************************************************************/
- /*
- int putchar(int ch)
- {
- if (ch == '\n') // '\n'(回車)擴展成 '\n''\r' (回車+換行)
- {
- UART_PutChar(0x0d) ; //'\r'
- }
- UART_PutChar(ch); //從串口發(fā)出數(shù)據(jù)
- return (ch);
- }
- */
復(fù)制代碼
|
|