<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > MSP430G2553單片機(jī)使用printf函數(shù)進(jìn)行串口打印輸出

          MSP430G2553單片機(jī)使用printf函數(shù)進(jìn)行串口打印輸出

          作者: 時間:2016-11-13 來源:網(wǎng)絡(luò) 收藏
          以前在使用keil編寫51單片機(jī)程序的時候,經(jīng)常使用printf函數(shù)打印輸出一些關(guān)鍵的過程數(shù)據(jù)到電腦,方便監(jiān)控程序的運行狀況。最近使用IAR for MSP430 調(diào)試MSP430G2553程序的時候,發(fā)現(xiàn)了一些小問題,MSP430G2553單片機(jī)并沒有按照我的預(yù)期輸出數(shù)據(jù)到電腦。
          帶著疑惑我查看了keil的幫助文件里面的printf函數(shù)說明,原來printf函數(shù)最終是調(diào)用putchar函數(shù)來實現(xiàn)打印輸出字符的。
          putchar,該函數(shù)將制定的表達(dá)式的值所對應(yīng)的字符輸出到標(biāo)準(zhǔn)輸出終端上。表達(dá)式可以是字符型或整型,它每次只能輸出一個字符。我們來看keil標(biāo)準(zhǔn)函數(shù)庫里的putchar函數(shù)的函數(shù)體。
          根據(jù)說明提示,我在D:KeilC51LIB文件夾里面找到了putchar.c文件
          #include
          #define XON 0x11
          #define XOFF 0x13
          /*
          * putchar (full version): expands n into CR LF and handles
          * XON/XOFF (Ctrl+S/Ctrl+Q) protocol
          */
          char putchar (char c) {
          if (c == n) {
          if (RI) {
          if (SBUF == XOFF) {
          do {
          RI = 0;
          while (!RI);
          }
          while (SBUF != XON);
          RI = 0;
          }
          }
          while (!TI);
          TI = 0;
          SBUF = 0x0d; /* output CR */
          }
          if (RI) {
          if (SBUF == XOFF) {
          do {
          RI = 0;
          while (!RI);
          }
          while (SBUF != XON);
          RI = 0;
          }
          }
          while (!TI);
          TI = 0;
          return (SBUF = c);
          }
          #if 0 // comment out versions below
          /*
          * putchar (basic version): expands n into CR LF
          */
          char putchar (char c) {
          if (c == n) {
          while (!TI);
          TI = 0;
          SBUF = 0x0d; /* output CR */
          }
          while (!TI);
          TI = 0;
          return (SBUF = c);
          }
          /*
          * putchar (mini version): outputs charcter only
          */
          char putchar (char c) {
          while (!TI);
          TI = 0;
          return (SBUF = c);
          }
          #endif
          由說明文件可以看出,我們可以改寫這個底層的putchar函數(shù)來適應(yīng)不同的硬件。keil里面的putchar函數(shù)是默認(rèn)用串行口輸出信息的,我們可以自由定義成另外的輸出模塊,比如自定義IO向1602液晶輸出信息。
          keil的printf函數(shù)大致搞明白了,回頭再研究下IAR for MSP430,可惜,我沒有能夠查看該軟件標(biāo)準(zhǔn)函數(shù)庫里的printf.c 和 putchar.c,不過我覺得程序沒有通過串口向電腦打印輸出信息,是因為底層的putchar函數(shù)沒有定義為通過MSP430G2553的UART進(jìn)行輸出,那如果我自己重定向一個putchar函數(shù),覆蓋掉標(biāo)準(zhǔn)函數(shù)庫里面的putchar,是不是就能夠輸出了呢。于是編寫putchar函數(shù)如下:
          /*********************************************************************
          * 函數(shù)名 : putchar,函數(shù)重定向,自動覆蓋標(biāo)準(zhǔn)庫函數(shù)
          * 函數(shù)功能 : 向串口終端發(fā)送一個字符
          * 形參 : c為待發(fā)送的字符
          * 返回值 : c
          *********************************************************************/
          int putchar(int c)
          {
          if(c == n)
          {
          while(UCA0STAT & UCBUSY);
          UCA0TXBUF = r;
          }
          while(UCA0STAT & UCBUSY);
          UCA0TXBUF = c;
          return c;
          }
          編譯后,輸出完全正確。大功告成。
          將測試程序向大家展示一下,希望能幫到大家。
          ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
          uart.c
          ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
          #include <msp430g2553.h>
          /**********************************************************************
          * 函數(shù)名 : UartInit
          * 函數(shù)功能 : 初始化msp430g2553的USCI寄存器,使其工作在UART模式
          * 形參 : 無
          * 返回值 : 無
          * ********************************************************************/
          void UartInit()
          {
          BCSCTL1 = CALBC1_1MHZ; // Set DCO
          DCOCTL = CALDCO_1MHZ;
          BCSCTL2 &= ~(DIVS_3);
          P1SEL = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
          P1SEL2 = BIT1 + BIT2; // P1.1 = RXD, P1.2=TXD
          UCA0CTL1 |= UCSSEL_2; // SMCLK
          UCA0BR0 = 104; // 1MHz 9600
          UCA0BR1 = 0; // 1MHz 9600
          UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
          UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
          UC0IE |= UCA0RXIE; // Enable RX int
          }
          /*********************************************************************
          * 函數(shù)名 : putchar,函數(shù)重定向,自動覆蓋標(biāo)準(zhǔn)庫函數(shù)
          * 函數(shù)功能 : 向串口終端發(fā)送一個字符
          * 形參 : c為待發(fā)送的字符
          * 返回值 : c
          *********************************************************************/
          int putchar(int c)
          {
          if(c == n)
          {
          while(UCA0STAT & UCBUSY);
          UCA0TXBUF = r;
          }
          while(UCA0STAT & UCBUSY);
          UCA0TXBUF = c;
          return c;
          }
          ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
          main.c
          ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
          #include
          #include "uart.h"
          #include "stdio.h"
          void main()
          {
          float value = 123.123456789;
          char *string="http://www.hao123.com";
          WDTCTL = WDTPW + WDTHOLD;
          UartInit();
          printf("value = %fn%sn",value,string);
          while(1);
          }
          信息打印輸出到電腦的超級終端,截圖如下:



          評論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();