stm32串口5怎么配置
答案:1 悬赏:80 手机版
解决时间 2021-01-21 18:58
- 提问者网友:佞臣
- 2021-01-20 21:48
stm32串口5怎么配置
最佳答案
- 五星知识达人网友:由着我着迷
- 2021-01-20 23:09
STM32串口配置的一般步骤(库函数)
(1)串口时钟使能:RCC_APBxPeriphClockCmd();
GPIO时钟使能:RCC_AHBxPeriphClockCmd();
(2)引脚复用映射:GPIO_PinAFConfig();
(3)GPIO端口模式配置:GPIO_Init(); 模式配置为GPIO_Mode_AF
(4)串口参数初始化:USART_Init();
(5)开启中断并且初始化NVIC(如果需要开启中断才需要这个步骤)
NVIC_Init();
USART_ITConfig();
(6)使能串口:USART_Cmd();
(7)编写中断处理函数:USARTx_IRQHandler();
(8)串口数据收发:
void USART_SendData();//发送数据到串口,DR
uint16_t USART_ReceiveData();//接收数据,从DR读取接收的数据
(9)串口传输状态获取:
FlagStatus USART_GetFlagStatus();
void USART_ClearITPendingBit();更加详细的可以在闯客网技术论坛进行查看的。
范例代码:
#include "stm32f4xx.h"
#include "usart.h"
void USART1_IRQHandler(void)
{
uint16_t recv;
if (USART_GetFlagStatus(USART1,USART_IT_RXNE))
{
recv = USART_ReceiveData(USART1);
USART_SendData(USART1,recv);
}
}
void Usart1_Demo_Init(void)
{
GPIO_InitTypeDef GPIOA_InitStruct;
USART_InitTypeDef USART1_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
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);
GPIOA_InitStruct.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIOA_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIOA_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIOA_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIOA_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA, &GPIOA_InitStruct);
USART1_InitStruct.USART_BaudRate = 115200;
USART1_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART1_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART1_InitStruct.USART_Parity = USART_Parity_No;
USART1_InitStruct.USART_StopBits = USART_StopBits_1;
USART1_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1, &USART1_InitStruct);
USART_Cmd(USART1, ENABLE);
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStruct);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
}
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
Usart1_Demo_Init();
while (1);
}
(1)串口时钟使能:RCC_APBxPeriphClockCmd();
GPIO时钟使能:RCC_AHBxPeriphClockCmd();
(2)引脚复用映射:GPIO_PinAFConfig();
(3)GPIO端口模式配置:GPIO_Init(); 模式配置为GPIO_Mode_AF
(4)串口参数初始化:USART_Init();
(5)开启中断并且初始化NVIC(如果需要开启中断才需要这个步骤)
NVIC_Init();
USART_ITConfig();
(6)使能串口:USART_Cmd();
(7)编写中断处理函数:USARTx_IRQHandler();
(8)串口数据收发:
void USART_SendData();//发送数据到串口,DR
uint16_t USART_ReceiveData();//接收数据,从DR读取接收的数据
(9)串口传输状态获取:
FlagStatus USART_GetFlagStatus();
void USART_ClearITPendingBit();更加详细的可以在闯客网技术论坛进行查看的。
范例代码:
#include "stm32f4xx.h"
#include "usart.h"
void USART1_IRQHandler(void)
{
uint16_t recv;
if (USART_GetFlagStatus(USART1,USART_IT_RXNE))
{
recv = USART_ReceiveData(USART1);
USART_SendData(USART1,recv);
}
}
void Usart1_Demo_Init(void)
{
GPIO_InitTypeDef GPIOA_InitStruct;
USART_InitTypeDef USART1_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
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);
GPIOA_InitStruct.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIOA_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIOA_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIOA_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIOA_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA, &GPIOA_InitStruct);
USART1_InitStruct.USART_BaudRate = 115200;
USART1_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART1_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART1_InitStruct.USART_Parity = USART_Parity_No;
USART1_InitStruct.USART_StopBits = USART_StopBits_1;
USART1_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1, &USART1_InitStruct);
USART_Cmd(USART1, ENABLE);
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStruct);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
}
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
Usart1_Demo_Init();
while (1);
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯