中断程序如下:
void USART1_IRQHandler(void)
{
//接受数据
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE); //清楚标记位
//Read one byte from the receive data register
RxBuffer[RxCounter++] = USART_ReceiveData(USART1);
RxCounter = RxCounter % 8;
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
{
;
}
//溢出-如果发生溢出需要先读SR,再读DR寄存器则可清除不断入中断的问题
if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET)
{
USART_ClearFlag(USART1,USART_FLAG_ORE); //读SR其实就是清除标志
USART_ReceiveData(USART1); //读DR
}
if(USART_ReceiveData(USART1) == '\0') //判断是否接受完成
{
RxBuffer[RxCounter] = '\0';
// Disable the USART1 Receive interrupt
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
GPIO_SetBits(GPIOA,GPIO_Pin_11); //切换到发送模式
RxCounter = 0;
}
}
//发送 数据
// USART_ITConfig(USART1, USART_IT_TXE, ENABLE); 为什么 就 死了
if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{
USART_ClearITPendingBit(USART1,USART_IT_TXE); //清楚标记位
// Write one byte to the transmit data register
TxBuffer1[0] = 'A';
TxBuffer1[1] = 'C';
TxBuffer1[2] = 'K';
TxBuffer1[3] = '\0';
while(TxBuffer1[TxCounter] != '\0')
{
USART_SendData(USART1,TxBuffer1[TxCounter]);//发送ACK信息
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
{
;
}
TxCounter ++;
RxCounter = RxCounter % 8;
}
TxCounter = 0;
Delay(6000000);
TxBuffer[0] = Charg_Vin;
TxBuffer[1] = Charg_Vin >> 8;
TxBuffer[2] = Charg_Voltage;
TxBuffer[3] = Charg_Voltage >> 8;
TxBuffer[4] = Charg_Current;
TxBuffer[5] = Charg_Current >> 8;
TxBuffer[6] = Charg_Distance;
TxBuffer[7] = Charg_AutoDoor;
TxBuffer[8] = Charg_Connect;
TxBuffer[9] = '\0';
while(TxBuffer[TxCounter] != '\0')
{
USART_SendData(USART1, TxBuffer[TxCounter]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
{
;
}
TxCounter ++;
RxCounter = RxCounter % 8;
}
if(TxBuffer[TxCounter] == '\0') //判断是否发送完成
{
//Disable the USART1 Transmit interrupt
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
GPIO_ResetBits(GPIOA,GPIO_Pin_11); //切换到接受模式
TxCounter = 0;
}
}
}
希望楼主以及其余大侠给点建议,谢谢 |