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

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STM32串口通信中使用printf發(fā)送數(shù)據(jù)配置方法

          STM32串口通信中使用printf發(fā)送數(shù)據(jù)配置方法

          作者: 時(shí)間:2016-11-19 來源:網(wǎng)絡(luò) 收藏
          STM32串口通信程序中使用printf發(fā)送數(shù)據(jù),非常的方便??稍趧傞_始使用的時(shí)候總是遇到問題,常見的是硬件訪真時(shí)無法進(jìn)入main主函數(shù),其實(shí)只要簡(jiǎn)單的配置一下就可以了。
          下面就說一下使用printf需要做哪些配置。
          有兩種配置方法
          一、對(duì)工程屬性進(jìn)行配置,詳細(xì)步驟如下
          1、首先要在你的main 文件中 包含“stdio.h” (標(biāo)準(zhǔn)輸入輸出頭文件)。
          2、在main文件中重定義函數(shù) 如下:
          // 發(fā)送數(shù)據(jù)
          intfputc(int ch, FILE *f)
          {
          USART_SendData(USART1, (unsigned char) ch);// USART1 可以換成 USART2 等
          while (!(USART1->SR & USART_FLAG_TXE));
          return (ch);
          }
          // 接收數(shù)據(jù)
          int GetKey (void) {
          while (!(USART1->SR & USART_FLAG_RXNE));
          return ((int)(USART1->DR & 0x1FF));
          }
          這樣在使用printf時(shí)就會(huì)調(diào)用自定義的fputc函數(shù),來發(fā)送字符。
          3、在工程屬性的 “Target" -> "Code Generation" 選項(xiàng)中勾選 "Use MicroLIB"”
          MicroLIB 是缺省C的備份庫,關(guān)于它可以到網(wǎng)上查找詳細(xì)資料。
          至此完成配置,在工程中可以隨意使用printf向串口發(fā)送數(shù)據(jù)了。
          二、第二種方法是在工程中添加“Regtarge.c”文件
          1、在main文件中包含 “stdio.h” 文件
          2、在工程中創(chuàng)建一個(gè)文件保存為 Regtarge.c, 然后將其添加工程中
          在文件中輸入如下內(nèi)容(直接復(fù)制即可)
          #include
          #include
          #pragma import(__use_no_semihosting_swi)
          extern int SendChar(int ch); // 聲明外部函數(shù),在main文件中定義
          extern int GetKey(void);
          struct __FILE {
          int handle; // Add whatever you need here
          };
          FILE __stdout;
          FILE __stdin;
          int fputc(int ch, FILE *f) {
          return (SendChar(ch));
          }
          int fgetc(FILE *f) {
          return (SendChar(GetKey()));
          }
          void _ttywrch(int ch) {
          SendChar (ch);
          }
          int ferror(FILE *f) { // Your implementation of ferror
          return EOF;
          }
          void _sys_exit(int return_code) {
          label: goto label; // endless loop
          }
          3、在main文件中添加定義以下兩個(gè)函數(shù)
          int SendChar (int ch) {
          while (!(USART1->SR & USART_FLAG_TXE)); // USART1可換成你程序中通信的串口
          USART1->DR = (ch & 0x1FF);
          return (ch);
          }
          int GetKey (void) {
          while (!(USART1->SR & USART_FLAG_RXNE));
          return ((int)(USART1->DR & 0x1FF));
          }
          至此完成配置,可以在main文件中隨意使用 printf 。



          關(guān)鍵詞: STM32串口通信配置方

          評(píng)論


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