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

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 單片機(jī)程序巧用printf

          單片機(jī)程序巧用printf

          作者: 時間:2016-12-01 來源:網(wǎng)絡(luò) 收藏
          當(dāng)我們在調(diào)試代碼時,通常需要將程序中的某個變量打印至PC機(jī)上,來判斷我們的程序是否按預(yù)期的運行,printf函數(shù)很好的做到了這一點,它能直接以字符的方式輸出變量名和變量的值。
          printf函數(shù)在使用時,不僅僅要初始化串口,還需要其它的一些設(shè)置或者要調(diào)用其它的一些函數(shù) 否則printf函數(shù)將不能按我們想要的方式執(zhí)行。
          由于不同的編譯器studio函數(shù)不一樣,所以使用的方法也不一樣,這需要大家去看編譯器的help幫助選項,這里我們以STM32、51和AVR整理了幾個串口打印程序,供需要的朋友參考。
          1、在KEIL下使用printf函數(shù),以STM32為例
          uart.c中添加如下代碼
          View Code
          /*******************************************************************************
          函數(shù)名:fputc
          輸 入:
          輸 出:
          功能說明:
          重定義putc函數(shù),這樣可以使用printf函數(shù)從串口1打印輸出
          *******************************************************************************/
          int fputc(int ch, FILE *f)
          {
          /* Place your implementation of fputc here */
          /* e.g. write a character to the USART */
          USART_SendData(USART1, (uint8_t) ch);
          /* Loop until the end of transmission */
          while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
          {}
          return ch;
          }
          /*******************************************************************************
          函數(shù)名:fputc
          輸 入:
          輸 出:
          功能說明:
          重定義getc函數(shù),這樣可以使用scanff函數(shù)從串口1輸入數(shù)據(jù)
          ******************************************************************************/
          int fgetc(FILE *f)
          {
          /* 等待串口1輸入數(shù)據(jù) */
          while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
          {}
          return (int)USART_ReceiveData(USART1);
          }
          這樣,只要在需要用printf的文件里#include 就可以了,printf會自已的調(diào)用fputc函數(shù)來實現(xiàn)串口數(shù)據(jù)的輸出。
          2、添加Retarget.c,實現(xiàn)在KEIL下使用printf函數(shù),以LPC2478為例
          首先在Keil安裝目錄下面ARM/Startup/Retarget.c找到Retarget.c文件將其復(fù)制到你的工程文件夾下面;并將其加入到工程中
          在uart.c中添加如下代碼
          View Code
          // Implementation of sendchar (also used by printf function to output data)
          int sendchar (int ch) { // Write character to Serial Port
          while (!(U0LSR & 0x20));
          return (U0THR = ch);
          }
          int getkey (void) { // Read character from Serial Port
          while (!(U0LSR & 0x01));
          return (U0RBR);
          }
          這樣,只要在需要用printf的文件里#include 就可以了,printf會通過Retarget中的fputc函數(shù)調(diào)用sendchar來實現(xiàn)串口數(shù)據(jù)的輸出。
          事實上,和第一種的方式是一樣的。
          3、自定義printf函數(shù),以AVR為例
          前面介紹的是在KEIL編譯器上使用printf函數(shù),但不是所有的編譯器平臺都能適用,因此有時候我們需要自定義printf函數(shù),下面以AVR在GCC下為例
          在usart.c中添加如下代碼
          View Code
          #include
          #include
          /*********************************************************/
          //向串口usart0發(fā)送一個字節(jié)函數(shù)
          void Uart0_putchar( unsigned char sdbyte)
          {
          UDR0=sdbyte;
          while(!(UCSR0A&0x40));
          UCSR0A|=0x40;
          }
          ////////////////////////////////////////////////////////
          void Uart0_printf(char *str,...)
          {
          char buf[128];
          unsigned char i = 0;
          va_list ptr;
          va_start(ptr,str);
          vsprintf(buf,str,ptr);
          while(buf[i])
          {
          Uart0_putchar(buf[i]);
          i++;
          }
          }
          這樣有了printf格式化輸出函數(shù),隨時能把需要的變量打印到pc機(jī)或液晶上,調(diào)試起來就方便多了。


          關(guān)鍵詞: 單片機(jī)程序print

          評論


          技術(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); })();