K210代码
import sensor, image, time, lcd
from fpioa_manager import fm
from machine import UART
# binding UART2 IO:6->RX, 8->TX
fm.register(6, fm.fpioa.UART2_RX)
fm.register(8, fm.fpioa.UART2_TX)
uart_A = UART(UART.UART2, 115200, 8, 1, 0, timeout=1000, read_buf_len=4096)
#串口发送函数
def usart_sendResult(a,b):
data = bytearray([0xa3,0xb3, a, b, 0xc3]) #帧头 + 帧头 + a + b + 帧尾
uart_A.write(data) #串口发送
print("a =", a) #终端显示
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA) #160*120
sensor.skip_frames(time = 2000)
lcd.init(freq=15000000) #初始化lcd
lcd.rotation(2) #lcd旋转(0-3)
a=0
b=0
while(True):
img = sensor.snapshot() #拍照
lcd.display(img) #lcd显示
usart_sendResult(a,b)
a=a+1
b=b+1
time.sleep_ms(100)
M0端代码
#include "ti_msp_dl_config.h"
#include "oled.h"
#include "stdio.h"
int openmv_data[5]; //openmv发来的数据包
int a1=5, a2=5,a3=5;
int b=0;
int data_test(int data[]) //判断数据是否合理的函数
{
if(data[4]!=0xc3) return 0; //帧尾
//if(data[2]>150) return 0; //x坐标上限
//if(data[3]>110) return 0; //y坐标上限
return 1;
}
int main(void)
{
uint8_t str[64];
SYSCFG_DL_init();
NVIC_ClearPendingIRQ(UART_0_INST_INT_IRQN); //先清除一下中断,防止直接接入中断
NVIC_EnableIRQ(UART_0_INST_INT_IRQN); //开启串口的中断
OLED_Init();
OLED_Fill(0x00);
OLED_ShowStr(0,0,(unsigned char*)("MSPM0G3507"),1);
while (1)
{
sprintf((char *)str, "%d", a1);
OLED_ShowStr(0,2, str,2);
sprintf((char *)str, "%d", a2);
OLED_ShowStr(0,4, str,2);
sprintf((char *)str, "%d", a3);
OLED_ShowStr(100,0, str,2);
sprintf((char *)str, "%d", b);
OLED_ShowStr(0,6, str,2);
b++;
}
}
void UART_0_INST_IRQHandler(void)
{
static int i=0;
switch (DL_UART_Main_getPendingInterrupt(UART_0_INST))
{
case DL_UART_MAIN_IIDX_RX:
openmv_data[i++] = DL_UART_Main_receiveData(UART_0_INST); //接收数据
if(openmv_data[0]!=0xa3) i=0; //判断第一个帧头
if((i==2)&&(openmv_data[1]!=0xb3)) i=0; //判断第二个帧头
if(i==5) //代表一组数据传输完毕
{
i = 0;
a3=data_test(openmv_data);
if( data_test(openmv_data) ) //判断数据合理性
{
a1 = openmv_data[2];
a2 = openmv_data[3];
}
}
break;
default:
break;
}
}