stm32串口模拟printf

没有闲话,直接上代码

库函数

#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 ) ;
		

	}
	
}

输出结果``
`
在这里插入图片描述
目前看着框架应该没有什么问题,需要其余输出显示的可以直接在框架里自行添加

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值