当前位置:   article > 正文

OpenMV接收stm32单片机数据_openmv代码读取单片机传来的值能否一次读取两个数

openmv代码读取单片机传来的值能否一次读取两个数


前言

本篇文章将会分享如何用OpenMV接收到stm32单片机传来的数据。

一、连线

OpenMVstm32
p4PA3
p5PA2
GNDGND

二、程序源码

1.OpenMV源码

代码如下(示例):

import sensor, image, time
from pyb import UART

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock = time.clock()

rx_buff=[]
state = 0
tx_flag = 0

uart = UART(3, 115200, timeout_char=1000)

#串口接收函数
def Receive_Prepare(data):
    global state
    global tx_flag
    if state==0:
        if data == 0x0d:#帧头
            state = 1
        else:
            state = 0
            rx_buff.clear()
    elif state==1:
        rx_buff.append(data)
        state = 2
    elif state==2:
        rx_buff.append(data)
        state = 3
    elif state == 3:
        if data == 0x5b:
            tx_flag = int(rx_buff[0])
            state = 4
    else:
        state = 0
        rx_buff.clear()

while(True):
    clock.tick()
    img = sensor.snapshot()
    if(uart.any()>0):
       c=uart.readchar()
       Receive_Prepare(c)
       print(c)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

2.32单片机源码

代码如下(示例):

#include "sys.h"
#include "usart2.h"
#include "delay.h"

u8 send_buff[2] = {1,1};

void Usart2_Sendata()
{	
	u8 i = 0;
	
	USART_SendData(USART2,0x0d);
	while( USART_GetFlagStatus(USART2,USART_FLAG_TC)!= SET);
	
	for(i = 0;i < 2;i++)
	{
		USART_SendData(USART2,send_buff[i]);
		while( USART_GetFlagStatus(USART2,USART_FLAG_TC)!= SET);
	}

	USART_SendData(USART2,0x5b);
	while( USART_GetFlagStatus(USART2,USART_FLAG_TC)!= SET);
}

int main(void)
{  	
	delay_init();
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); 
	uart2_init(115200);
	while(1)
	{
		Usart2_Sendata();
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

三、运行效果

在这里插入图片描述

总结

本篇文章分享了博主在准备电赛期间所写的OpenMV接收stm32单片机传来的数据OpenMV程序和stm32单片机程序,有不足之处,还请斧正!
完整工程源码

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/935625
推荐阅读
相关标签
  

闽ICP备14008679号