Bootstrap

单片机串口打印printf函数显示内容(固件库开发)

1.hal_usart.c 文件

#include <stdio.h>
#include "hal_usart.h"
#include "stm32F10x.h"


//**要根据 使用的是哪个串口 对应修改 串口号   eg:USART1**
void USART_PUTC(char ch)
{
    /* 等待数据寄存器为空 */
    while((USART1->SR & 0x40) == 0);
    /* 写入数据寄存器 */
    USART1->DR = (uint8_t)ch;
}
//重指向

#pragma import(__use_no_semihosting)

struct __FILE
{
		int handle;
};

FILE __stdout;

void _sys_exit(int x)
{
	x = x;
}

int fputc(int ch, FILE *f)
{
    if (ch == '\n')
    {
        USART_PUTC('\r');
    }
    USART_PUTC(ch);
    return ch;
}

static void hal_usart_Config(void);

void hal_UsartInit()
{

	hal_usart_Config();

	printf("FreeRtos教程学习\n\r");
}


static void hal_usart_Config(void)
{ 
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure; 
//	NVIC_InitTypeDef NVIC_InitStructure;
 	RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA,ENABLE);
 	RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1,ENABLE);

/****************GPIO Setting *******************************************/
//  USART1_TX -> PA9 , 		
	GPIO_InitStructure.GPIO_Pin = DEBUF_TX_PIN;	         
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(DEBUG_TX_PORT, &GPIO_InitStructure);		   
//  USART1_RX ->	PA10
	GPIO_InitStructure.GPIO_Pin = DEBUF_RX_PIN;	        
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
	GPIO_Init(DEBUG_RX_PORT, &GPIO_InitStructure);

	USART_InitStructure.USART_BaudRate = 115200;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	
	USART_Init(DEBUG_USART_PORT, &USART_InitStructure); 

    // NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//
	// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;		//
	// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//
	// NVIC_Init(&NVIC_InitStructure);	//


    // USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//
	// USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//
 	// USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//
 	// USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);//
	
	USART_Cmd(USART1, ENABLE);                    ///
}

//
///
void USART1_IRQHandler(void)
{
    //unsigned char dat;
	if(USART_GetITStatus(USART1,USART_IT_RXNE) != RESET)
	{							
		 //dat = USART_ReceiveData(USART1);
		 USART_ClearITPendingBit(USART1,USART_IT_RXNE);
	}
}



2. hal_usar.h 文件

说明:要根据不同单片串口引脚的不同,来改变修改对应引脚

#ifndef _HAL_USART_H
#define _HAL_USART_H


#define DEBUG_TX_PORT	GPIOA         //根据使用串口的 RX,TX 引脚的不同来修改头文件即可
#define DEBUF_TX_PIN	GPIO_Pin_9
#define DEBUG_RX_PORT	GPIOA
#define DEBUF_RX_PIN	GPIO_Pin_10
#define DEBUG_USART_PORT	USART1    //根据利用哪个串口来做Debug口  这里就设置为哪个串口

void hal_UsartInit(void);

#endif


3. 后续使用printf函数时,加入对应头文件,就能显示想要显示的内容

#include <stdio.h>

;