Single-chip microcomputer engineering adding printf function

Source: Internet
Author: User

Before learning to use the Alientek MiniSTM32 Development Board, the use of the development environment can be integrated with the printf function through a special configuration, debugging the program is not like to use the emulator, the recent project began to use a single-chip microcomputer, only enough uart1_send (uint8_t * Const tx_buf,uint16_t Tx_num), really a bit not accustomed to the day without printf function, the most convenient program debugging is the printf function, so began to study how to add the printf function in the microcontroller.

The function Put_char (uint8_t data)related to the serial port driver needs to be changed according to the different embedded platform, because the Put_char () function simply sends a byte serial operation function, So the basic principle is to send a new data byte A1 before you need to check the string before a send byte A0 has been sent, only A0 has been sent, only to be able to write the new byte data A1 to the serial port send register, the same A2 send also need to wait for A1 send complete.

Va_start () and other definitions in the header file Stdarg.h, stdio.h, stdlib.h, so need to add these header files, these headers are compiler include path, I use the IAR development environment, generally use <> Contains header files, the compiler will automatically search for these header files in its installation directory, if "X.h" is used to locate the X.h file on the project path
1#include <stdio.h>2#include <stdlib.h>3#include <stdarg.h>4 5 Static voidPut_char (uint8_t data)6 {  7     if(Data = ='/ R')  8Put_char (0x09); 9      while( ! (Ucsra & (1<<udre)))//not empty, etc.Ten         ;  OneUDR = data;//sending data to the serial port send register A }   -    - voidmy_printf (Const Char*FMT,...)  the {   -     Const Char*s;  -     intD;  -     Charbuf[ -];  + va_list ap;  -Va_start (AP,FMT);//Point the AP to the FMT (that is, the first of the mutable parameters?) next? ) +      while(*FMT) A     {   at         if(*fmt! ='%')   -         {   -Put_char (*fmt++);//Normal Send -             Continue;  -         }   -         Switch(*++FMT)//Next% in         {   -          Case 's':   tos = Va_arg (AP,Const Char*);//Turn the AP pointer into the char* type and return the +              for(; *s; s++)   -Put_char (*s);  the              Break;  *          Case 'x':   $D = Va_arg (AP,int);//Turn the AP pointer to int and returnPanax NotoginsengItoa (D,buf, -);//convert int D to buf with 16 binary -              for(s = buf; *s; s++)   thePut_char (*s);  +              Break;  A          Case 'D':   theD = Va_arg (AP,int);  +Itoa (D,buf,Ten);//convert int D to buf with 10 binary -              for(s = buf; *s; s++)   $Put_char (*s);  $              Break;  -         default:   -Put_char (*FMT);  the              Break;  -         }  Wuyifmt++;  the     }   - Va_end (AP);  Wu}

Some development environments may not have the itoa () function, where the prototype of the ITOA () function is found from the forum, and if the compiler hints that the ITOA function cannot be found, you can copy the following code into the file where the printf function is located, which is tested and available.

1 Char*itoa (intNumChar*STR,intRadix)2 {     3     /*Index Table*/4     Charindex[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 5Unsigned unum;/*Intermediate Variables*/6     intI=0, J,k;/*determine the value of the Unum*/ 7     if(radix==Ten&&num<0)/*decimal Negative*/8     {     9unum= (unsigned)-num;Tenstr[i++]='-';  One     } A      Else  -unum= (unsigned) num;/*Other conditions*/    /*Reverse*/ -      Do{          thestr[i++]=index[unum%(unsigned) radix];  -Unum/=Radix;  -} while(unum); -str[i]=' /';/*Conversion*/ +     if(str[0]=='-')  -k=1;/*decimal Negative*/  +     Else  Ak=0; at     Chartemp; -      for(J=k;j<= (i1)/2; j + +) -      {          -temp=Str[j]; -STR[J] = str[i-1+k-J];  -str[i-1+K-J] =temp;  in     }  -     returnstr; to}

Finally, you can add code to the main function to test

uint8_t buffer[]= "Hello World";

uint8_t x=0x64;

my_printf ("Test%s,%d,0x%x\r\n", buffer,x,x);

The final output is

Test Hello World,100,0x64

Single-chip microcomputer engineering adding printf function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.