0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

stm325个串口的配置函数 STM32串口如何发送数据

ss 来源:CSDNSumjess、可以吃的鱼 作者:CSDNSumjess、可以吃 2021-07-22 15:02 次阅读

5个串口的配置函数和收发数据函数代码:

#include “stm32f10x.h”

#include “misc.h”

#include “stm32f10x_gpio.h”

#include “stm32f10x_usart.h”

void USART1_Configuration(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

USART_InitTypeDef USART_InitStructure;

NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1 TX;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure); //端口A;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART1 RX;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入;

GPIO_Init(GPIOA, &GPIO_InitStructure); //端口A;

USART_InitStructure.USART_BaudRate = 9600; //波特率;

USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位8位;

USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位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);//配置串口参数

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置中断组,4位抢占优先级,4位响应优先级;

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //中断号;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

USART_Cmd(USART1, ENABLE); //使能串口;

}

void USART1_Send_Byte(u8 Data) //发送一个字节;

{

USART_SendData(USART1,Data);

while( USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET );

}

void USART1_Send_String(u8 *Data) //发送字符串;

{

while(*Data)

USART1_Send_Byte(*Data++);

}

void USART1_IRQHandler(void) //中断处理函数;

{

u8 res;

if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET) //判断是否发生中断;

{

USART_ClearFlag(USART1, USART_IT_RXNE); //清除标志位;

res=USART_ReceiveData(USART1); //接收数据;

USART1_Send_Byte(res); //用户自定义;

}

}

void USART2_Configuration(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

USART_InitTypeDef USART_InitStructure;

NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //USART2 TX;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure); //端口A;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //USART2 RX;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入;

GPIO_Init(GPIOA, &GPIO_InitStructure); //端口A;

USART_InitStructure.USART_BaudRate = 9600; //波特率;

USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位8位;

USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位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(USART2, &USART_InitStructure);//配置串口参数;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置中断组,4位抢占优先级,4位响应优先级;

NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //中断号;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

USART_Cmd(USART2, ENABLE); //使能串口;

}

void USART2_Send_Byte(u8 Data) //发送一个字节;

{

USART_SendData(USART2,Data);

while( USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET );

}

void USART2_Send_String(u8 *Data) //发送字符串;

{

while(*Data)

USART2_Send_Byte(*Data++);

}

void USART2_IRQHandler(void) //中断处理函数;

{

u8 res;

if(USART_GetITStatus(USART2, USART_IT_RXNE) == SET) //判断是否发生中断;

{

USART_ClearFlag(USART2, USART_IT_RXNE); //清除标志位;

res=USART_ReceiveData(USART2); //接收数据;

USART2_Send_Byte(res); //用户自定义;

}

}

void USART3_Configuration(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

USART_InitTypeDef USART_InitStructure;

NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE );

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART3 TX;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOB, &GPIO_InitStructure); //端口B;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //USART3 RX;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入;

GPIO_Init(GPIOB, &GPIO_InitStructure); //端口B;

USART_InitStructure.USART_BaudRate = 9600; //波特率;

USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位8位;

USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位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(USART3, &USART_InitStructure);//配置串口参数;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置中断组,4位抢占优先级,4位响应优先级;

NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; //中断号;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);

USART_Cmd(USART3, ENABLE); //使能串口;

}

void USART3_Send_Byte(u8 Data) //发送一个字节;

{

USART_SendData(USART3,Data);

while( USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET );

}

void USART3_Send_String(u8 *Data) //发送字符串;

{

while(*Data)

USART3_Send_Byte(*Data++);

}

void USART3_IRQHandler(void) //中断处理函数;

{

u8 res;

if(USART_GetITStatus(USART3, USART_IT_RXNE) == SET) //判断是否发生中断;

{

USART_ClearFlag(USART3, USART_IT_RXNE); //清除标志位;

res=USART_ReceiveData(USART3); //接收数据;

USART3_Send_Byte(res); //用户自定义;

}

}

void UART4_Configuration(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

USART_InitTypeDef USART_InitStructure;

NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE );

RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //UART4 TX;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOC, &GPIO_InitStructure); //端口C;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //UART4 RX;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入;

GPIO_Init(GPIOC, &GPIO_InitStructure); //端口C;

USART_InitStructure.USART_BaudRate = 9600; //波特率;

USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位8位;

USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位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(UART4, &USART_InitStructure);//配置串口参数;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置中断组,4位抢占优先级,4位响应优先级;

NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; //中断号;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);

USART_Cmd(UART4, ENABLE); //使能串口;

}

void UART4_Send_Byte(u8 Data) //发送一个字节;

{

USART_SendData(UART4,Data);

while( USART_GetFlagStatus(UART4, USART_FLAG_TC) == RESET );

}

void UART4_Send_String(u8 *Data) //发送字符串;

{

while(*Data)

UART4_Send_Byte(*Data++);

}

void UART4_IRQHandler(void) //中断处理函数;

{

u8 res;

if(USART_GetITStatus(UART4, USART_IT_RXNE) == SET) //判断是否发生中断;

{

USART_ClearFlag(UART4, USART_IT_RXNE); //清除标志位;

res=USART_ReceiveData(UART4); //接收数据;

UART4_Send_Byte(res); //用户自定义;

}

}

void UART5_Configuration(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

USART_InitTypeDef USART_InitStructure;

NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE );

RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //UART5 TX;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOC, &GPIO_InitStructure); //端口C;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //UART5 RX;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入;

GPIO_Init(GPIOD, &GPIO_InitStructure); //端口D;

USART_InitStructure.USART_BaudRate = 9600; //波特率;

USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位8位;

USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位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(UART5, &USART_InitStructure);//配置串口参数;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置中断组,4位抢占优先级,4位响应优先级;

NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn; //中断号;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);

USART_Cmd(UART5, ENABLE); //使能串口;

}

void UART5_Send_Byte(u8 Data) //发送一个字节;

{

USART_SendData(UART5,Data);

while( USART_GetFlagStatus(UART5, USART_FLAG_TC) == RESET );

}

void UART5_Send_String(u8 *Data) //发送字符串;

{

while(*Data)

UART5_Send_Byte(*Data++);

}

void UART5_IRQHandler(void) //中断处理函数;

{

u8 res;

if(USART_GetITStatus(UART5, USART_IT_RXNE) == SET) //判断是否发生中断;

{

USART_ClearFlag(UART5, USART_IT_RXNE); //清除标志位;

res=USART_ReceiveData(UART5); //接收数据;

UART5_Send_Byte(res); //用户自定义;

}

STM32串口发送数据

1. 串口发送数据最直接的方式就是标准调用库函数 。

void Send_data(u8 *s)

{

while(*s!=‘\0’)

{

while(USART_GetFlagStatus(USART1,USART_FLAG_TC )==RESET);

USART_SendData(USART1,*s);

s++;

}

}

2. 直接使用printf函数。

可以吃的鱼

整合自:CSDNSumjess、可以吃的鱼

编辑:jq

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • STM32
    +关注

    关注

    2239

    文章

    10671

    浏览量

    348735
收藏 人收藏

    评论

    相关推荐

    使用CubeMX配置STM32010C6T6的LPUART1外设,调用串口发送和接收函数均不能收发数据怎么解决?

    如题,STM32010C6T6串口1配置完成后,Cube界面接收引脚模式默认为推挽输出,并且没有其它选项可以更改,(其它系列MCU默认是输入模式)生成的工程调用串口
    发表于 03-19 07:48

    STM32G031k8t6串口发送为什么会进入硬件中断?

    STM32G031k8t6串口发送进入硬件中断
    发表于 03-13 07:59

    stm32怎么读取串口发来的指令

    介绍使用STM32读取串口指令的步骤。 初始化串口:在开始读取串口指令之前,首先需要初始化串口接口。这包括设置
    的头像 发表于 01-07 17:08 1325次阅读

    智能车ROS与STM32串口通信代码

    receives the data structure //串口接收数据结构体 SEND_DATA Send_Data ; //The serial port sends the data structure //串口
    的头像 发表于 11-26 17:47 782次阅读

    ROS与STM32串口通信代码

    receives the data structure //串口接收数据结构体 SEND_DATA Send_Data ; //The serial port sends the data structure //串口
    的头像 发表于 11-17 18:10 486次阅读

    串口STM32中的配置

    首先要明确几点:使用STM32串口外设中的哪一个?串口发送或者接收数据串口相关的参数
    的头像 发表于 11-10 16:09 1600次阅读
    <b class='flag-5'>串口</b>在<b class='flag-5'>STM32</b>中的<b class='flag-5'>配置</b>

    STM32里的串口通信

    端。STM32串口资源有USART1、USART2、USART3. 串口的几个重要的参数: 波特率,串口通信的速率 空闲,一般为高电平 起始位,标志一个
    的头像 发表于 11-10 15:58 1521次阅读
    <b class='flag-5'>STM32</b>里的<b class='flag-5'>串口</b>通信

    GD32串口dma接收空闲中断配置流程是怎样的?

    GD32串口dma接收空闲中断配置流程是怎样的,用过的朋友分享下经验。现在已经实现串口+DMA的发送和接收,都没有问题,并且使用查询idle方式也可以接收不定长
    发表于 11-03 07:51

    STM32 HAL库串口同时收发,接收卡死?

    HAL库是针对STM32系列单片机的一套常用的高级抽象层库。在HAL库中,串口通信是通过针对USART外设的封装实现的。HAL库中提供了一些函数,可以方便地配置USART外设的各种参
    的头像 发表于 10-26 17:42 2416次阅读

    STM32 HAL库串口收发是如何使用的?

    的一种库,它提供了一种简单易用的方法来使用STM32的各种外设。 本文将详细介绍如何使用STM32 HAL库来进行串口通信,包括初始化、发送数据
    的头像 发表于 10-26 17:42 720次阅读

    stm32如何向串口用一个字节发送3300这个数?

    stm32如何向串口用一个字节发送3300这个数? STM32是一种嵌入式微控制器,具有高性能,低功耗和低成本等优势。其中,串口通信是嵌入式
    的头像 发表于 10-26 11:31 484次阅读

    STM32串口发送数据和接收数据方式总结

    STM32串口发送数据和接收数据方式总结
    的头像 发表于 09-19 09:14 5687次阅读
    <b class='flag-5'>STM32</b><b class='flag-5'>串口</b><b class='flag-5'>发送</b><b class='flag-5'>数据</b>和接收<b class='flag-5'>数据</b>方式总结

    STM32F407 串口配置步骤

    介绍STM32F407串口配置步骤,完成串口数据发送与接收、实现中断接收,支持printf重定
    的头像 发表于 07-06 14:29 1886次阅读
    <b class='flag-5'>STM32</b>F407 <b class='flag-5'>串口</b><b class='flag-5'>配置</b>步骤

    STM32 HAL库串口收发如何使用

    使用 STM32CubeMX 做好初始化,就可以直接使用了。 但是最近在某些产品上使用串口同时收发的时候,发现有时候串口会收不到数据了,但是发送
    的头像 发表于 06-22 10:38 4037次阅读
    <b class='flag-5'>STM32</b> HAL库<b class='flag-5'>串口</b>收发如何使用

    STM32采用串口DMA方式向上位机连续发送数据

    完成1.3DMA库函数配置过程二、串口DMA方式向上位机发送数据2.1新建工程2.2设置RCC 2.3打开USART1及DMA模式​ 三、代
    发表于 05-11 09:32 2次下载
    <b class='flag-5'>STM32</b>采用<b class='flag-5'>串口</b>DMA方式向上位机连续<b class='flag-5'>发送</b><b class='flag-5'>数据</b>