赞
踩
OpenMV与STM32之间使用串口进行通信。
详细解释可以看代码里面的注释。
注意:转换成字节型传输时,负数会以补码形式传输,比如:-1在单片机接受时变成255.
import sensor, image, time, math#调用声明 from pyb import UART #串口 uart = UART(3, 115200, timeout_char=1000) # i使用给定波特率初始化 uart.init(115200, bits=8, parity=None, stop=1, timeout_char=1000) #这一行一定要写!! #设置帧头和帧尾,便于接受方接收数据 u_start=bytearray([0xb3,0xb3]) u_over=bytearray([0x0d,0x0a]) # Camera setup... sensor.reset() # Initialize the camera sensor. sensor.set_pixformat(sensor.RGB565) # use grayscale. sensor.set_framesize(sensor.QVGA) # use QQVGA for speed. sensor.skip_frames(30) # Let new settings take affect. sensor.set_auto_gain(False) # must be turned off for color tracking sensor.set_auto_whitebal(False) # must be turned off for color tracking #关闭白平衡 clock = time.clock() # Tracks FPS. mark=([-1,-1],[-1,-1],[-1,-1]) while(True): times=0 clock.tick() # Track elapsed milliseconds between snapshots(). img = sensor.snapshot()# Take a picture and return the image. row_data=[0,0,0,0] #row_data里面存放需要返回的数据,长度可以根据需要调整 #print(row_data) uart_buf = bytearray(row_data) #转换成字节型 uart.write(u_start) #先发帧头,再发数据,最后发帧尾 uart.write(uart_buf) uart.write(u_over) #print(clock.fps())
先在项目中加入串口通信包,PA2为TXD,PA3为RXD。可以开一个串口通过串口助手打印接收到的数据。
注意:OpenMV的TXD和RXD口可以查看官网的手册,OpenMV的TXD接单片机RXD,OpenMV的RXD接单片机TXD。!!小心OpenMV给单片机反向供电,OpenMV通过数据线接电脑时务必把VCC线拔掉。!!注意共地,不然数据传不过去。
UART2.c
#include "sys.h" #include "USART2.h" #include "stm32f4xx.h" u8 USART2_RX_BUF[USART2_REC_LEN]; u16 USART2_RX_STA=0; void uart2_init(u32 bound){ //GPIO???? GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); //??2???????? GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2); GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA,&GPIO_InitStructure); //??? //??2????? USART_InitStructure.USART_BaudRate = bound;//????? USART_InitStructure.USART_WordLength = USART_WordLength_8b;//???8????? 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); //?????2 //??2 NVIC ?? NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;//??2???? NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//?????3 NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //????3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ???? NVIC_Init(&NVIC_InitStructure); //??????????VIC??? USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//?????? USART_Cmd(USART2, ENABLE); //????1 } void USART2_IRQHandler(void) //??2?????? { u8 Res; if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //????(?????????0x0d 0x0a??) { Res =USART_ReceiveData(USART2);//(USART1->DR); //???????? if((USART2_RX_STA&0x8000)==0)//????? { if(USART2_RX_STA&0x4000)//????0x0d { if(Res!=0x0a)USART2_RX_STA=0;//????,???? else USART2_RX_STA|=0x8000; //????? } else //???0X0D { if(Res==0x0d)USART2_RX_STA|=0x4000; else { USART2_RX_BUF[USART2_RX_STA&0X3FFF]=Res ; USART2_RX_STA++; if(USART2_RX_STA>(USART2_REC_LEN-1))USART2_RX_STA=0;//??????,?????? } } } } }
UART2.h
#ifndef __USART2_H
#define __USART2_H
#include "stdio.h"
#include "stm32f4xx_conf.h"
#include "sys.h"
#define USART2_REC_LEN 200
extern u8 USART2_RX_BUF[USART2_REC_LEN];
extern u16 USART2_RX_STA;
void uart2_init(u32 bound);
#endif
main.c
#include "stm32f4xx.h" #include "usart.h" #include "delay.h" #include "initGPIO.h" #include "USART2.h" #include "math.h" #include "stdlib.h" #define num_pul 1 #define move_time_ms 1 #define len_things 4 #define delay_time_ms 5 extern u16 TIM5_Flag; u8 row_data[200];//接受已经收到的数字; void get_data()//获得PA2、PA3传回的数据; { u8 t,n=0; u8 len=0; if(USART2_RX_STA&0x8000) { len=USART2_RX_STA&0x3fff; for(t=0;t<len;t++)//检测到开头数字,并开始记录数据; { if(USART2_RX_BUF[t]==0xb3) { if(USART2_RX_BUF[t+1]==0xb3) { n=t+2; break; } } } for(t=0;n<len;t++,n++) { row_data[t] = USART2_RX_BUF[n]; printf("row_data%d:%d ",t+1,row_data[t]); } printf("\r\n"); USART2_RX_STA=0; } } int main(void) { uart2_init(115200); uart_init(115200); while(1) { //后续操作 } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。