您的位置 首页 FPGA

STM32串口发送问题

在串口1发送中,第一个字总是发不出来,单步调试发送正常,硬件上电复位发送不正常,发现是STM32上电复位后需要先读取USART_SR,然后写USA…

在串口1发送中,榜首个字总是发不出来,单步调试发送正常,硬件上电复位发送不正常,发现是STM32上电复位后需求先读取USART_SR,然后写 USART_DR,解决办法在串口初始化参加USART_GetFlagStatus(USART2, USART_FLAG_TC);

void USART1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
// NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);

// NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //

// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //
// NVIC_Init(&NVIC_InitStructure);

// – BaudRate = 115200 baud
// – Word Length = 8 Bits
// – One Stop Bit
// – No parity
// – Hardware flow control disabled (RTS and CTS signals)
// – Receive and transmit enabled
USART_InitStructure.USART_BaudRate = 9600;
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(USART1, &USART_InitStructure);

// USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//敞开中止

USART_GetFlagStatus(USART1, USART_FLAG_TC);

}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/fangan/fpga/277469.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部