目錄
- 串口
- 步驟
- 1、確定 IO 口并初始化
- 2、初始化 UATR
- 3、UART 中斷配置
- 4、撰寫 UART 中斷服務函式
- 5、重定向 fputc()
- 效果
- 附原始碼
串口
- 串口全稱為串行介面,采用 全雙工、異步通信的通信方式,一次只能傳輸一幀,一幀中包含 起始位、資料位(一般為 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”
附原始碼
//uart_printf.c
#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);
/* Enable and set EXTI Line0 Interrupt 配置中斷優先級*/
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 的頻率 21MHz 21次 1us, 21000次 1ms,
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
SysTick->CTRL = 0; //使能時鐘
SysTick->LOAD = 21000*nms-1; //計數值
SysTick->VAL = 0; //清除計數標志
//SysTick->CTRL |= (1<<0); //使能時鐘
SysTick->CTRL = 1;
while ((SysTick->CTRL & 0x00010000)==0);//等待計數標志被置 1
SysTick->CTRL = 0; //失能 systick
SysTick->VAL = 0; //清除計數標志
}
int main()
{
//1.串口初始化
usart1_init();
//2.進入死回圈
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);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/295737.html
標籤:其他
上一篇:LwIP協議堆疊中UDP部分詳解