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

发布时间:2023-06-26  

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!=‘ ’)

{

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

USART_SendData(USART1,*s);

s++;

}

}

2. 直接使用printf函数。

可以吃的鱼


文章来源于:电子工程世界    原文链接
本站所有转载文章系出于传递更多信息之目的,且明确注明来源,不希望被转载的媒体或个人可与我们联系,我们将立即进行删除处理。

我们与500+贴片厂合作,完美满足客户的定制需求。为品牌提供定制化的推广方案、专属产品特色页,多渠道推广,SEM/SEO精准营销以及与公众号的联合推广...详细>>

利用葫芦芯平台的卓越技术服务和新产品推广能力,原厂代理能轻松打入消费物联网(IOT)、信息与通信(ICT)、汽车及新能源汽车、工业自动化及工业物联网、装备及功率电子...详细>>

充分利用其强大的电子元器件采购流量,创新性地为这些物料提供了一个全新的窗口。我们的高效数字营销技术,不仅可以助你轻松识别与连接到需求方,更能够极大地提高“闲置物料”的处理能力,通过葫芦芯平台...详细>>

我们的目标很明确:构建一个全方位的半导体产业生态系统。成为一家全球领先的半导体互联网生态公司。目前,我们已成功打造了智能汽车、智能家居、大健康医疗、机器人和材料等五大生态领域。更为重要的是...详细>>

我们深知加工与定制类服务商的价值和重要性,因此,我们倾力为您提供最顶尖的营销资源。在我们的平台上,您可以直接接触到100万的研发工程师和采购工程师,以及10万的活跃客户群体...详细>>

凭借我们强大的专业流量和尖端的互联网数字营销技术,我们承诺为原厂提供免费的产品资料推广服务。无论是最新的资讯、技术动态还是创新产品,都可以通过我们的平台迅速传达给目标客户...详细>>

我们不止于将线索转化为潜在客户。葫芦芯平台致力于形成业务闭环,从引流、宣传到最终销售,全程跟进,确保每一个potential lead都得到妥善处理,从而大幅提高转化率。不仅如此...详细>>