本帖最后由 maxcontrol 于 2009-11-22 23:18 编辑
近日在配置STM32的USART1时,发现在初始化后,只要打开发送中断,程序就会立即
进入发送中断,并且会发个随机数据出来.请问,这种情况该如何处理?
我的代码如下:
/*************************************************
*函数: cfg_usart1
*功能: 配置USART1
*输入: 无
*输出: 无
*返回: 无
**************************************************/
void cfg_usart1(void)
{
USART_InitTypeDef USART_InitStructure; //定义串口初始化结构体
//清空缓冲区
rdy_buf();
/* Enable USARTx clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* USART1 configuration ------------------------------------------------------*/
/* USART1 configured as follow:
- BaudRate = 38400 baud
- Word Length = 8 Bits
- One Stop Bit
- Odd parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_StructInit(&USART_InitStructure);//2008-12-02添加
//原因是每次复位后CPOL中的值为0x3xxx的随机值
//导致停止位配置不对,比如初始化时配置成1位停止位,
//若没有此句,则会变成1.5位
USART_InitStructure.USART_BaudRate = 38400;
USART_InitStructure.USART_WordLength = USART_WordLength_9b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_Odd;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
/* Configure the USART1 */
USART_Init(USART1, &USART_InitStructure);
/* Enable the USART Transmoit compeleted interrupt: this interrupt is generated when the
USART transmit data register was transmited */
USART_ClearITPendingBit(USART1, USART_IT_TC);
USART_ITConfig(USART1, USART_IT_TC, ENABLE);
/* Enable the USART Receive interrupt: this interrupt is generated when the
USART receive data register is not empty */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
/* Enable USART */
USART_Cmd(USART1, ENABLE);
} |