初级会员

- 积分
- 56
- 金钱
- 56
- 注册时间
- 2014-1-5
- 在线时间
- 7 小时
|
1金钱
我有个制作,需要用到3个串口,串口初始化后,串口1和2都能正常输出,唯独串口3一直没有输出,也查阅了很多资料,也找不出问题所在,请假一下各位大侠,能帮我指点一下吗?小弟先在这里谢过了。
这里我将串口1、2、3的时钟集中在一个函数里配置
void Usart_RCC_Configuration(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB1Periph_USART3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1, ENABLE)
}
void COM1_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //重新定义GPIO结构体名称
USART_InitTypeDef USART_InitStructure; //重新定义USART结构体名称
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
USART_InitStructure.USART_BaudRate = 115200; //设置波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8位数据位
USART_InitStructure.USART_StopBits = USART_StopBits_1; //1位停止位
USART_InitStructure.USART_Parity = USART_Parity_No; //无校验
USART_InitStructure.USART_Mode = USART_Mode_Tx|USART_Mode_Rx; //收发使能
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //硬件流控制失能
USART_Init(USART1,&USART_InitStructure); //初始化COM1
//配置PA9位复用推挽输出TX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //选择GPIO9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速率50MHZ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置为复用推挽输出
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化TX引脚
//配置PA10位浮空输入RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //选择GPIO10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //配置为浮空输入
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化RX引脚
USART_Cmd(USART1,ENABLE); //使能COM1
}
void COM2_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //重新定义GPIO结构体名称
USART_InitTypeDef USART_InitStructure; //重新定义USART结构体名称
USART_InitStructure.USART_BaudRate = 57600; //设置波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8位数据位
USART_InitStructure.USART_StopBits = USART_StopBits_1; //1位停止位
USART_InitStructure.USART_Parity = USART_Parity_No; //无校验
USART_InitStructure.USART_Mode = USART_Mode_Tx|USART_Mode_Rx; //收发使能
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //硬件流控制失能
USART_Init(USART2,&USART_InitStructure); //初始化COM2
//配置PA2位复用推挽输出TX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //选择GPIO2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速率50MHZ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置为复用推挽输出
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化TX引脚
//配置PA3位浮空输入RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //选择GPIO3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //配置为浮空输入
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化RX引脚
USART_Cmd(USART2,ENABLE); //使能COM2
}
void COM3_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //重新定义GPIO结构体名称
USART_InitTypeDef USART_InitStructure; //重新定义USART结构体名称
USART_InitStructure.USART_BaudRate = 9600; //设置波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8位数据位
USART_InitStructure.USART_StopBits = USART_StopBits_1; //1位停止位
USART_InitStructure.USART_Parity = USART_Parity_No; //无校验
USART_InitStructure.USART_Mode = USART_Mode_Tx|USART_Mode_Rx; //收发使能
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //硬件流控制失能
USART_Init(USART3,&USART_InitStructure); //初始化COM3
//配置PB10位复用推挽输出TX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//配置PB11位浮空输入RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
串口发1个字节函数,
void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch)
{
USART_SendData(pUSARTx,ch); // 发送一个字节数据到USART
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET); // 等待发送数据寄存器为空
}
main函数里就是初始化3个串口
Usart_SendByte( USART1, ‘A’);
Usart_SendByte( USART2, ‘B’);
Usart_SendByte( USART3, ‘C’);
|
|