- //GPIO初始化
- void USART2_init(u32 bound)
- {
- //GPIO端口设置
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStruct;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
-
- //USART2_TX
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- //USART2_RX
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- //USART 初始化设置
- USART_InitStructure.USART_BaudRate = bound;//一般设置为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_Cmd(USART2, ENABLE); //使能串口
-
- NVIC初始化
- MY_NVIC_PriorityGroupConfig(2);
- NVIC_InitStruct.NVIC_IRQChannel=USART2_IRQn;
- NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
- NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=0;
- NVIC_InitStruct.NVIC_IRQChannelSubPriority=0;
- NVIC_Init(&NVIC_InitStruct);
-
- }
- u8 Fore,Back,Left,Right;
- void USART2_IRQHandler(void) //先尝试能不能进中断 不能再放外边
- {
- char Bluetooth_data;
- if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)//接收中断标志位拉高
- {
- Bluetooth_data=USART_ReceiveData(USART2);//保存接收的数据
- if(Bluetooth_data==0x5A) Fore=0,Back=0,Left=0,Right=0;//刹
- else if(Bluetooth_data==0x41) Fore=1,Back=0,Left=0,Right=0;//前
- else if(Bluetooth_data==0x45) Fore=0,Back=1,Left=0,Right=0;//后
- //else if(Bluetooth_data==0x42) Fore=1,Back=0,Left=1,Right=0;//右前
- else if(Bluetooth_data==0x47) Fore=0,Back=0,Left=1,Right=0;//左
- else if(Bluetooth_data==0x43) Fore=0,Back=0,Left=0,Right=1;//右
- else Fore=0,Back=0,Left=0,Right=0;//刹
- }
-
- }
- //一个字符
- void USART2_Send_Data(char data)
- {
- USART_SendData(USART2,data);
- while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=1);
- }
- //一串字符
- void USART2_Send_String(char *String)
- {
- u16 len,j;
-
- len=strlen(String);
- for(j=0;j<len;j++)
- {
- USART2_Send_Data(*String++);
- }
- }
-
- if((Fore==0)&&(Back==0))
- Target_Speed=0;//未接受到前进后退指令-->速度清零,稳在原地
- if(Fore==1)
- Target_Speed--;//前进1标志位拉高-->需要前进
- if(Back==1)
- Target_Speed++;//
-
- /*左右*/
- if((Left==0)&&(Right==0))
- Turn_Speed=0;
- if(Left==1)
- Turn_Speed+=30; //左转
- if(Right==1)
- Turn_Speed-=30; //右转
-
-
- /*转向约束*/
- if((Left==0)&&(Right==0))Turn_Kd=-0.8;//若无左右转向指令,则开启转向约束
- else if((Left==1)||(Right==1))Turn_Kd=0;//若左右转向指令接收到,则去掉转向约束
|