码迷,mamicode.com
首页 > 其他好文 > 详细

STM32串口发送问题

时间:2015-05-07 16:36:24      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

在串口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);

}

STM32串口发送问题

标签:

原文地址:http://www.cnblogs.com/duguqiuying/p/4485039.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!