Ultra detailed communication between openmv and STM32 MCU with complete source code

Time:2022-5-28
catalogue
  • 1. preface (gossip)
  • 2. hardware connection
  • 3. software code ―――OpenMVend
  • 4. software code ―――STM32end
  • 5. use PC to test whether data transmission and reception are normal
  • 6. learning supplement (you can take a look when you don’t understand the code)
  • 8. blog update
  • 9. reference links
  • 10. full version code link

 

1. preface (gossip)

Recently, I was working on an electromagnetic gun. I found that color tracking was needed for the problem, so I spent some time learning openmv. It is not enough to only learn openmv, but also need to implement andsinglechipofcommunication, I thought it was very simple. I found some codes on CSDN, modified and pasted them directly, and understood the codes. It took only a few hours. I thought I had figured out the communication between the two, but I made a mistake when transferring the openmv data to the MCU later. I chose to use OLED to display the transmitted data. After debugging here for a long time, I encountered many previous learning vulnerabilities. I hereby write a blog to record my learning experience.*


 

2. hardware connection

The materials I used are as follows: four pin IIC OLED, openmv (ov7725), stm32f103c8t6 minimum system board, N data lines (openmv data lines can only be used by the official, and other basic lines can not be used), and several DuPont lines.

1. Openmv end: UART is known from the figure_ RX ― P5 —- UART_ Tx-p4

Ultra detailed communication between openmv and STM32 MCU with complete source code

2.stm32 end: USART_ TX ― PA9 —-usart_ Rx-pa10

Ultra detailed communication between openmv and STM32 MCU with complete source code

3. four pin OLED IIC connection: sda-pa2—scl-pa1 because the analog IIC is used instead of the hardware IIC, you can modify the IO port to control the SDA line and SCL line according to your personal needs. You only need to modify the code.
4. The TX (Rx) of STM32 is connected to the RX (TX) of openmv, and the OLED can be connected to STM32.


 

3. software code – openmv end

Import sensor, image, time, math, PyB

Bytearray ([,,]) combines uart The function of write() and direct call to send_ Data (CX, cy, CW, CH) has the same effect


 

4. software code – STM32 end

The project contains the following files in total: main c. Iic c. Iic h. Oled c. Oled h. Uart c. Uart H. Due to the copyright problem of OLED code, you can send it privately through email.

/***** oled.h *****/

#Ifndef__ USART_ H

/***** oled.c *****/

#Include "uart.h"

Explanation: openmv sends data packets to STM32. STM32 uses interrupts to receive data and store the data in the rxbuffer1 array. OLED is used to display the four coordinates of CX, cy, CW and Ch in interrupts. In the interrupt, there are the following functions:

Else if (rxstate==2)

Rxbuffer1 is an array that receives openmv data. Rxcounter1 acts as a counter. When the data stored in rxbuffer[rxcounter1-1] is the frame bit of the packet, it indicates that the entire packet has been successfully received. At this time, rxbuffer[rxcounter1-2] stores the ch coordinate value, rxbuffer[rxcounter1-3] stores the CW coordinate value, rxbuffer[rxcounter1-4] stores the CY coordinate value, and rxbuffer[rxcounter1-5] stores the CX coordinate value, Then display these four coordinates in the rxstate=3 process.
It should be noted that each time an STM32 interrupt occurs, only one byte of data will be received. Therefore, a whole frame of data packets will not be received until seven times. This needs to be carefully considered by the reader. In combination with the static variable keyword mentioned above, the following definitions are defined:

u8 com_data; 
u8 i;
static u8 RxCounter1=0;
static u8 RxBuffer1[10]={0};
static u8 RxState = 0;	
static u8 RxFlag1 = 0;

Please carefully figure out why com_ Data (data received by the port) and I are defined as dynamic (auto), while rxbuffer1 (static global array loaded with received data), rxstate (State Flag variable) and rxflag1 (accept end flag variable) are defined as static, which is not difficult to understand.


 

5. use PC to test whether data transmission and reception are normal

During the communication test between openmv and STM32, I used the USB to TTL module to communicate between openmv (or STM32) and PC to ensure that the data is sent or received normally.
OpenMV&&PC
Openmv_ RX connected to module TX
Openmv_ TX connected to module RX
Openmv_ GND connected to module GND
Then open openmv and use the statement in the large loop while (true):

DATA=bytearray[(1,2,3,4,5)]
uart.write(DATA)

Open the PC serial port assistant, pay attention to setting the same baud rate, stop bit, number of bytes sent, etc., and check whether the serial port assistant has received data.
STM32&&PC
Stm32_ RX connected to module TX
Stm32_ TX connected to module RX
Stm32_ GND connected to module GND
Note: whether STM32 communicates with PC, openmv communicates with PC, or STM32 communicates with openmv, the GND of the two must be connected together.
In main C, call stdio header file first, and use the following statements in the large loop:

while(1)
{
		printf("HelloWorld!");
}

Open the serial port assistant to check whether data has been received.


 

6. learning supplement (you can take a look when you don’t understand the code)

Supplement 1: use of static keyword (static variable)

Static modifies global functions and global variables. It can only be used in this source file. For example, use the following statementstatic u8 RxBuffer[10]A static array named rxbuffer is defined. The array element type is unsigned char. In the source file containing rxbuffer, rxbuffer is equivalent to a global variable. If you modify the value of rxbuffer anywhere, rxbuffer will change accordingly. furthermoreThe rxbuffer value of a function containing rxbuffer is always saved after multiple runs (unless re assigned). In C language learning, factoring with the static keyword is a good example:

#include“stdio.h”
long fun(int n);
void main()
{
    int i,n;
    printf("input the value of n:");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        printf("%d! = %1d
",i,fun(i));
    }
}
>long fun(int n)
{
    static long p=1; 
    p=p*n;
    return p;
}

The effect is to output n in sequence! (n=1, 2, 3… N)
In this example, the first time the value of P is 1, and the second time the value of P becomes p x n=1 x 2=2. This value will always be saved. If P is not defined as a static type, the value of P will be re assigned to 1 after the first operation. This is the biggest difference between auto type (not declaring auto type by default) and static type.

Summary: the variable defined by the static keyword is a global variable. When the function contained in static runs multiple times, the variable will not be initialized multiple times, but only once.

Supplement 2: use of extern keyword (external variable)

The compilation unit of a program is the source program file. A source file can contain one or several functions. Variables defined inside a function are local variables, while variables defined outside a function are called external variables. External variables are what we call global variables. Its storage mode is static storage, and its life cycle is the life cycle of the whole program.The global variable can be shared by other functions in this file. Its valid range is from the position where the variable is defined to the end of this source file.
If the whole project is composed of multiple source files, if you want to refer to an external variable defined in another source file in one source file, you just need to use the extern keyword in the file referencing the variable. Here is an example of multiple files:

/****Max.c****/

For projects with multiple files, the above method can be used. For modular program files, the interface of external variables can be reserved in the file in advance, that is, only extern is used to declare variables without defining variables. G in max.c file_ X and G_ That’s how y works. For example, you want to call usart Variable x in C, usart C has such a definition:static u8 x=0At usart H can be written as follows:extern u8 xIn main Usart H header file, which will be in main Call x external variable in C.

Summary: the extern keyword is an external variable. It is a static global variable. You can call variables in other files in the source file and use it with header files in multi file projects.

Supplement 3: explanation of some library functions of micropython

1.ustruct Pack function:
Import ustruct, in ustruct

Data = ustruct Pack ("<bbhhhhb", \

“” bbhhhhb “To put it simply, it is a statement to send data. There are seven bbhhb, which means seven data are sent. Comparing with the following table, we can know that the seven data are sent as unsigned char, unsigned char, short, short, short and unsigned char in sequence. 0x2c is the frame header of the data frame, that is, the beginning of the data flow is detected, but one frame header may be accidental. Therefore, two frame headers 0x2c and 0x12 are set to detect in the interrupt Test whether the frame header is detected to store useful data. 0x5b is the end of the frame, that is, the end of the data frame.

Ultra detailed communication between openmv and STM32 MCU with complete source code

2.bytearray ([,,]) function:
It is used to store hexadecimal data in byte array in order to send it out in the form of data frame for communication.

FH = bytearray([0x2C,0x12,cx,cy,cw,ch,0x5B])
uart,write(FH)

7. effect display (you can see the effect first)

Ultra detailed communication between openmv and STM32 MCU with complete source code

CX, cy, CW, ch from top to bottom


 

8. blog update

1. a friend reported that an error would be reported if the openmv terminal could not find the color block. The solution is as follows:

While (true):

In the above code, theMax_ B = find_ Move max (blobs) outside if blobs


2. a friend reported that only one byte of openmv data can be sent, that is, data larger than 255 cannot be directly completed through the code. Now the following solution is provided: save the upper eight bits and the lower eight bits of numbers larger than 255 in the STM32 end code, and finally combine them together.
2021/9/15 update conversion between 4 bytes and floating point numbers (Reference)

#If 1

The above code realizes the function of converting four bytes into a floating-point number, and also realizes the function of splitting a floating-point number into four bytes. When openmv transmits data, only one byte can be transmitted. Data greater than 255 cannot be sent in the form of one byte. Therefore, the data can be split into two bytes at the openmv end and sent to STM32 end respectively. At the same time, STM32 end synthesizes the transmitted data, synthesizes and parses it into corresponding data.
Another solution: Python transmits 1/2 of the data, and the single-chip microcomputer multiplies 2.


 

9. reference links

[1] Extern external variable reference link
[2] Star pupil technology openmv Chinese reference manual official
[3] Micropython Library


 

10. full version code link

Link to the full version of the code (it’s free to like collections)

This is the end of this article about the ultra detailed communication between openmv and STM32 single chip microcomputer with complete source code. For more information about the communication between openmv and STM32 single chip microcomputer, please search the previous articles of developeppaper or continue to browse the following articles. I hope you will support developeppaper in the future!

Recommended Today

Why is reids fast

1. What is redis? Redis is completely open source and complies with the BSD protocol. It is a high-performance key value database. Redis is also one of the most popular NoSQL databases at present. It contains a variety of data structures, supports network, is memory based, and has an optional key value pair storage database […]