STM32F103VC下将串口作为输入打印到终端
- 定义两个文件,一个是uart.c 一个是uart.h
- uart.h的代码:
/*uart.h code*/
#ifndef UART_H
#define UART_H
void uart1_init(void);
#endif
对外调用的初始化函数进行声明。
要包含的头文件
#include "stm32f10x.h"
#include "stdio.h"
#include "uarth"
初始化串口要用到的GPIO口,这里是PA9,PA10
int uart_gpio_init(){
GPIO_InitTypeDef UART_GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_StructInit(&UART_GPIO_InitStructure);
UART_GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
UART_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
UART_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &UART_GPIO_InitStructure);
UART_GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
UART_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &UART_GPIO_InitStructure);
return 0;
}
配置串口的波特率,校验位等等属性
int uart_config(){
USART_InitTypeDef USART_InitStructure;
USART_StructInit(&USART_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(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
return 0;
}
配置串口所用到中断向量表
void nvic_uart_config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
写一个外部调用的串口初始化函数
void uart1_init(void)
{
uart_gpio_init();
uart_config();
nvic_uart_config();
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
}
将printf输出进行编写为该串口
int fputc(int ch, FILE * f){
USART_SendData(USART1, (uint8_t)ch);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET );
return ch;
}
文件中定义串口中断采用中断接收,接收到之后采用轮询的方式发送
void USART1_IRQHndler(void){
STM32F103VC下将串口作为输入打印到终端
- 定义两个文件,一个是uart.c 一个是uart.h
- uart.h的代码:
/*uart.h code*/
#ifndef UART_H
#define UART_H
void uart1_init(void);
#endif
对外调用的初始化函数进行声明。
要包含的头文件
#include "stm32f10x.h"
#include "stdio.h"
#include "uarth"
初始化串口要用到的GPIO口,这里是PA9,PA10
int uart_gpio_init(){
GPIO_InitTypeDef UART_GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_StructInit(&UART_GPIO_InitStructure);
UART_GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
UART_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
UART_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &UART_GPIO_InitStructure);
UART_GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
UART_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &UART_GPIO_InitStructure);
return 0;
}
配置串口的波特率,校验位等等属性
int uart_config(){
USART_InitTypeDef USART_InitStructure;
USART_StructInit(&USART_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(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
return 0;
}
配置串口所用到中断向量表
void nvic_uart_config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
写一个外部调用的串口初始化函数
void uart1_init(void)
{
uart_gpio_init();
uart_config();
nvic_uart_config();
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
}
将printf输出进行编写为该串口
int fputc(int ch, FILE * f){
USART_SendData(USART1, (uint8_t)ch);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET );
return ch;
}
文件中定义串口中断采用中断接收,接收到之后采用轮询的方式发送
void USART1_IRQHndler(void){
举报