云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > STM32f407 一个串口收传给另一个串口再发送出去串口收发设置

STM32f407 一个串口收传给另一个串口再发送出去串口收发设置

  • 62 次阅读
  • 3 次下载
  • 2025/6/15 11:12:05

/* Includes ------------------------------------------------------------------*/ #include \ #include

void Delay(__IO uint32_t nCount);

//*********************************************** //函数功能:延时ms //入口参数:延时长度 //出口参数:无 //备注:

//************************************************ voidDelay_ms(u16 ms) {

u32 j;

for(;ms>0;ms--)

for(j=0;j<9700;j++); }

//*********************************************** //函数功能:IO配置 //入口参数:无

//出口参数:无 //备注:

//************************************************ voidGPIO_Configuration(void) {

GPIO_InitTypeDefGPIO_InitStructure;

/* GPIOD Periph clock enable */

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

/* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOC, &GPIO_InitStructure);

/* Configure PA0 pin as input floating */

//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; //GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //GPIO_Init(GPIOA, &GPIO_InitStructure);

}

//*********************************************** //函数功能:UART配置 //入口参数:无 //出口参数:无 //备注:

//************************************************ voidUSART_Configuration(void) {

USART_InitTypeDefUSART_InitStructure;

USART_InitStructure.USART_BaudRate =9600; //波特率

USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8 位数据 USART_InitStructure.USART_StopBits = USART_StopBits_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; // 发送/接收使能

GPIO_StructInit(&GPIO_InitStructure);

/* Configure USART Tx as alternate function */ GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

/* Enable GPIO clock */

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

/* Enable UART clock */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); /* Connect PXx to USARTx_Tx*/

GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3); /* Connect PXx to USARTx_Rx*/

GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure);

/* Configure USART Rx as alternate function */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_Init(GPIOC, &GPIO_InitStructure); /* USART configuration */

USART_Init(USART3,&USART_InitStructure); USART_ITConfig(USART3,USART_IT_RXNE,ENABLE); USART_ITConfig(USART3,USART_IT_TXE,ENABLE); /* Enable USART */

USART_Cmd(USART3, ENABLE); /* Enable UART clock */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE); /* Connect PXx to USARTx_Tx*/

GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6); /* Connect PXx to USARTx_Rx*/

GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6); GPIO_StructInit(&GPIO_InitStructure);

/* Configure USART Tx as alternate function */ GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure);

/* Configure USART Rx as alternate function */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_Init(GPIOC, &GPIO_InitStructure);

/* USART configuration */

USART_Init(USART6,&USART_InitStructure); USART_ITConfig(USART6,USART_IT_RXNE,ENABLE); //USART_ITConfig(USART6,USART_IT_TXE,ENABLE); /* Enable USART */

USART_Cmd(USART6, ENABLE);

}

voidNVIC_Config(void) {

NVIC_InitTypeDefNVIC_InitStructure;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //嵌套优先级分组为1

NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; //嵌套通道为USART3_IRQn NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级为 0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级为 0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //通道中断使能 NVIC_Init(&NVIC_InitStructure);

NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn; //嵌套通道为USART6_IRQn NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0; //抢占优先级为 0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级为 0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //通道中断使能 NVIC_Init(&NVIC_InitStructure); }

/**

* @brief Main program * @param None * @retvalNone

**

* @brief Delay Function.

* @paramnCount:specifies the Delay time length. * @retvalNone

*/

void Delay(__IO uint32_t nCount) {

while(nCount--) { } } /**

  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

/* Includes ------------------------------------------------------------------*/ #include \ #include void Delay(__IO uint32_t nCount); //*********************************************** //函数功能:延时ms //入口参数:延时长度 //出口参数:无 //备注: //************************************************ voidDelay_ms(u16 ms) { u32 j; for(;ms>0;ms--) for(j=0;j<

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com