comm.c
#include "comm.h"
/*Comm2 Initiate Remap */
void COMM2_Init(uint32_t BaudRate)
{
??GPIO_InitTypeDef GPIO_InitStructure;
??USART_InitTypeDef USART_InitStructure;
?
?//IO initiate TX2,PD5; RX2,PD6
??RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);?
??RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
??RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
?/*Configure USART Tx as alternate function push-pull*/
?/*初始化stm32的USART的TX管腳,配置為復(fù)用功能推挽輸出*/
??GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
??GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
??GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
?? GPIO_Init(GPIOD, &GPIO_InitStructure);
?
?/*Confugure USART Rx as input floating*/
?/*初始化stm32的USART的RX管腳,配置為復(fù)用功能輸入*/
??GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
??GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
??GPIO_Init(GPIOD, &GPIO_InitStructure);
?
?/*ENABLE USART2 REMAP*/
?GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);
?
?USART_InitStructure.USART_BaudRate = BaudRate;???????????????????????????????? //串口波特率,bps 115200eg
?USART_InitStructure.USART_WordLength = USART_WordLength_8b;??????????????????? //數(shù)據(jù)字長度(8位或9位)
?USART_InitStructure.USART_StopBits = USART_StopBits_1;???????????? //可配置的停止位-支持1或2個停止位
?USART_InitStructure.USART_Parity = USART_Parity_No;??????????????? //無奇偶校驗(yàn)位
?USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無硬件流控制
?USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;???????? //雙工模式,使能發(fā)送和接收
?
?/*USART configuration*/
?/*根據(jù)傳入的參數(shù)初始化STM32的USART配置*/
?USART_Init(USART2,&USART_InitStructure);
?
?/*ENABLE USART*/
?/*使能STM32的USART功能模塊*/
?USART_Cmd(USART2,ENABLE);
}
/*Comm1 Initiate */
void COMM1_Init(uint32_t BaudRate)
{
??GPIO_InitTypeDef GPIO_InitStructure;
??USART_InitTypeDef USART_InitStructure;
??
?//IO initiate TX1,PA9; RX1,PA10
??RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);?
??RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
?/*Configure USART Tx as alternate function push-pull*/
?/*初始化stm32的USART的TX管腳,配置為復(fù)用功能推挽輸出*/
??GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
??GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
??GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
?? GPIO_Init(GPIOA, &GPIO_InitStructure);
?
?/*Confugure USART Rx as input floating*/
?/*初始化stm32的USART的RX管腳,配置為復(fù)用功能輸入*/
??GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
??GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
??GPIO_Init(GPIOA, &GPIO_InitStructure);
?
??USART_InitStructure.USART_BaudRate = BaudRate;???????????????????????????????? //串口波特率,bps 115200eg
??USART_InitStructure.USART_WordLength = USART_WordLength_8b;??????????????????? //數(shù)據(jù)字長度(8位或9位)
??USART_InitStructure.USART_StopBits = USART_StopBits_1;???????????? //可配置的停止位-支持1或2個停止位
??USART_InitStructure.USART_Parity = USART_Parity_No;??????????????? //無奇偶校驗(yàn)位
??USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無硬件流控制
??USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;???????? //雙工模式,使能發(fā)送和接收
??
?/*USART configuration*/
?/*根據(jù)傳入的參數(shù)初始化STM32的USART配置*/
??USART_Init(USART1,&USART_InitStructure);
?
?/*ENABLE USART*/
?/*使能STM32的USART功能模塊*/
??USART_Cmd(USART1,ENABLE);
?
?
}