没有闲话,直接上代码
库函数
#include "mine_function.h"
#include "stdarg.h"
#include "string.h"
#include "stdio.h"
#include "stm32f10x.h" // Device header
#include "math.h"
void data_out ( uint8_t data )
{
USART_SendData (USART2, data ) ;
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
}
int my_printf(const char *format, ...)
{
va_list ap;
char c;
uint8_t counter = 0 ;
uint8_t number;
uint16_t value ;
uint8_t i ;
uint16_t value1;
char * s_add ;
va_start(ap, format);
while( *format != 0 )
{
if (*format == '%')
{
format++;
c = *format;
switch(c)
{
case 'd':
{
value = va_arg(ap, int);
number = value ;
while ( number != 0 )
{
number = number / 10 ;
counter++ ;
}
while ( counter != 0 )
{
value1 = value % (uint16_t)(pow ( 10 , counter -1 )) ;
data_out ( (value / (uint16_t)(pow ( 10 , counter -1 )) ) + '0' ) ;
value = value1 ;
counter--;
}
break;
}
case 'X':
{
value = va_arg(ap, int);
number = value ;
while ( number != 0 )
{
number = number / 16 ;
counter++ ;
}
if ( (counter % 2) != 0 )
data_out ( '0' ) ;
while ( counter != 0 )
{
value1 = value % (uint16_t)(pow ( 16 , counter -1 )) ;
value = value / (uint16_t)(pow ( 16 , counter -1 )) ;
if ( value > 9 )
{
value = value - 10 + 'A' ;
}
else
{
value = value + '0' ;
}
data_out ( value ) ;
value = value1 ;
counter--;
}
break;
}
case 's':
s_add = va_arg(ap, char *);
while ( *s_add != 0 )
{
data_out ( *s_add ) ;
s_add++;
}
break;
default:
{
printf("format error");
return -1;
}
}
}
else if ( c == '\r' ) data_out ( 0x0d ) ; //回车符
else if ( c == '\n' ) data_out ( 0x0a) ; //换行符
else data_out ( *format ) ;
format++;
}
return 0 ;
}
主函数
```c
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include <stdio.h>
#include "USART1.h"
#include "USART2.h"
#include "mine_function.h"
int main ()
{
char c[] = "acv23fhgcfrg";
uint8_t i = 0x64 ;
NVIC_USART2_Configuration () ;
USART2_INIT ( ) ;
USART2_GPIO ( ) ;
uint8_t data_len ;
while ( 1 )
{
my_printf (" i = %d\r\n", i+1 );
my_printf (" %s\r\n",c );
my_printf (" i = 0x%X\r\n",i );
Delay_s ( 2 ) ;
}
}
输出结果``
`
目前看着框架应该没有什么问题,需要其余输出显示的可以直接在框架里自行添加