Openmv and stm32 serial communication completes QR code recognition

Openmv and stm32 serial communication completes QR code recognition


Preface

Note: I'm just a freshman, this article only completes the basic functions, I hope to help people who are confused (I also just walked out of the mystery, don't spray.) During the
work training competition, I suddenly learned the QR code recognition ( Based on openmv4). Openmv4 completes communication with stm32f103 through serial communication and json, and the result is displayed on the LCD screen connected to stm32

1. The hardware used:

Openmv4, punctual atom mini board (stm32f103rct6), punctual atom supporting LCD screen.

Two, openmv end

Directly on the code:

The code is as follows (example):

mport sensor, image, time
from pyb import UART

uart = UART(3, 115200)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA) # can be QVGA on M7...
sensor.skip_frames(30)
sensor.set_auto_gain(False) # must turn this off to prevent image washout...
while(True):
    img = sensor.snapshot()
    img.lens_corr(1.8) # strength of 1.8 is good for the 2.8mm lens.
    for code in img.find_qrcodes():
        print(code)
        FH= bytearray([0xb3,0xb3])
        uart.write(FH)
        uart.write(code.payload())
        FH = bytearray([0x0d,0x0a])
        uart.write(FH)
        time.sleep_ms(1000)

Note:
1. P4 of openmv4 is TX and p5 is RX. (Don't connect the wrong wire)
2. FH is the frame header to check whether the data is sent correctly. (If you don't understand, you can understand with the 32-terminal code)
3. The characters in the QR code recorded by the code.payload() function.
4. Set the baud rate to 115200 (to be consistent with the 32-terminal).
5. The detailed code for QR code recognition can be found on the official website of openmv.

2.stm32 end

main.c :

#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "lcd.h"


 int main(void)
 {
    
    		
 	u8 len;
	u8 t,n;
	u8 UsartDisPlay[200];
	uart_init(115200);
	 delay_init();
	 LCD_Init();
	POINT_COLOR=RED; 
while(1)
{
    
    
   if(USART_RX_STA&0x8000)
   {
    
    
       len=USART_RX_STA&0x3fff;
		 for(t=0;t<len;t++)
		  {
    
     if(USART_RX_BUF[t]==0xb3)
				 if(USART_RX_BUF[t]==0xb3) n=t+1;
			}
			for(t=0;n<len;t++,n++)
			{
    
    UsartDisPlay[t]=USART_RX_BUF[n];
				delay_ms(3000);
			 }
			USART_RX_STA=0;
    }
	 
LCD_ShowString(15,50,260,16,16,UsartDisPlay);
	
}	
	  

usart1 configuration:

void uart_init(u32 bound){
    
    
  //GPIO端口设置
  GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);	//使能USART1,GPIOA时钟
  
	//USART1_TX   GPIOA.9
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	//复用推挽输出
  GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9
   
  //USART1_RX	  GPIOA.10初始化
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
  GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10  

  //Usart1 NVIC 配置
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;		//子优先级3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ通道使能
	NVIC_Init(&NVIC_InitStructure);	//根据指定的参数初始化VIC寄存器
  
   //USART 初始化设置

	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(USART1, &USART_InitStructure); //初始化串口1
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断
  USART_Cmd(USART1, ENABLE);                    //使能串口1 

}

Interrupt function:

if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)  //接收中断(接收到的数据必须是0x0d 0x0a结尾)
		{
    
    
		Res =USART_ReceiveData(USART1);	//读取接收到的数据
		
		if((USART_RX_STA&0x8000)==0)//接收未完成
			{
    
    
			if(USART_RX_STA&0x4000)//接收到了0x0d
				{
    
    
				if(Res!=0x0a)USART_RX_STA=0;//接收错误,重新开始
				else USART_RX_STA|=0x8000;	//接收完成了 
				}
			else //还没收到0X0D
				{
    
    	
				if(Res==0x0d)USART_RX_STA|=0x4000;
				else
					{
    
    
					USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
					USART_RX_STA++;
					if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//接收数据错误,重新开始接收	  
					}		 
				}
			}   		 
     } 

Note:
1. PA9 is TX, PA10 is RX.
2. Openmv and stm32 must be connected to GND together.


Insert picture description here

Insert picture description here
Insert picture description here

to sum up

If you want the source code, you can leave the mailbox in the comment area. I just started, please correct me if you have any mistakes.

Guess you like

Origin blog.csdn.net/qq_51621579/article/details/115057847