程序部分
串口定义
void UART2_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
UART2_GPIO_Configuration();
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(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART2, USART_IT_TC, ENABLE);
USART_ITConfig(USART2, USART_IT_PE, ENABLE);
USART_ITConfig(USART2, USART_IT_ERR, ENABLE);
//GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);
USART_Cmd(USART2, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
中断函数
void RTU_USART2_IRT()
{
u8 date;
if(USART_GetITStatus(USART2, USART_IT_RXNE)==SET)
{
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
date=USART_ReceiveData(USART2);
if((RecvBufferLength<BUF_MAX-3)&&SendTaskDoing==FALSE)
{
RecvBuffer[RecvBufferLength++]=date;
}
}
if(USART_GetFlagStatus(USART2,USART_FLAG_ORE)==SET)
{
USART_ClearFlag(USART2,USART_FLAG_ORE); //¶ÁSRÆäʵ¾ÍÊÇÇå³ý±êÖ¾
USART_ReceiveData(USART2); //¶ÁDR
}
if(USART_GetITStatus(USART2, USART_IT_TXE)==SET)
{
USART_ClearITPendingBit(USART2,USART_IT_TXE);
USART_ITConfig(USART2,USART_IT_TXE,DISABLE);
}
if(USART_GetFlagStatus(USART2, USART_FLAG_NE) != RESET)
{
USART_ClearFlag(USART2, USART_FLAG_NE);
}
if(USART_GetFlagStatus(USART2, USART_FLAG_FE) != RESET)
{
USART_ClearFlag(USART2, USART_FLAG_FE);
}
if(USART_GetFlagStatus(USART2, USART_FLAG_PE) != RESET)
{
USART_ClearFlag(USART2, USART_FLAG_PE);
}
if(USART_GetITStatus(USART2, USART_IT_TC)==SET)
{
USART_ClearITPendingBit(USART2, USART_IT_TC);
if(SendBufferIndex<SendBufferLength)
{
USART_SendData(USART2, SendBuffer[SendBufferIndex++]);
}
else
{
SendTaskDoing=FALSE;
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
UART2_SendEnable(FALSE);
}
}
}
|