串口打印数据输出

使用MDK的情况

1.使用printf库函数时,要加入头文件<stdio.h>

2.另外在keil里面需要把:use MicroLIB 勾选上,不然程序没办法在线调试。编译的时候不会报错。

3.当然可以不用库函数,自己写。正点原子里面的代码:

 //加入以下代码,支持printf函数,而不需要选择use MicroLIB

#if 1
 #pragma import(__use_no_semihosting) 
 //标准库需要的支持函数 
struct __FILE 
{ 
  int handle;

};

FILE __stdout; 
//定义_sys_exit()以避免使用半主机模式 
_sys_exit(int x) 
{ 
  x = x; 
} 
//重定义fputc函数 
int fputc(int ch, FILE *f)
{ 
  while((USART1->SR&0X40)==0);//循环发送,直到发送完毕 
  USART1->DR = (u8) ch; 
  return ch;
}
#endif


/*使用microLib的方法*/

/* 
int fputc(int ch, FILE *f)
{
  USART_SendData(USART1, (uint8_t) ch);

  while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}	
  return ch;
}
int GetKey (void)

{

  while (!(USART1->SR & USART_FLAG_RXNE));

  return ((int)(USART1->DR & 0x1FF));
}
*/


不使用printf 自己使用vsprint函数

#include <stdarg.h>

uint8_t g_uartPrintStr[256];
void shellPrint(const char *fmt, ...)
{
    va_list ap;
    int i;
	
    va_start(ap, fmt);
    vsprintf((char *)g_uartPrintStr, fmt, ap);
	
    va_end(ap);


    for (i = 0; i < 128; i++)
    {
        if (g_uartPrintStr[i] == '\0')
        {
             break;
        }
    }


    shellSnd((uint8_t *)g_uartPrintStr , i);
}


再增加一个:

/*******************************************************************************
* Function Name  : int fputc(int ch, FILE *f)
* Description    : Retargets the C library printf function to the USART.printfÖض¨Ïò
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
//int fputc(int ch, FILE *f)
//{
//  UART_SendByte(UART_0, (uint8_t) ch); /* Write a character to the USART */
//  while((LPC_UART0->LSR&0x40)==0){}; /* Loop until the end of transmission */
//  return ch;
//}

/*******************************************************************************
* Function Name  : int fgetc(FILE *f)
* Description    : Retargets the C library printf function to the USART.fgetcÖض¨Ïò
* Input          : None
* Output         : None
* Return         : Read a character from the USART
*******************************************************************************/
//int fgetc(FILE *f)
//{
//  while((LPC_UART0->LSR&0x1)==0){};//* Loop until received a char */
//  return (UART_ReceiveByte(UART_0));/* Read a character from the USART and RETURN */
//}


  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在STM32上,串口输出可以通过调用HAL库的函数来实现。以下是一个简单的例子: ```c #include "stm32f1xx_hal.h" #include "stdio.h" UART_HandleTypeDef huart1; // 定义串口句柄 void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_USART1_UART_Init(void); int main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_USART1_UART_Init(); char buffer[50]; // 定义一个字符串缓冲区 sprintf(buffer, "Hello World!\r\n"); // 将字符串格式化为缓冲区 while (1) { HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), HAL_MAX_DELAY); // 通过串口发送字符串 HAL_Delay(1000); // 延时1秒钟 } } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) { Error_Handler(); } } /** * @brief USART1 Initialization Function * @param None * @retval None */ static void MX_USART1_UART_Init(void) { huart1.Instance = USART1; huart1.Init.BaudRate = 115200; huart1.Init.WordLength = UART_WORDLENGTH_8B; huart1.Init.StopBits = UART_STOPBITS_1; huart1.Init.Parity = UART_PARITY_NONE; huart1.Init.Mode = UART_MODE_TX_RX; huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart1.Init.OverSampling = UART_OVERSAMPLING_16; if (HAL_UART_Init(&huart1) != HAL_OK) { Error_Handler(); } } /** * @brief GPIO Initialization Function * @param None * @retval None */ static void MX_GPIO_Init(void) { /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); } /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* User may add here some code to deal with this error */ while(1) { } } ``` 在这个例子,我们使用UART1作为我们的串口,并将其初始化为115200波特率,8位数据位,无校验位和1个停止位。我们还定义了一个名为"buffer"的字符数组来存储要发送的字符串。在while循环,我们使用sprintf()函数将要发送的字符串格式化为缓冲区,然后使用HAL_UART_Transmit()函数通过串口发送该字符串。最后,我们使用HAL_Delay()函数延时1秒钟,然后再次发送相同的字符串。 注意,为了使用UART,您还需要在STM32CubeMX配置UART引脚,并在代码初始化GPIO引脚。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值