单片机
返回首页

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=='')

{break;}

word=word+1;

}   

word=psword;

return 3;

 }

int receiveWord(uchar* prword)  // lun xun receive   &receivearray[Mlength]

{  uchar * word;

   int i;

word= prword;

    REN=1;

  for( i=0;i<Mlength;i++)

  {

    *word= receiveByte(usartWAY, 1);

      RI=0;

if(*word=='') //receivearray[word]==''

{break;}

word++;

  } 

    REN=0;

   return 4;

}

/////////////////////////////////zi dong length str/////

extern void sendWordauto(uchar* psword)    // lun xun send ES=0

 {

   uchar * word;

    for(word=psword;*word!='';word++)

{

Uart_SendChar(*word);

}

  Uart_SendChar('');

  

 }

extern void receiveWordauto(uchar* prword)// lin xun receive

 {

  uchar * word;

REN=1;

word=prword;

     while((*word=receiveByte(usartWAY, 1))!='')

{

word++;

}

word=prword;

REN=0;

 }

////////////////////////////////////////////////ku////////////

char putchar(char c)//重定向

{

Uart_SendChar(c);

return c;

}   

void Uart_SendChar(unsigned char  dat)

{

    //SBUF = dat; 

    //while(!TI); 

    //TI = 0; 

sendByte(usartWAY,dat, 1);

TI=0;

}

LCD9648部分

lcd9648.h

#ifndef _LCD9648_H_

#define _LCD9648_H_

#include <REGX52.h>

   

sbit CS0  = P0^0; //片选

sbit RST  = P0^1; //复位

sbit RS   = P2^7; //数据命令选择端

void Delay10us(unsigned int c) ;

void WriteComm(unsigned int i);

void WriteData(unsigned int i);

void LCD_Init(void);

void Clear();

void Cleararea(int p,int l,int p2,int l2);

void poseSign(int p,int l);

void writeStr(int p,int l,uchar *str);

void SetPoint(int l,int y); //x<96 y<48

void writenum(int num);

void writenums(int p,int l,int num);

#endif

lcd9648.c

#include "spi.h"

#include "LCD9648.h"

 #include "oledfont.h"

void Delay10us(unsigned int c)  //误差 0us

{

//    unsigned char b;

    for(;c>0;c--);

       // for(b=3;b>0;b--);

            

}

 void WriteComm(unsigned int i)

{

    CS0 = 0;

RS  = 0;

SendDataSPI(i);

CS0 = 1;

}

void WriteData(unsigned int i)

{

    CS0 = 0;

RS  = 1;

SendDataSPI(i);

CS0 = 1;

}

void LCD_Init(void)

{

unsigned int i;

//CS0=0;

RST=1;  

for(i = 0; i < 1000; i++);

RST=0;

for(i = 0; i < 1000; i++);

RST=1;

for(i = 0; i < 1000; i++);

    WriteComm(0xe2);   //软件复位

WriteComm(0xc8);   //0xC8普通方向选择选择反向,0xC0为正常方向

WriteComm(0xa0);   //0xA0段方向选择正常方向(0xA1为反方向

WriteComm(0x2f);

WriteComm(0x26);

WriteComm(0x81);   //背景光对比度

WriteComm(0x10);

WriteComm(0xaf);   //开启显示

}

void Clear()

{

unsigned char i,j;

for(i=0; i < 6; i++)

{

WriteComm(0x40);

WriteComm(0xb0+i);

WriteComm(0x10);

WriteComm(0x00);

for(j = 0; j < 96; j++)

{

WriteData(0x00);

}

}

}

 void Cleararea(int p,int l,int p2,int l2)

{

unsigned char i,j;

for(i=p; i < p2; i++)

{

WriteComm(0x40);

WriteComm(0xb0+i);

WriteComm(0x10);

WriteComm(0x00);

l=l%96;

l2=l2%96;

for(j = l; j < l2; j++)

{

WriteData(0x00);

}

}

}

void poseSign(int p,int l)

{

    WriteComm(0x40);

p=p%6;

WriteComm(0xb0+p);

l=l%96;

WriteComm(0x10+l/16);

WriteComm(0x00+l%16);

// WriteData(0xFF);  //u

// WriteData(0x00); //+u

// Delay10ms(100);  //uu

// clear();

}

void writeStr(int p,int l,uchar *str)

{   uchar * word;

      int i;

      word=str;

    WriteComm(0x40);

p=p%6;

WriteComm(0xb0+p);

l=l%96;

WriteComm(0x10+l/16);

WriteComm(0x00+l%16);

    while(*word!='')

{

   for(i=0;i<6;i++)

      {

   WriteData(F6x8[(*word)-0x20][i]);

  }

   word++;

}

  word=str;

}

void SetPoint(int l,int y)  //x<96 y<48

{

   int p;int px;

    char pxA;

//   int i;

p=y/8;

px=y%8;

p=5-p;

px=7-px;

pxA=0x01<<px;

    WriteComm(0x40);

WriteComm(0xb0+p);

  l=l%96;

WriteComm(0x10+l/16);

WriteComm(0x00+l%16);

//

   WriteData(pxA); ///////////////////

        Delay10us(10); //try

}

void writenum(int num)

{   

int i;

i=0;

   for(i=0;i<6;i++)

      {

   WriteData(F6x8[num-0x20+0x30][i]);

  }

}

void writenums(int p,int l,int num)

{ int b,s,g;

        b=0;s=0;g=0;

    WriteComm(0x40);

p=p%6;

WriteComm(0xb0+p);

l=l%96;

WriteComm(0x10+l/16);

WriteComm(0x00+l%16); //set

if(num<1000)

{g= num%10;

s=(num%100)/10;

b=num/100;}

else

{

  b=s=g=0;

  num=0;

}

writenum(b);

writenum(s);

writenum(g);

}

oledfont.h

   #ifndef __OLEDFONT_H

#define __OLEDFONT_H    

//常用ASCII表

//偏移量32

//ASCII字符集

//偏移量32

//大小:12*6

/************************************6*8的点阵************************************/

const unsigned char code F6x8[][6] =

{

0x00, 0x00, 0x00, 0x00, 0x00, 0x00,// sp

0x00, 0x00, 0x00, 0x2f, 0x00, 0x00,// !

0x00, 0x00, 0x07, 0x00, 0x07, 0x00,// "

0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14,// #

0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12,// $

0x00, 0x62, 0x64, 0x08, 0x13, 0x23,// %

0x00, 0x36, 0x49, 0x55, 0x22, 0x50,// &

0x00, 0x00, 0x05, 0x03, 0x00, 0x00,// '

0x00, 0x00, 0x1c, 0x22, 0x41, 0x00,// (

0x00, 0x00, 0x41, 0x22, 0x1c, 0x00,// )

0x00, 0x14, 0x08, 0x3E, 0x08, 0x14,// *

0x00, 0x08, 0x08, 0x3E, 0x08, 0x08,// +

0x00, 0x00, 0x00, 0xA0, 0x60, 0x00,// ,

0x00, 0x08, 0x08, 0x08, 0x08, 0x08,// -

0x00, 0x00, 0x60, 0x60, 0x00, 0x00,// .

0x00, 0x20, 0x10, 0x08, 0x04, 0x02,// /

0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E,// 0

0x00, 0x00, 0x42, 0x7F, 0x40, 0x00,// 1

0x00, 0x42, 0x61, 0x51, 0x49, 0x46,// 2

0x00, 0x21, 0x41, 0x45, 0x4B, 0x31,// 3

0x00, 0x18, 0x14, 0x12, 0x7F, 0x10,// 4

0x00, 0x27, 0x45, 0x45, 0x45, 0x39,// 5

0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30,// 6

0x00, 0x01, 0x71, 0x09, 0x05, 0x03,// 7

0x00, 0x36, 0x49, 0x49, 0x49, 0x36,// 8

0x00, 0x06, 0x49, 0x49, 0x29, 0x1E,// 9

0x00, 0x00, 0x36, 0x36, 0x00, 0x00,// :

0x00, 0x00, 0x56, 0x36, 0x00, 0x00,// ;

0x00, 0x08, 0x14, 0x22, 0x41, 0x00,// <

0x00, 0x14, 0x14, 0x14, 0x14, 0x14,// =

0x00, 0x00, 0x41, 0x22, 0x14, 0x08,// >

0x00, 0x02, 0x01, 0x51, 0x09, 0x06,// ?

0x00, 0x32, 0x49, 0x59, 0x51, 0x3E,// @

0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C,// A

0x00, 0x7F, 0x49, 0x49, 0x49, 0x36,// B

0x00, 0x3E, 0x41, 0x41, 0x41, 0x22,// C

0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C,// D

0x00, 0x7F, 0x49, 0x49, 0x49, 0x41,// E

0x00, 0x7F, 0x09, 0x09, 0x09, 0x01,// F

0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A,// G

0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F,// H

0x00, 0x00, 0x41, 0x7F, 0x41, 0x00,// I

0x00, 0x20, 0x40, 0x41, 0x3F, 0x01,// J

0x00, 0x7F, 0x08, 0x14, 0x22, 0x41,// K

0x00, 0x7F, 0x40, 0x40, 0x40, 0x40,// L

0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F,// M

0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F,// N

0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E,// O

0x00, 0x7F, 0x09, 0x09, 0x09, 0x06,// P

0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E,// Q

0x00, 0x7F, 0x09, 0x19, 0x29, 0x46,// R

0x00, 0x46, 0x49, 0x49, 0x49, 0x31,// S

0x00, 0x01, 0x01, 0x7F, 0x01, 0x01,// T

0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F,// U

0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F,// V

0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F,// W

0x00, 0x63, 0x14, 0x08, 0x14, 0x63,// X

0x00, 0x07, 0x08, 0x70, 0x08, 0x07,// Y

0x00, 0x61, 0x51, 0x49, 0x45, 0x43,// Z

0x00, 0x00, 0x7F, 0x41, 0x41, 0x00,// [

0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55,// 55

0x00, 0x00, 0x41, 0x41, 0x7F, 0x00,// ]

0x00, 0x04, 0x02, 0x01, 0x02, 0x04,// ^

0x00, 0x40, 0x40, 0x40, 0x40, 0x40,// _

0x00, 0x00, 0x01, 0x02, 0x04, 0x00,// '

0x00, 0x20, 0x54, 0x54, 0x54, 0x78,// a

0x00, 0x7F, 0x48, 0x44, 0x44, 0x38,// b

0x00, 0x38, 0x44, 0x44, 0x44, 0x20,// c

0x00, 0x38, 0x44, 0x44, 0x48, 0x7F,// d

0x00, 0x38, 0x54, 0x54, 0x54, 0x18,// e

0x00, 0x08, 0x7E, 0x09, 0x01, 0x02,// f

0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C,// g

0x00, 0x7F, 0x08, 0x04, 0x04, 0x78,// h

0x00, 0x00, 0x44, 0x7D, 0x40, 0x00,// i

0x00, 0x40, 0x80, 0x84, 0x7D, 0x00,// j

0x00, 0x7F, 0x10, 0x28, 0x44, 0x00,// k

0x00, 0x00, 0x41, 0x7F, 0x40, 0x00,// l

0x00, 0x7C, 0x04, 0x18, 0x04, 0x78,// m

0x00, 0x7C, 0x08, 0x04, 0x04, 0x78,// n

0x00, 0x38, 0x44, 0x44, 0x44, 0x38,// o

0x00, 0xFC, 0x24, 0x24, 0x24, 0x18,// p

0x00, 0x18, 0x24, 0x24, 0x18, 0xFC,// q

0x00, 0x7C, 0x08, 0x04, 0x04, 0x08,// r

0x00, 0x48, 0x54, 0x54, 0x54, 0x20,// s

0x00, 0x04, 0x3F, 0x44, 0x40, 0x20,// t

0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C,// u

0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C,// v

0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C,// w

0x00, 0x44, 0x28, 0x10, 0x28, 0x44,// x

0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C,// y

0x00, 0x44, 0x64, 0x54, 0x4C, 0x44,// z

0x14, 0x14, 0x14, 0x14, 0x14, 0x14,// horiz lines

};

#endif

SPI部分

spi.h

#ifndef _SPI_H_

#define  _SPI_H_

#define uchar unsigned char

#include <REGX52.H>

sbit SCL  = P2^6; //SPI时钟端

sbit SDA  = P2^5; //SPI数据端

void Delay10ms(unsigned int c);   //误差 0us

void  SendDataSPI(uchar dat);

uchar SpiReceive();

#endif

spi.c

#include "spi.h"

void Delay10ms(unsigned int c)  //误差 0us

{

    unsigned char a,b;

    for(;c>0;c--)

        for(b=38;b>0;b--)

            for(a=130;a>0;a--);

}

void  SendDataSPI(uchar dat)

{  

   unsigned char i;

   

   for(i=0; i<8; i++)

   {  

      if( (dat&0x80)!=0 ) SDA = 1;

        else SDA = 0;

      dat <<= 1;

  SCL = 0;

      SCL = 1;

   }

}

uchar SpiReceive()

{

  uchar byt0;

  int i;

  i=0;

  byt0=0x00;

   SCL=1;

  for(i=0;i<8;i++)

  {

     while(SCL==1);

     SCL=0;//下降沿输入数据

   byt0<<=1;

   byt0|=(uchar)SDA; 

   Delay10ms(1); 

   }

return(byt0);

}



进入单片机查看更多内容>>
相关视频
  • 【TI MSPM0 应用实战】智能小车+工业角度编码器+血氧仪+烟雾探测器!硬核参考设计详解!

  • 2022 Digi-Key KOL 系列: 你见过1GHz主频的单片机吗?Teensy 4.1开发板介绍

  • TI 新一代 C2000™ 微控制器:全方位助力伺服及马达驱动应用

  • MSP430电容触摸技术 - 防水Demo演示

  • 直播回放: Microchip Timberwolf™ 音频处理器在线研讨会

  • 基于灵动MM32W0系列MCU的指夹血氧仪控制及OTA升级应用方案分享

精选电路图
  • 1瓦线性调频增强器

  • 12V 转 28V DC-DC 变换器(基于 LM2585)

  • 红外遥控音量控制

  • LM317过压保护

  • 12V转110V/220V 500W逆变器

  • DS1669数字电位器

    相关电子头条文章