中断服务子程序:
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;
}
}
}
int main()
{
while(1)
{
;
}
}
初始化如下:
void ChargeCTRL_RS485_Init(void)
{
//RCC
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
//RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //P208
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
//GPIO
GPIO_InitTypeDef GPIO_InitStructure;
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);
//GPIO_ResetBits(GPIOB,GPIO_Pin_10);
//485TRN
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//GPIO_SetBits(GPIOA,GPIO_Pin_11);//PA.11 = 1 处于发送状态
GPIO_ResetBits(GPIOA,GPIO_Pin_11);//PA.11 = 0 处于接受状态
//USART
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_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
//CLOCK
USART_ClockInitTypeDef USART_ClockInitStructure;
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
USART_ClockInit(USART1,&USART_ClockInitStructure);
//USART_ClearFlag(USART1, USART_FLAG_TC);
//USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
// USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
USART_Cmd(USART1, ENABLE);
//NVIC
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //通道设置为串口1中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //中断占先等级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //中断响应优先级0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断
NVIC_Init(&NVIC_InitStructure);
} |
|