串口相关配置
CPU处理串口数据---接受
void Serial_Init(int BaudRate)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//打开时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA ,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//配置引脚复用
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //发送引脚配置为复用模式让USART控制
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//GPIO口速度配置
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入配置 , 接受引脚配置为上拉输入 , 让外设进行控制
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = BaudRate;//波特率配置
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件流控制
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;//发送接受使能
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;//停止位一位
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//8位字长
USART_Init(USART2 , &USART_InitStructure);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);//打开串口2的接受中断
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//中断分组2
NVIC_InitTypeDef NVIC_InitStrcture;//NVIC配置
NVIC_InitStrcture.NVIC_IRQChannel = USART2_IRQn;//通道配置
NVIC_InitStrcture.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStrcture.NVIC_IRQChannelPreemptionPriority = 2;//越低级别越高
NVIC_InitStrcture.NVIC_IRQChannelSubPriority =2;
NVIC_Init(&NVIC_InitStrcture);
USART_Cmd(USART2,ENABLE);
}
void USART2_IRQHandler(void)//接受中断
{
//static uint8_t state;//使用状态机完成帧头帧尾检测---静态变量存放在FLASH区
if(USART_GetFlagStatus(USART2,USART_FLAG_RXNE) == SET)
{
if(USART_GetFlagStatus(USART2,USART_IT_RXNE) == SET)
{
Serial_RxData = USART_ReceiveData(USART2);//串口中断
if(Serial_RxData == '1')
{
//在这里写需要进行的操作
}
if(Serial_RxData == '2')
{
}
if(Serial_RxData == '3')
{
}
if(Serial_RxData == '4')
{
}
if(Serial_RxData == '5')
{
}
if(Serial_RxData == '6')
{
}
if(Serial_RxData == '7')
{
}
if(Serial_RxData == '8')
{
}
if(Serial_RxData == '0')
{
}
if(Serial_RxData == '9')
{
}
USART_ClearITPendingBit(USART2,USART_IT_RXNE);//清除中断标志位
}
}
}
DMP+串口中断接受(使用串口空闲中断与接收中断)