mv–115200 8a8k—串口3,P00-rxd3,P01txd3— mv连接 8a8k–串口1电脑串口助手,调试。
8a8k发送 ReccmdA给openmv, mv收到之后,进行判断,然后
mv端代码先发送 LED=0#, 再发送angle-x=1.3#给8a8k。
8a8k串口3收到数据,判断LED=0,再提取数据(在超时之内),将数据发送给mv,print出来,同时串口1发给串口助手,打印出来。测试数据是不是正常。
mv端代码
# Untitled - By: Administrator - 周四 四月 7 2022
import sensor, image, time
from pyb import UART
from pyb import Pin, Timer, LED
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
#uart=UART(1,9600) #PA9 txd PA10--rxd 微翻weact 版OPENMV
uart = UART(3, 115200) #正式openmv
uart.init(115200, bits=8, parity=None, stop=1) # init with given parameters timeout_char=1000
clock = time.clock()
g_uart_cmdA_flag=0 # 将变量声明为全局变量,如此才可改变其数值
g_uart_cmdB_flag=0
def Uart_recv(): # 串口接收数据
global g_uart_cmdA_flag
global g_uart_cmdB_flag
if (uart.any()): # 更新串口接收数据
recv_data = eval(str(uart.read()))
print(recv_data)
#uart.write(recv_data)
if ("Rec" in recv_data) :
print("Openmv has recved CMD data.")
if ("cmdA" in recv_data):
g_uart_cmdA_flag = 1
print("Ready for cmdA !")
if ("cmdB" in recv_data):
g_uart_cmdB_flag = 1
print("Ready for cmdB !")
#--------------------------------定时器部分 start
is_need_send_data=False
def uart_time_tick(timer):
global is_need_send_data
# uart.write("tick = true ^^^^^^\r\n ")
is_need_send_data=True
##5ms读取一次检测一次串口,t=5ms f=1/t=200hz
tim = Timer(4, freq=5) # create a timer object using timer 2 - trigger at 100Hz
tim.callback(uart_time_tick) # set the callback to our tick function
#--------------------------------定时器结束
angle_x =10.2
xmm=896
#--------------------------------
while(True):
clock.tick()
if is_need_send_data==False:
continue #注意continue用法,退出当前的循环一次,不执行后面的语句
if is_need_send_data==True:
is_need_send_data =False
# uart.write("is_need_send_data = False *******\r\n ")
Uart_recv()
#time.sleep(500)
img = sensor.snapshot()
if g_uart_cmdA_flag ==1 :
# uart.write("loc"+(str)adata+"num"+(str)bdata+"#") # 串口发送
# time.sleep(1000)
uart.write("LED=0#")
time.sleep(2)
uart.write("angle_x=%0.2f#"%(angle_x))
angle_x=angle_x+1.2
if angle_x>2000:
angle_x=10.2
g_uart_cmdA_flag = 0
if g_uart_cmdB_flag ==1 :
#time.sleep(1000)
uart.write("LED=1#")
uart.write("xmm=%d#"%(xmm))
g_uart_cmdB_flag = 0
8a8k端代码 22.1184M,115200波特率,定时器2和定时器3都被用了
#include <stc8a8k.h>
#include <LCD12864B.h>
#include <delay.h>
#include <stdio.h>
#include "stdlib.h"
#include "stdarg.h"
#include "string.h"
bit Global_Uart3_busy;
#define Uart3_Rec_Maxlength 100
unsigned char Global_Uart3_Rec[Uart3_Rec_Maxlength];
#define Uart3_END_CODE '#'
unsigned char Uart3_Rec_Byte_Length=0;
unsigned char Uart3_Rec_Frame_Flag=0;
unsigned char Global_uart3cmdnum=0;
#define Uart3CMD_NULL 0
#define Uart3CMD_LED0 1
#define Uart3CMD_LED1 2
#define Uart3CMD_LED2 3
char uart3_print_buff[50];
void Uart3_Init(void);
void UART3_SendData(char dat);
void Uart3SendStr(char *p);
void Clear_Uart3_Rxbuff(void);
unsigned char Judge_Uart3_Response(char* fmt,...);
unsigned char Get_Uart3_AngleData(float *input_angle);
unsigned char Get_Uart3_Int_Data(int *input_data);
unsigned char Get_Uart3_Many(int input_data[]);
void Uart1_Init(void);
void UartSendStr(char *p);
void UART1_SendData(char dat);
void PortMode()
{
P0M0=0x00;P0M1=0x00;
P1M0=0x00;P1M1=0x00;
P2M0=0x00;P2M1=0x00;
P3M0=0x00;P3M1=0x00;
P4M0=0x00;P4M1=0x00;
P5M0=0x00;P5M1=0x00;
P6M0=0x00;P6M1=0x00;
P7M0=0x00;P7M1=0x00;
}
int main()
{
float angle_x=12.45;
int xmm;
int input_dataRec[3];
int i;
PortMode();
Uart1_Init();
Uart3_Init();
IE2 = 0x08;
ES=1;
EA = 1;
delay_ms(1000);
printf("Uart3 Test !\r\n");
sprintf(uart3_print_buff,"%.2f",angle_x);
printf("Uart3 angle_x=");
printf(uart3_print_buff);
printf("\r\n");
while(1)
{
Uart3SendStr("ReccmdA");
delay_ms(5);
if(Global_uart3cmdnum==Uart3CMD_LED0)
{
Global_uart3cmdnum=Uart3CMD_NULL;
printf("Uart3CMD_LED0 Test !\r\n");
i=0;
while(!Get_Uart3_AngleData(&angle_x))
{
delay_ms(5);
i++;
if(i>3000)return 0;
}
angle_x=angle_x+100;
sprintf(uart3_print_buff,"%.2f",angle_x);
Uart3SendStr("Uart3 angle_x=");
Uart3SendStr(uart3_print_buff);
Uart3SendStr("\r\n");
printf("angle =%0.2f\r\n",angle_x);
}
else if(Global_uart3cmdnum==Uart3CMD_LED1)
{
printf("Uart3CMD_LED111Test !\r\n");
Global_uart3cmdnum=Uart3CMD_NULL;
i=0;
while(! Get_Uart3_Int_Data(&xmm))
{
delay_ms(5);
i++;
if(i>3000)return 0;
}
xmm=xmm+100;
sprintf(uart3_print_buff,"%d",xmm);
printf("Uart3 xmm=");
printf(uart3_print_buff);
printf("\r\n");
}
else if(Global_uart3cmdnum==Uart3CMD_LED2)
{
printf("Uart3CMD_LED333 Test !\r\n");
Global_uart3cmdnum=Uart3CMD_NULL;
i=0;
while(!Get_Uart3_Many(input_dataRec))
{
delay_ms(5);
i++;
if(i>3000)return 0;
}
sprintf(uart3_print_buff,"%d",input_dataRec[0]);
printf("Uart3 xmm=");
printf(uart3_print_buff);
printf("\r\n");
sprintf(uart3_print_buff,"%d",input_dataRec[1]);
printf("Uart3 xmm=");
printf(uart3_print_buff);
printf("\r\n");
sprintf(uart3_print_buff,"%d",input_dataRec[2]);
printf("Uart3 xmm=");
printf(uart3_print_buff);
printf("\r\n");
delay_ms(500);
}
}
}
void Uart3_Init(void)
{
Global_Uart3_busy=0;
S3CON = 0x10;
S3CON |= 0x40;
T4T3M &= 0xFD;
T3L = 0xFC;
T3H = 0xFF;
T4T3M |= 0x08;
}
void UART3_SendData(char dat)
{
while (Global_Uart3_busy);
Global_Uart3_busy = 1;
S3BUF = dat;
}
void Uart3SendStr(char *p)
{
while (*p)
{
UART3_SendData(*p++);
}
}
void Uart3Isr() interrupt 17 using 1
{
if(S3CON & 0x02)
{
S3CON &= ~0x02;
Global_Uart3_busy=0;
}
if (S3CON & 0x01)
{
S3CON &= ~0x01;
Global_Uart3_Rec[Uart3_Rec_Byte_Length]=S3BUF;
if(Global_Uart3_Rec[Uart3_Rec_Byte_Length]==Uart3_END_CODE)
{
Uart3_Rec_Byte_Length=0;
Uart3_Rec_Frame_Flag=1;
if(Judge_Uart3_Response("LED=0"))
{
Clear_Uart3_Rxbuff();
Global_uart3cmdnum = Uart3CMD_LED0;
}
else if(Judge_Uart3_Response("LED=1"))
{
Clear_Uart3_Rxbuff();
Global_uart3cmdnum = Uart3CMD_LED1 ;
}
else if(Judge_Uart3_Response("LED=2"))
{
Clear_Uart3_Rxbuff();
Global_uart3cmdnum = Uart3CMD_LED2 ;
}
}
else
{
Uart3_Rec_Byte_Length++;
if(Uart3_Rec_Byte_Length>Uart3_Rec_Maxlength-1) Uart3_Rec_Byte_Length=0;
}
}
}
void Clear_Uart3_Rxbuff(void)
{
unsigned char *p,i;
p=Global_Uart3_Rec;
for(i=0;i<Uart3_Rec_Maxlength;i++)
{
*p++=0;
}
Uart3_Rec_Byte_Length=0;
Uart3_Rec_Frame_Flag=0;
}
unsigned char Judge_Uart3_Response(char* fmt,...)
{
va_list ap;
char p[30];
if(!Uart3_Rec_Frame_Flag) return 0;
va_start(ap,fmt);
vsprintf((char*)p,fmt,ap);
va_end(ap);
if(strstr((char*)Global_Uart3_Rec,p)==NULL) return 0;
else return 1;
}
unsigned char Get_Uart3_AngleData(float *input_angle)
{
float angletemp;
if(!Uart3_Rec_Frame_Flag) return 0;
sscanf((const char *)Global_Uart3_Rec,"angle_x=%f",&angletemp);
*input_angle=(float)angletemp;
return 1;
}
unsigned char Get_Uart3_Int_Data(int *input_data)
{
int inttemp;
if(!Uart3_Rec_Frame_Flag) return 0;
sscanf((const char *)Global_Uart3_Rec,"xmm=%d",&inttemp);
*input_data=(float)inttemp;
return 1;
}
unsigned char Get_Uart3_Many(int input_data[])
{
int input_dataTemp[3];
if(!Uart3_Rec_Frame_Flag) return 0;
sscanf((const char *)Global_Uart3_Rec,"lr:%d,ud:%d,an:%d",&input_dataTemp[0],&input_dataTemp[1],&input_dataTemp[2]);
input_data[0]=input_dataTemp[0];
input_data[1]=input_dataTemp[1];
input_data[2]=input_dataTemp[2];
return 1;
}
void UART1_SendData(char dat)
{
ES=0;
SBUF=dat;
while(TI!=1);
TI=0;
ES=1;
}
void UartSendStr(char *p)
{
while (*p)
{
UART1_SendData(*p++);
}
}
char putchar(char c)
{
UART1_SendData(c);
return c;
}
void Uart1_Init(void)
{
SCON = 0x50;
AUXR |= 0x01;
AUXR &= 0xFB;
T2L = 0xFC;
T2H = 0xFF;
AUXR |= 0x10;
}
|