最近在调试STM32串口,发现串口发送数据总是进入如下死循环
while(USART_GetFlagStatus(UART5, USART_FLAG_TXE) == RESET);
经检查发现串口时钟没有开,打开串口时钟后串口发送数据正常
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE); //打开串口时钟
附上串口配置完整代码
void uart_init(void)
{
/* 定义结构体变量 */
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/*开启时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); //ʹÄÜUSART1£¬GPIOAʱÖÓ
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE); //ʹÄÜUSART1£¬GPIOAʱÖÓ
/* 初始化GPIO口*/
//RS4851 USART1_TX GPIOA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA.9
//USART1_RX GPIOA.10³õʼ»¯
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë
GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA.10
//RS4851 RE/DE
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//PB0
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;// ÍÆÍìÊä³ö
GPIO_Init(GPIOB, &GPIO_InitStructure);
//UART5_TX GPIOC.12
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //PC12
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_Init(GPIOC, &GPIO_InitStructure);//³õʼ»¯PC12
//UART5_RX GPIOD.2³õʼ»¯
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;//PD2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë
GPIO_Init(GPIOD, &GPIO_InitStructure);//³õʼ»¯GPIOD.2
/* NVICÅäÖÃ */
//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3 ; //ÇÀÕ¼ÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×ÓÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷
//Uart5 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3 ; //ÇÀÕ¼ÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //×ÓÓÅÏȼ¶2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷
/* USARTÅäÖÃ */
//USART1 ³õʼ»¯ÉèÖÃ
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; //ÊÕ·¢Ä£Ê½
USART_Init(USART1, &USART_InitStructure); //³õʼ»¯´®¿Ú1
//UART5 ³õʼ»¯ÉèÖÃ
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; //ÊÕ·¢Ä£Ê½
USART_Init(UART5, &USART_InitStructure); //³õʼ»¯´®¿Ú5
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //¿ªÆô´®¿Ú1½ÓÊÜÖжÏ
USART_ITConfig(UART5, USART_IT_RXNE, ENABLE); //¿ªÆô´®¿Ú5½ÓÊÜÖжÏ
USART_Cmd(USART1, ENABLE); //ʹÄÜ´®¿Ú1
USART_Cmd(UART5, ENABLE); //ʹÄÜ´®¿Ú5
}
以上为完成配置代码。
最近在调试STM32串口,发现串口发送数据总是进入如下死循环
while(USART_GetFlagStatus(UART5, USART_FLAG_TXE) == RESET);
经检查发现串口时钟没有开,打开串口时钟后串口发送数据正常
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE); //打开串口时钟
附上串口配置完整代码
void uart_init(void)
{
/* 定义结构体变量 */
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/*开启时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); //ʹÄÜUSART1£¬GPIOAʱÖÓ
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE); //ʹÄÜUSART1£¬GPIOAʱÖÓ
/* 初始化GPIO口*/
//RS4851 USART1_TX GPIOA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA.9
//USART1_RX GPIOA.10³õʼ»¯
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë
GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA.10
//RS4851 RE/DE
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//PB0
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;// ÍÆÍìÊä³ö
GPIO_Init(GPIOB, &GPIO_InitStructure);
//UART5_TX GPIOC.12
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //PC12
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_Init(GPIOC, &GPIO_InitStructure);//³õʼ»¯PC12
//UART5_RX GPIOD.2³õʼ»¯
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;//PD2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë
GPIO_Init(GPIOD, &GPIO_InitStructure);//³õʼ»¯GPIOD.2
/* NVICÅäÖÃ */
//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3 ; //ÇÀÕ¼ÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×ÓÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷
//Uart5 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3 ; //ÇÀÕ¼ÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //×ÓÓÅÏȼ¶2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷
/* USARTÅäÖÃ */
//USART1 ³õʼ»¯ÉèÖÃ
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; //ÊÕ·¢Ä£Ê½
USART_Init(USART1, &USART_InitStructure); //³õʼ»¯´®¿Ú1
//UART5 ³õʼ»¯ÉèÖÃ
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; //ÊÕ·¢Ä£Ê½
USART_Init(UART5, &USART_InitStructure); //³õʼ»¯´®¿Ú5
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //¿ªÆô´®¿Ú1½ÓÊÜÖжÏ
USART_ITConfig(UART5, USART_IT_RXNE, ENABLE); //¿ªÆô´®¿Ú5½ÓÊÜÖжÏ
USART_Cmd(USART1, ENABLE); //ʹÄÜ´®¿Ú1
USART_Cmd(UART5, ENABLE); //ʹÄÜ´®¿Ú5
}
以上为完成配置代码。
举报