本帖最后由 遇从灵来 于 2014-6-25 15:13 编辑
请教一个串口相关的问题,代码如下:
void Uart_Port_Init(void) ?配置端口
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
// USART_DeInit(USART1);
/* Connect PXx to USARTx_Tx */
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
/* Connect PXx to USARTx_Rx */
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void Uart_Init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
USART_ClockInitTypeDef USART_Clock_InitStructure;
USART_InitTypeDef USART_InitStructure;
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_Parity_Even;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1, &USART_InitStructure);
USART_Clock_InitStructure.USART_Clock = USART_Clock_Disable;
USART_Clock_InitStructure.USART_CPOL = USART_CPOL_Low; //²»Ê¹ÓÃͬ²½Ä£Ê½
USART_Clock_InitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_Clock_InitStructure.USART_LastBit = USART_LastBit_Disable;
USART_ClockInit(USART1,&USART_Clock_InitStructure);
USART_ClearFlag(USART1,USART_IT_RXNE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ClearFlag(USART1,USART_IT_TXE);
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC);
USART_ITConfig(USART1, USART_IT_TC, ENABLE);
// USART_Cmd(USART1, ENABLE);
USART1->CR1 |= 0x0006;
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);
}
void USART1_IRQHandler(void)
{
INT8U Temp;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
Temp = USART_ReceiveData(USART1);
USART_SendData(USART1,Temp);
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
}
if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{
USART_ClearITPendingBit(USART1,USART_IT_TXE);
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
// USART1->SR &= ~(1<<7);
}
}
结果程序一直进入TXE中断,仿真时TXE一直为1,专门清也清不掉,想进入RXNE中断。反而进入不了。 |