OpenMv与stm32简单串口通信

前言

    刚开始学openmv与32串口通信,我是用的stm32f103的板子。开始想简单发送字符或者16位数试验一下,然后就遇到了个小问题,即openmv和单片机可以分别和电脑通信,但是让他俩通信的话就不行,随后解决,在此记录。

我的思路是用openmv给单片机每秒发送五个bit的数据,然后单片机每接受一次led灯就翻转一次。

1 发送字符

    这是单片机主程序: 用的正点原子的串口实验例程,只是主程序加了个led灯的翻转。

 int main(void)
 {	
	u8 t;
	u8 len;	
	u16 times=0; 
 
	delay_init();	    	 //延时函数初始化	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 设置中断优先级分组2
	uart_init(9600);	 //串口初始化为9600
	LED_Init();		  	 //初始化与LED连接的硬件接口 
 
	while(1)
	{
		if(USART_RX_STA&0x8000)
		{					   
			len=USART_RX_STA&0x3fff;//得到此次接收到的数据长度
			if(len==5)LED0=!LED0;
			printf("\r\n您发送的消息为:\r\n");
			for(t=0;t<len;t++)
			{
				USART1->DR=USART_RX_BUF[t];
				while((USART1->SR&0X40)==0);//等待发送结束
			}
			printf("\r\n\r\n");//插入换行
			USART_RX_STA=0;
		}

	}	 
}

由于单片机接收到的数据需要以0x0d,0xad结尾作为接受完成判断的,如果是字符的话就是回车换行。而用电脑与单片机实验,给单片机发数据时,电脑发的数据是在后面自动加回车和换行的,也就是说我用电脑发 hello=用openmv发 hello\r\n。

 这是openmv的程序:

import time
from pyb import UART

# Always pass UART 3 for the UART number for your OpenMV Cam.
# The second argument is the UART baud rate. For a more advanced UART control
# example see the BLE-Shield driver.
uart = UART(3, 9600)
while(True):
    uart.write("Hello\r\n")
    #data=bytearray([0x80,0x06,0x02,0x78,0x23,0x0d,0x0a])
    #uart.write(data)
    time.sleep_ms(1000)
    

现在上面openmv程序发送的是正确的,为5个字符。

2 发送十六位数据

    单片机程序不用变,只需改动openmv程序即可。

import time
from pyb import UART

# Always pass UART 3 for the UART number for your OpenMV Cam.
# The second argument is the UART baud rate. For a more advanced UART control
# example see the BLE-Shield driver.
uart = UART(3, 9600)
while(True):
    #uart.write("Hello\r\n")
    data=bytearray([0x80,0x06,0x02,0x78,0x23,0x0d,0x0a])
    uart.write(data)
    time.sleep_ms(1000)

同理,发送16位数据需要在后面加上0x0d,0xad即可。

 参考博主文章:openmv 发送16进制数_zzzzjh的博客-CSDN博客

  • 14
    点赞
  • 134
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
你可以通过串口通信将数字从OpenMV送到STM32。首先,确保OpenMVSTM32通过串口连接,并且它们的波特率设置相同。然后,在OpenMV的代码中,你可以使用`uart`模块来送数字。以下是一个简单的示例代码: ```python import sensor import image import time from pyb import UART # 初始化串口 uart = UART(3, 115200) # 选择正确的串口和波特率 # 捕获图像并处理 sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time=2000) while(True): img = sensor.snapshot() # 在这里进行图像处理和数字计算 # 送数字到STM32 number = 123 # 要送的数字 uart.write(str(number)) # 送数字的字符串表示 time.sleep(100) ``` 在STM32的代码中,你需要设置相同的波特率,并使用相应的串口接收数据。以下是一个简单的示例代码: ```c #include "stm32f4xx.h" #include "stm32f4xx_usart.h" // 初始化串口 void USART3_Init(void) { USART_InitTypeDef USART_InitStruct; GPIO_InitTypeDef GPIO_InitStruct; RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStruct); GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3); GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3); USART_InitStruct.USART_BaudRate = 115200; USART_InitStruct.USART_WordLength = USART_WordLength_8b; USART_InitStruct.USART_StopBits = USART_StopBits_1; USART_InitStruct.USART_Parity = USART_Parity_No; USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART3, &USART_InitStruct); USART_Cmd(USART3, ENABLE); } // 接收数字 int32_t receiveNumber(void) { while (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET) {} return (int32_t)USART_ReceiveData(USART3); } int main(void) { USART3_Init(); while (1) { // 接收数字并进行处理 int32_t receivedNumber = receiveNumber(); // 在这里进行数字处理 // 根据需要执行其他操作 } } ``` 这只是一个简单的示例,具体的实现可能需要根据你的具体需求进行修改。另外,请注意在使用串口通信时,确保OpenMVSTM32的电平兼容性,以避免损坏设备。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十万空击

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值