STM32——串口发送字符、字符串 + printf 的重定向
- 基本框架
- 1. 配置时钟:配置GPIO时钟、窗口时钟、引脚复用时钟
- 2. 配置GPIO结构体
- 在 stm32f10x_gpio.h 头文件中查找相关函数函数
- 3. 配置串口结构体
- 在 stm32f10x_usart.h 头文件中查找相关函数
- 4. 串口的发送
- STM32 串口通信中 USART_FLAG_TC 与 USART_FLAG_TXE 区别
- 在 stm32f10x_usart.h 头文件中查找相关函数
- 基本框架整合代码
-
- 串口发送字符、字符串
- 1. 串口发送字符
- 2. 串口发送字符串
- 串口发送字符、字符串 代码整合
-
- printf 的重定向
- fputc
- fgetc
- usart.c
- usart.h
- main.c
基本框架
1. 配置时钟:配置GPIO时钟、窗口时钟、引脚复用时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
2. 配置GPIO结构体
在 stm32f10x_gpio.h 头文件中查找相关函数函数
GPIO_InitTypeDef gpioInitStructure;
gpioInitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
gpioInitStructure.GPIO_Pin = GPIO_Pin_9;
gpioInitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &gpioInitStructure);
gpioInitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
gpioInitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &gpioInitStructure);
3. 配置串口结构体
在 stm32f10x_usart.h 头文件中查找相关函数
usartInitStructure.USART_BaudRate = 115200;
usartInitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
usartInitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
usartInitStructure.USART_Parity = USART_Parity_No;
usartInitStructure.USART_StopBits = USART_StopBits_1;
usartInitStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1, &usartInitStructure);
USART_Cmd(USART1, ENABLE);
4. 串口的发送
STM32 串口通信中 USART_FLAG_TC 与 USART_FLAG_TXE 区别
在 stm32f10x_usart.h 头文件中查找相关函数
int main()
{
USART_SendData( USART1, 'O');
while( USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET );
USART_SendData( USART1, 'K');
while( USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET );
delay(1000);
}
基本框架整合代码
usart.c
#include "usart.h"
#include "stm32f10x.h"
void usart_init()
{
GPIO_InitTypeDef gpioInitStructure;
USART_InitTypeDef usartInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
gpioInitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
gpioInitStructure.GPIO_Pin = GPIO_Pin_9;
gpioInitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &gpioInitStructure);
gpioInitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
gpioInitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &gpioInitStructure);
usartInitStructure.USART_BaudRate = 115200;
usartInitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
usartInitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
usartInitStructure.USART_Parity = USART_Parity_No;
usartInitStructure.USART_StopBits = USART_StopBits_1;
usartInitStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1, &usartInitStructure);
USART_Cmd(USART1, ENABLE);
}
usart.h
#include "stm32f10x.h"
void usart_init(void);
main.c
#include "stm32f10x.h"
#include "main.h"
#include "led.h"
#include "shake.h"
#include "relay.h"
#include "exti.h"
#include "usart.h"
void delay(uint16_t time)
{
uint16_t i=0;
while(time--)
{
i=10000;
while(i--);
}
}
int main()
{
USART_SendData( USART1, 'O');
while( USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET );
USART_SendData( USART1, 'K');
while( USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET );
delay(1000);
}
串口发送字符、字符串
1. 串口发送字符
void USARTSendByte(USART_TypeDef* USARTx, uint16_t Data)
{
USART_SendData( USARTx, Data);
while( USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET );
}
2. 串口发送字符串
void USARTSendStr(USART_TypeDef* USARTx, char* str)
{
uint16_t i=0;
while( *(str+i) != '\0' )
{
USARTSendByte(USARTx, *(str+i) );
i++;
}
while( USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET );
}
串口发送字符、字符串 代码整合
usart.c
#include "usart.h"
#include "stm32f10x.h"
void usart_init()
{
GPIO_InitTypeDef gpioInitStructure;
USART_InitTypeDef usartInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
gpioInitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
gpioInitStructure.GPIO_Pin = GPIO_Pin_9;
gpioInitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &gpioInitStructure);
gpioInitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
gpioInitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &gpioInitStructure);
usartInitStructure.USART_BaudRate = 115200;
usartInitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
usartInitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
usartInitStructure.USART_Parity = USART_Parity_No;
usartInitStructure.USART_StopBits = USART_StopBits_1;
usartInitStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1, &usartInitStructure);
USART_Cmd(USART1, ENABLE);
}
void USARTSendByte(USART_TypeDef* USARTx, uint16_t Data)
{
USART_SendData( USARTx, Data);
while( USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET );
}
void USARTSendStr(USART_TypeDef* USARTx, char* str)
{
uint16_t i=0;
while( *(str+i) != '\0' )
{
USARTSendByte( USART1, *(str+i) );
i++;
}
while( USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET );
}
usart.h
#include "stm32f10x.h"
void usart_init(void);
main.c
#include "stm32f10x.h"
#include "main.h"
#include "led.h"
#include "shake.h"
#include "relay.h"
#include "exti.h"
#include "usart.h"
void delay(uint16_t time)
{
uint16_t i=0;
while(time--)
{
i=10000;
while(i--);
}
}
int main()
{
usart_init();
USARTSendStr(USART1, "Yinyuer is a pretty girl!");
while(1)
{
}
}
printf 的重定向
fputc
int fputc(int ch, FILE* f)
{
USART_SendData(USART1, (uint8_t) ch);
while( USART_GetFlagStatus(USART1, USART_TXE) == RESET );
return ch;
}
fgetc
int fgetc(FILE* f)
{
while( USART_GetFlagStatus(USART1, USART_RXNE) == RESET )
return (int) USART_ReceiveData(USART1);
}
usart.c
#include "usart.h"
#include "stm32f10x.h"
void usart_init()
{
GPIO_InitTypeDef gpioInitStructure;
USART_InitTypeDef usartInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
gpioInitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
gpioInitStructure.GPIO_Pin = GPIO_Pin_9;
gpioInitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &gpioInitStructure);
gpioInitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
gpioInitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &gpioInitStructure);
usartInitStructure.USART_BaudRate = 115200;
usartInitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
usartInitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
usartInitStructure.USART_Parity = USART_Parity_No;
usartInitStructure.USART_StopBits = USART_StopBits_1;
usartInitStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1, &usartInitStructure);
USART_Cmd(USART1, ENABLE);
}
void USARTSendByte(USART_TypeDef* USARTx, uint16_t Data)
{
USART_SendData( USARTx, Data);
while( USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET );
}
void USARTSendStr(USART_TypeDef* USARTx, char* str)
{
uint16_t i=0;
while( *(str+i) != '\0' )
{
USARTSendByte( USART1, *(str+i) );
i++;
}
while( USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET );
}
int fputc( int ch, FILE* f )
{
USART_SendData( USART1, (uint8_t) ch );
while( USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET );
return ch;
}
int fgetc(FILE* f)
{
while( USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET );
return (int) USART_ReceiveData(USART1);
}
usart.h
#include "stm32f10x.h"
#include <stdio.h>
void usart_init(void);
void USARTSendByte(USART_TypeDef* USARTx, uint16_t Data);
void USARTSendStr(USART_TypeDef* USARTx, char* str);
main.c
#include "stm32f10x.h"
#include "main.h"
#include "led.h"
#include "shake.h"
#include "relay.h"
#include "exti.h"
#include "usart.h"
void delay(uint16_t time)
{
uint16_t i=0;
while(time--)
{
i=10000;
while(i--);
}
}
int main()
{
usart_init();
printf("Yinyuer is a pretty girl!");
while(1)
{
}
}
实验结果:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)