51单片机与MATLAB串口传图播放bad apple
2025-10-17 来源:bilibili
MATLAB部分
1.main1.m 视频转文档
var leng=0;
var re1=0;
var renum=0;
obj = VideoReader('D:51单片机编程辅助程序编写区chuanKouPicnewfangzhenbadap.mp4');%输入视频位置
numFrames = obj.NumberOfFrames;% 帧的总数
disp(numFrames);
re1=0;
for i = 1:3:numFrames % 读取前1帧
frame = read(obj,i);%读取第几帧
k= imresize(frame,[48,96]);
thresh=graythresh(k);%确定二值化阈值
B=im2bw(k,thresh);%对图像二值
imshow(B);
% imshow(frame);%显示帧
disp(length(B));
leng=length(B);
fid=fopen('D:51单片机编程辅助程序编写区chuanKouPicnewfangzhenpic22.txt','a');
for pag=1:1:6
for ii=1:1:leng
for iii=1:1:8
re1=re1 + B(iii+8*(pag-1),ii)*(2^(iii-1));
end
iii=1;
%disp(dec2bin(re1,8));
fprintf(fid,'%s',dec2hex(re1,2));
renum=fprintf(fid,'%s',',');
%fwrite(s,'0101010101') ;%%s
re1=0;
% dec2bin(re1,8);%re yi
disp(',')
end
ii=1;
end
for ei=1:1:96
fprintf(fid,'%s','00');
renum=fprintf(fid,'%s',',');
end
fclose(fid);
end
2.main2 文档解读与串口传数据
s=serial('COM3','Timeout',10,'DataBits',8,'BaudRate',9600);
%site(s,'Timeout',5,'DataBits',4);
get(s);
fclose(s);
file=fopen('D:51单片机编程辅助程序编写区chuanKouPicnewfangzhenpic22.txt','r');
%file1=str2num('5');
fopen(s);
i=0;
while 1%i<96*9*1600
file1= fscanf(file,'%c',1);
file10=abs(file1);
if file10 ~=abs(',')
if file10 >= 48&&file10<=58
file10=abs(file1)-abs('0');
end
if file10 >= 65&&file10<=72
file10=abs(file1)-abs('A')+10;
end
% file10= dec2bin(file10);
disp(file10);
%%fwrite(s,file10);
% fprintf(s,'%c',file1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
file2= fscanf(file,'%c',1);
file11=abs(file2);
if file11 ~=abs(',')
if file11 >= 48&&file11<=58
file11=abs(file2)-abs('0');
end
if file11 >= 65&&file11<=72
file11=abs(file2)-abs('A')+10;
end
% file10= dec2bin(file10);
disp(file11);
file12=file10*16+file11;
fwrite(s,file12);
% fprintf(s,'%c',file1);
end
% fprintf(s,'%c',file1);
i=i+2;
end
fclose(s);
fclose(file);
C51部分
user部分
main.c
#include "usart.h"
#include"spi.h"
#include "LCD9648.h"
#include "calculate.h"
#include <stdio.h>
#define WAY 1
sbit led1=P1^1;
uchar rec; // 2
int ii=0; //+u
int flag=0;
#if WAY > 0
int jj=0;
#else if WAY==0
int jj=0;
#endif
void main()
{
PCON &= 0x7F; //波特率不倍速
SCON = 0x50; //8位数据,可变波特率
// AUXR &= 0xBF; //定时器1时钟为Fosc/12,即12T
// AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //清除定时器1模式位
TMOD |= 0x20; //设定定时器1为8位自动重装方式
// TH1=0XFd; //u
// TL1=0XFd; //9600 //u
TL1 = 0xfd; //设定定时初值 fd
TH1 = 0xfd; //设定定时器重装值 //19200
// TL1 = 0xff; //设定定时初值
// TH1 = 0xff; //设定定时器重装值 //38400
ET1 = 0; //禁止定时器1中断
TR1 = 1; //启动定时器1
REN=1;
EA=1;
ES=1;
LCD_Init();
Clear();
poseSign(0,0);
flag=0;
while(1)
{
// ES=0;
//
if(flag==1)
{
if(jj<96+2)
{jj++;WriteData((int)*&sendarray[0]);}//try} //+u
if(jj==96+2)
{
jj=1;
ii=(ii+1)%7;
poseSign(ii,jj-1); //+u
// WriteData((int)*&sendarray[0]); //one make a large influence
jj++;
}
flag=0;
ES=1; //
}
//ES=1;
}
}
void IRQfun2() interrupt 4 //timer0
{
REN=0;
#if WAY >0
// static unsigned char j=-1; ////////+u
//unsigned char charistic; //+u
//+u
sendarray[0]=SBUF;
if(RI=1)
{
if(flag==0)
flag=1;
ES=0;//
//
}
#else if WAY==0
sendarray[0]=SBUF;
if(RI=1)
{
if(jj==0)
{
// WriteData((int)*&sendarray[0]); //+u
// writenums(5,30,(int)jj);
poseSign((int)*sendarray[0],0); //jj is count arg now
}
else
{
WriteData((int)*&sendarray[0]);
}
jj=(jj+1)%97;
SBUF=0;
}
#endif
REN=1;
RI=0;
}
calculate.c
#include "calculate.h"
int count=0;
float calcu()
{
float g;
g=0.75;
g=(count*1000)/2;
//return g;
return g;
}
calculate.h
#ifndef _CALCULATE_
#define _CALCULATE_
extern int count;
float calcu();
#endif
ChuanKou部分
usart.h
#ifndef _USART_H_
#define _USART_H_
#define uchar unsigned char
#include <REGX52.h>
/*51 chuankou tongxin */
#pragma SAVE
#pragma REGPARMS
void initusart( int way,int timer,long time,int smode,int sm2); // chu shi chuan kou
#define usartWAY 1 ///chuan kou gong zuo fang shi
int sendByte(int way,uchar Byte, uchar tb8);
uchar receiveByte(int way, uchar rb8);
#define Mlength 20 /// chang du
extern uchar sendarray[Mlength];
extern uchar receivearray[Mlength];
//uchar otherArray[Mlength];
//1
int sendWord(uchar * psword); // lun xun send
int receiveWord(uchar * prword);// lin xun receive
//2
extern void sendWordauto(uchar * psword); // lun xun send
extern void receiveWordauto(uchar * prword);// lin xun receive
/////////////////////////////
void Uart_SendChar(unsigned char dat);
#pragma RESTORE
#endif
usart.c
#include "usart.h"
#include <stdio.h>
uchar sendarray[Mlength];//="hello";
//1.way 工作方式 2.baud 3.定时时间 4.smode 0 1 5.SM2 0 1
void initusart( int way,int timer,long time,int smode,int sm2)
{
REN=0;TB8=0;RB8=0;TI=0;RI=0;
PCON=smode; //bei zeng
switch(way)
{
case 2:
{
SM0=1;//(way>>1)&0x01;
SM1=0;//way&0x01;
SM2=sm2;
break;
}
case 1:
{
SM1=way&0x01;
SM0=(way>>1)&0x01;
SM2=sm2;
TMOD=0x20;
EA=1;
ET1=1;
TH1=time<<timer;
TL1=time<<timer;
TF1=0;
TR1=1;
break;
}
}
}
int sendByte(int way,uchar Byte, uchar tb8)
{
if(way==2||way==3)
{TB8=tb8;}
else{TB8=1;}
SBUF=Byte;
while(!TI);
return 2;
}
uchar receiveByte(int way, uchar rb8)
{
uchar Byte;
Byte='a';
RI=0;
if(SM2==0)
{RB8=0;}
if(SM2==1)
{RB8==1;}
while(!RI);
Byte=SBUF;
RI=0;
if(way==2||way==3)
{rb8=RB8;
//if tb8==1 ......
}
else{;}
return Byte;
}
////////////////////////////////////
int sendWord(uchar* psword) // lun xun send
{ uchar * word;
int i;
word=psword;
for(i=0;i<Mlength;i++)
{
sendByte(usartWAY,*word, 1);
TI=0;
if(*word=='




