欢迎光临散文网 会员登陆 & 注册

comm.c

2020-05-20 21:03 作者:痴呆儿童欢乐多啊  | 我要投稿

#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管脚,配置为复用功能推挽输出*/
  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管脚,配置为复用功能输入*/
  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;                    //数据字长度(8位或9位)
 USART_InitStructure.USART_StopBits = USART_StopBits_1;             //可配置的停止位-支持1或2个停止位
 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 configuration*/
 /*根据传入的参数初始化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管脚,配置为复用功能推挽输出*/
  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管脚,配置为复用功能输入*/
  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;                    //数据字长度(8位或9位)
  USART_InitStructure.USART_StopBits = USART_StopBits_1;             //可配置的停止位-支持1或2个停止位
  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 configuration*/
 /*根据传入的参数初始化STM32的USART配置*/
  USART_Init(USART1,&USART_InitStructure);
 
 /*ENABLE USART*/
 /*使能STM32的USART功能模块*/
  USART_Cmd(USART1,ENABLE);
 
 
}


comm.c的评论 (共 条)

分享到微博请遵守国家法律