有个项目需要用到STM32F072的4个串口,串口1和2都已经OK,串口3和4怎么配置都配置不成功,发送数据都没有出来。配置串口1和2的时候,仿真读串口1和2的寄存器地址能看到设置进去的数据,但是3和4就没有,都是0。
有用过的帮忙看一下是什么情况,谢谢。
配置代码如下:
- void USART3_Config(void)
- {
- USART_InitTypeDef USART_InitStructure;
- // NVIC_InitTypeDef NVIC_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_DeInit(USART4);
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
-
- /* Connect PXx to USARTx_Tx */
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_1);
- /* Connect PXx to USARTx_Rx */
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_1);
- /* Configure USART Tx and Rx as alternate function push-pull */
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- USART_InitStructure.USART_BaudRate = 115200;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- 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;
- USART_Init(USART3, &USART_InitStructure);
- // USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
- /* NVIC configuration */
- /* Enable the USARTx Interrupt */
- // NVIC_InitStructure.NVIC_IRQChannel = USART3_4_IRQn;
- // NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
- // NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- // NVIC_Init(&NVIC_InitStructure);
- /* Enable USART */
- USART_Cmd(USART3, ENABLE);
- }
复制代码
- void USART4_Config(void)
- {
- USART_InitTypeDef USART_InitStructure;
- // NVIC_InitTypeDef NVIC_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- // USART_DeInit(USART4);
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART4, ENABLE);
-
- /* Connect PXx to USARTx_Tx */
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_4);
- /* Connect PXx to USARTx_Rx */
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_4);
- /* Configure USART Tx and Rx as alternate function push-pull */
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- USART_InitStructure.USART_BaudRate = 115200;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- 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;
- USART_Init(USART4, &USART_InitStructure);
- USART_ITConfig(USART4, USART_IT_RXNE, ENABLE);
- /* NVIC configuration */
- /* Enable the USARTx Interrupt */
- // NVIC_InitStructure.NVIC_IRQChannel = USART3_4_IRQn;
- // NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
- // NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- // NVIC_Init(&NVIC_InitStructure);
- /* Enable USART */
- USART_Cmd(USART4, ENABLE);
- }
复制代码
发送数据如下,循环里连续发送0XAA。
while(1)
{
USART_SendData(USART4,0xAA);
while (USART_GetFlagStatus(USART4, USART_FLAG_TC) == RESET);
USART_SendData(USART3,0xAA);
while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
}
一周热门 更多>