串口
- 串口全称为串行接口,采用 全双工、异步通信的通信方式,一次只能传输一帧,一帧中包含 起始位、数据位(一般为 8bit )、校验位、停止位。
- 由于采用异步通信,所以通信双方(这里是 上位机 与 STM32)必须提前说明好 字符格式(一帧中的字符格式) 和 通信速率(波特率)
步骤
1、确定 IO 口并初始化
数据进入 USB 端口之后会进入 CH340(RS232 转 TTL),接着会进入 UART1端口,根据下图选择 PA9、PA10 作为接受口和发送口
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &GPIO_InitStructure);
2、初始化 UATR
USART_InitStruct.USART_BaudRate = 9600;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1, &USART_InitStruct);
3、UART 中断配置
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
4、编写 UART 中断服务函数
void USART1_IRQHandler(void)
{
uint16_t buf;
if( USART_GetITStatus(USART1, USART_IT_RXNE) != RESET )
{
buf = USART_ReceiveData(USART1);
USART_SendData(USART1, buf);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}
5、重定向 fputc()
修改 ==fputc()==函数,将 printf() 内的内容通过串口输出给上位机,通过上位机的串口软件查看该内容
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, ch);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
return ch;
}
效果
上位机的串口软件每隔 500ms 接受一句“hello world”
附源码
#include "stm32f4xx.h"
#include <stdio.h>
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, ch);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
return ch;
}
void usart1_init(void){
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStruct.USART_BaudRate = 9600;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1, &USART_InitStruct);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void delay_ms(uint16_t nms){
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
SysTick->CTRL = 0;
SysTick->LOAD = 21000*nms-1;
SysTick->VAL = 0;
SysTick->CTRL = 1;
while ((SysTick->CTRL & 0x00010000)==0);
SysTick->CTRL = 0;
SysTick->VAL = 0;
}
int main()
{
usart1_init();
while(1)
{
printf("hello world \n");
delay_ms(500);
}
}
void USART1_IRQHandler(void)
{
uint16_t buf;
if( USART_GetITStatus(USART1, USART_IT_RXNE) != RESET )
{
buf = USART_ReceiveData(USART1);
USART_SendData(USART1, buf);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}
|