最近用到了一款WIFI摄像头,这款摄像头可以通过手机app控制,从而使串口发送指定的数据,这样会以来就可以通过这款摄像头在手机app上控制小车的前后左右,还可以实现无线图传的功能。
这款摄像头通过串口发送的是16进制数据,按下前进发送0x01,后退发送0x02......以此类推。因为之前通过串口中断实现过上位机发送指定字符从而控制LED亮灭的实验,因此我通过接收指定字符的方法来判断接收到的16进制数据。
实验后发现串口中断通过字符串点灯的方法对于接收并判断16进制数据同样有效。
void uart3_init(u32 bound)
{
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE);//使能USART3串口¬GPIOB时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); //串口3时钟使能
USART_DeInit(USART3); //复位串口3
//USART3_TX PB.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB.10
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOB, &GPIO_InitStructure); //³õʼ»¯PA9
//USART3_RX PB.11
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOB, &GPIO_InitStructure);
//Usart3 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;//抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //子优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure);
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//设置波特率
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(USART3, &USART_InitStructure); //初始化串口
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启中断
USART_Cmd(USART3, ENABLE); //使能串口
}
//串口3中断服务函数
void USART3_IRQHandler(void)
{
char tmp; //接收串口数据
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET){
test_led;
USART_ClearITPendingBit(USART3,USART_IT_RXNE);
tmp = USART_ReceiveData(USART3);
if(tmp == 0x01){
CarGo();
delay_ms(300);
}
if(tmp == 0x02){
CarBack();
delay_ms(300);
}
if(tmp == 0x03){
CarLeft();
delay_ms(300);
}
if(tmp == 0x04){
CarRight();
delay_ms(300);
}
if(tmp == 0x00){
CarStop();
delay_ms(300);
}
}
}