logo

openmv发送字符给51,波特率等都调好了,51接收的却不是该字符?

2020-01-06 22:53
U
_3fkd

这是openmv的代码(是教程里的代码,加了稍微的匹配修改)

# Blob Detection and uart transport
import sensor, image, time
from pyb import UART
import json
# For color tracking to work really well you should ideally be in a very, very,
# very, controlled enviroment where the lighting is constant...
yellow_threshold   = (0, 53, -118, 126, -115, -15)
# You may need to tweak the above settings for tracking green things...
# Select an area in the Framebuffer to copy the color settings.

sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565.
sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_auto_whitebal(False) # turn this off.
clock = time.clock() # Tracks FPS.

uart = UART(3, 9600)
def find_max(blobs):
    max_size=0
    for blob in blobs:
        if blob.pixels() > max_size:
            max_blob=blob
            max_size = blob.pixels()
    return max_blob

while(True):
    img = sensor.snapshot() # Take a picture and return the image.

    blobs = img.find_blobs([yellow_threshold])
    if blobs:
        max_blob=find_max(blobs)
        print('sum :', len(blobs))
        img.draw_rectangle(max_blob.rect())
        img.draw_cross(max_blob.cx(), max_blob.cy())
        
        if max_blob.cx()<=75: //当色块在左边时,是传送的数据为‘a’,下面以此类推
,          b='a'
        if max_blob.cx()>=85:
          b='b'
     #  if (max_blob.cx()>75 and max_blob.cx()<85)  
     #    b='c'
        output_str="%d" % (max_blob.cx()) #方式1
        #output_str=json.dumps([max_blob.cx(),max_blob.cy()]) #方式2
        
        print(max_blob.cx())
        print('you send:',  b)
        uart.write(b)
    else:
        print('not found!')

这是51的代码(P2_0等是led灯,P2_0=0亮,P2_0=1灭,但是接收到串口的数据时,结果都不亮,没有根据语句实现,很大可能是接收到的数据不是想的那个,该怎么解决呢)

#include<reg52.h>

#define uchar unsigned char

#define uint unsigned int

char i;

char flag,c,x;

sbit P2_0=P2^0;

sbit P2_1=P2^1;

sbit P2_2=P2^2;

sbit P2_3=P2^3;

sbit P2_4=P2^4;

void delay(unsigned char i)

{

while(i--);

}//延时函数

void delay1(unsigned int k)

{

unsigned int x,y;

for(x=0;x<k;x++)

for(y=0;y<2000;y++);

}

void init()//赋初值,定义串行口的标志位以及数码管标志位

{

TMOD=0x20;

TH1=0xfd; //使得波特率为9600,和openmv一致

TL1=0xfd;

TR1=1;

REN=1;

SM0=0;

SM1=1;

EA=1;

ES=1;

}

void main()

{

flag=0;

init();

P2_0=1;

P2_1=1;

P2_2=1;

P2_3=1;

P2_4=1;

while(1)

{

P2_0=1;//灯灭

P2_1=1;

P2_2=1;

}

}

void ser() interrupt 4//串口中断

{

RI=0;

x=SBUF;//x是接收的数据

if(x=='a')//如果接收到‘a’则亮,下面同理

{ P2_0=0;}

if(x=='b')

{ P2_1=0;}

if(x=='c')

{ P2_2=0;}

else

{P2_4=~P2_4;}

}

2020-01-07 17:32
I
网红小智智 星瞳科技

https://singtown.com/learn/50235/

先拿串口调试扩展板或者示波器看一下OpenMV的数据

然后用串口调试器测试51单片机的程序对不对。

最后看连线对不对,RX TX有没有反,GND有没有连,有没有供电。