1. 程式人生 > 實用技巧 >移植msp430中的printf函式

移植msp430中的printf函式

程式碼:

#include "msp430x42x.h"  /*微控制器暫存器標頭檔案*/
#include "ctype.h"       /*isdigit函式需要該標頭檔案*/
#include "LCD_Display.h" /*LCD函式庫標頭檔案*/
char  FirstChrFlag=1;  //第一個字元標誌位


/****************************************************************************
* 名    稱:putchar()
* 功    能:向LCD顯示屏輸出一個ASCII字元。
* 入口引數:ch: 待發送的字元  
* 出口引數:發出的字元
* 說    明: printf函式會呼叫該函式作為底層輸出。該函式將字元輸出到LCD上
            因此printf的結果將顯示在LCD上。
***************************************************************************
*/ int putchar(int ch) { if(FirstChrFlag) LCD_Clear(); //第一個字元到來的時候清除上一屏顯示內容 FirstChrFlag=0; if(ch=='\f') LCD_Clear(); //'\f'表示走紙翻頁,相當於清除顯示 if(isdigit(ch)) LCD_InsertChar(ch-0x30); //若字元是數字則顯示數字 //數字和對應ASCII字母之間差0x30 '1'=0x31 '2'=0x32... isdigit也是C語言標準函式 else //否則,不是數字,是字母 { switch(ch) //根據字母選擇程式分支
{ 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; //小數點,直接顯示在右下角 case '\n': case '\r': FirstChrFlag=1; break; //換行符的下一個字母將清屏 default : LCD_InsertChar(SP);break;//顯示不出來的字母用空格替代 } } return(ch); //返回顯示的字元(putchar函式標準格式要求返回顯示字元) } /**************************************************************************** * 名 稱:putchar() * 功 能:向標準終端裝置傳送一位元組資料(1個ASCII字元) * 入口引數:ch: 待發送的字元 * 出口引數:發出的字元 * 說 明: UART.c內的putchar函式printf函式,這裡從串列埠輸出字元到PC機的超 級終端軟體上,printf的結果將列印到超級終端上。供對比。 ****************************************************************************/ /* int putchar(int ch) { if (ch == '\n') // '\n'(回車)擴充套件成 '\n''\r' (回車+換行) { UART_PutChar(0x0d) ; //'\r' } UART_PutChar(ch); //從串列埠發出資料 return (ch); } */