当前位置:
文档之家› STM32f407 一个串口收传给另一个串口再发送出去串口收发设置
STM32f407 一个串口收传给另一个串口再发送出去串口收发设置
USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
USART_ITConfig(USART3,USART_IT_TXE,ENABLE);
/* Enable USART */
USART_Cmd(USART3, ENABLE);
/* Enable UART clock */
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_conf.h"
#include <stdio.h>
void Delay(__IO uint32_t nCount);
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*/
//函数功能:
UART配置
//入口参数:无//出口参数:无//备注:
//************************************************
voidUSART_Configuration(void){USART_InitTypeDefUSART_InitStructure;
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);
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//通道中断使能
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn; //嵌套通道为USART6_IRQn
/* 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_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Configure PA0 pin as input floating */
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
* @retvalNone
*/
void Delay(__IO uint32_t nCount){while(nCount--){}}/**
intfputc(intch, FILE *f){/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
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);
for(;ms>0;ms--)
for(j=0;j<9700;j++);}//***********************************************
//函数功能:
IO配置
//入口参数:无//出口参数:无//备注:
//************************************************
USART_ART_BaudRate =9600;//波特率
USART_ART_WordLength = USART_WordLength_8b; //8位数据USART_ART_StopBits = USART_StopBits_1;//停止位
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_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
//GPIO_Init(GPIOA, &GPIO_InitStructure);}//***********************************************
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; //嵌套通道为USART3_IRQn
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//响应优先级为0
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_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* USART configuration */
USART_Init(USART3,&USART_InitStructure);
USART_SendData(USART6, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART6, USART_FLAG_TC) == RESET)
{}
returnch;}#ifdefUSE_FULL_ASSERT
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0; //抢占优先级为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//响应优先级为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//通道中断使能
/**
* @briefReports the name of the source file and the source line number
*where the assert_param error has occurred.
//***********************************************
//函数功能:
xxms
//xx参数:
xxxx
//出口参数:无//备注:
//****************************************ቤተ መጻሕፍቲ ባይዱ*******
voidDelay_ms(u16 ms){u32 j;
GPIO_StructInit(&GPIO_InitStructure);
/* Configure USART Tx as alternate function*/
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
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 */