printf("Hello 2018!");

一位学生分享了自己全力以赴准备期末考试的决心,决定在年前这段时间暂停浏览博客,专心备考。同时,表达了对新年的期待和健康祝福。

月考 has Boom!

要全心准备期末考试,到年前是不能再看Blog了

新年加油!!!


不要感冒

:joy:

转载于:https://www.cnblogs.com/widerg/p/8169103.html

要实现串口printf()函数并打印"hello world!",需要以下几个步骤: 1. **初始化串口**:配置串口的波特率、数据位、停止位和校验位等参数。 2. **重定向printf函数**:将printf函数的输出重定向到串口。 3. **编写printf函数**:实现一个简单的printf函数,将字符通过串口发送出去。 以下是一个简单的示例代码,假设使用的是STM32微控制器: ```c #include "stm32f10x.h" #include <stdio.h> // 串口初始化函数 void USART_Init(void) { // 初始化GPIO RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // TX GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // RX GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); // 初始化USART USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 9600; 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(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE); } // 重定向printf函数 int fputc(int ch, FILE *f) { while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); USART_SendData(USART1, (uint8_t)ch); return ch; } int main(void) { USART_Init(); printf("hello world!\n"); while (1); return 0; } ``` ### 解释 1. **初始化串口**:配置GPIO和USART1的初始化参数。 2. **重定向printf函数**:通过重写`fputc`函数,将字符输出到串口。 3. **主函数**:初始化串口并调用`printf`函数打印"hello world!"。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值