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博客

版权声明:本文为CSDN博主「杰森斯坦森二号」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_57747837/article/details/120751141

前言

    刚开始学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博客

版权声明:本文为CSDN博主「杰森斯坦森二号」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_57747837/article/details/120751141

生成海报
点赞 0

杰森斯坦森二号

我还没有学会写个人说明!

暂无评论

发表评论

相关推荐

电脑不识别STM32的USB虚拟串口

电脑不识别STM32的USB虚拟串口 现象 板子和电脑联调的时候发现,USB线插入板子以后电脑不识别虚拟串口,通过禁用设备再启用,可以正常工作。也可以按一下复位键才能识别。 以前似乎没有这个问题&#