IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    STM8 UART的使用,以及串口调试环境的建立(超精简代码)

    yoyo发表于 2016-01-11 08:00:00
    love 0

    学习arduino的时候只能使用串口来调试程序,STM8就有强大的ST-Link来调试,但是有时候还是需要串口来输出一些调试信息,我提取了stm8的官方uart1库函数,然后结合了我以前c语言调试函数宏的应用,写出一份代码,可以最快的速度建立起串口调试环境,支持printf、debug、error三种函数,炒鸡清爽,炒鸡好用有木有?

    #include <iostm8.h>
    
    #define _DLIB_FILE_DESCRIPTOR 1 //为了得到fputc函数的FILE类型定义
    #include <stdio.h>
    
    #define DEBUG //定义是否调试模式,如果没有定义的话,debug、error的调用代码不会编译进去
    
    
    #ifndef DEBUG
    #define debug(fmt, ...) ((void)0)
    #define error(fmt, ...) ((void)0)
    #else
    #define debug(fmt, ...)                    \
      printf("[DEBUG] %s:%d <%s>: " fmt "\n", \
              __FILE__, __LINE__, __func__, ##__VA_ARGS__)
    #define error(fmt, ...)                    \
      printf("[ERROR] %s:%d <%s>: " fmt "\n", \
              __FILE__, __LINE__, __func__, ##__VA_ARGS__)
    #endif
                                              
    //重定向c库函数printf到USART1
    int fputc(int ch, FILE *f) {
      /***代码来自:http://yoyo.play175.com/p/stm8-uart.html***/
      while(!(UART1_SR & 0X80));
      UART1_DR = (unsigned char) ch;
      while(!(UART1_SR & 0X40));
      return (ch);
    }
    
    //取得当前主时钟频率fMaster,系统上电后默认是2Mhz
    unsigned long clk_getfreq(void) {
      /***代码来自:http://yoyo.play175.com/p/stm8-uart.html***/
      if (CLK_CMSR == 0xE1)//HSI
      {
          return 16000000L / (unsigned char)(1 << CLK_CKDIVR_HSIDIV);
      }
      else if ( CLK_CMSR == 0xD2)//LSI
      {
          return 128000L;
      }
      else //HSE
      {
        #if defined (STM8S208) || defined (STM8S207) || defined (STM8AF52Ax) || defined (STM8AF62Ax)
         #define HSE_VALUE (24000000L) /* Value of the External oscillator in Hz*/
        #else
         #define HSE_VALUE (16000000L) /* Value of the External oscillator in Hz*/
        #endif /* STM8S208 || STM8S207 || STM8AF62Ax || STM8AF52Ax */
        return HSE_VALUE;
      }
    }
    
    //开启UART1
    void uart_begin(unsigned long baudrate) {
      /***代码来自:http://yoyo.play175.com/p/stm8-uart.html***/
      CLK_PCKENR1 |= 1<<2;     // UART1 Enable the clock
    
      UART1_CR1 = 0X00;
      UART1_CR2 = 0X00;
      UART1_CR3 = 0X00;   
    
      unsigned long clkfrq = clk_getfreq();//取得当前时钟频率
    
      unsigned long mantissa    = (clkfrq / (baudrate << 4));
      unsigned long mantissa100 = ((clkfrq * 100) / (baudrate << 4));
      UART1_BRR2 |= (unsigned char)((unsigned char)(((mantissa100 - (mantissa * 100)) << 4) / 100) & (unsigned char)0x0F); 
      UART1_BRR2 |= (unsigned char)((mantissa >> 4) & (unsigned char)0xF0); 
      UART1_BRR1 |= (unsigned char)mantissa; 
    
      UART1_CR2 = 0X2c; 
    }
       
    int main( void ) {
      
      uart_begin(115200);
      
      printf("=====uart test=====\n");
      
      //调试输出,可以输出文件名、行号 等信息
      debug("fMaster:%ld",clk_getfreq());
      
      error("got error here:%d",123);
      
      while(1) {
        
      }
    }
    

    输出截图:

    uart.png

    接线参考:

    以单片机STM8S103F3P6为例,芯片引脚定义如下图:

    stm8s103f.png

    请把串口转TTL线的RX线与芯片的UART1_TX(D5)引脚相连,TX线与芯片的UART1_RX(D6)引脚相连,然后执行uart_begin函数就可以使用串口发送数据了



沪ICP备19023445号-2号
友情链接