当前位置:首页 > STM32f407 一个串口收传给另一个串口再发送出去串口收发设置
/* 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--) { } } /**
共分享92篇相关文档