⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 t.c

📁 用C51编写的基于51单片机串口点对点双向通信程序
💻 C
字号:

#include <reg52.h>
#include <absacc.h>

#define lcd_wc XBYTE[0x400]
#define lcd_wd XBYTE[0x600]
#define lcd_rc XBYTE[0x500]
#define lcd_rd XBYTE[0x700]

unsigned int  idata shows[9];
unsigned char idata trans[10];
unsigned char idata tring[4];
unsigned char idata inbuf[10];
unsigned char idata wait_time;
unsigned char idata incount;
unsigned char idata t_chknum;
unsigned char idata r_chknum;
unsigned char idata r_dis;
unsigned char idata r_cor;
unsigned char idata t_cor;
unsigned char idata ms;
int  xxxz;
sbit en_k = P1^3;
sbit up_k = P1^2;
sbit do_k = P1^1;
sbit mo_k = P1^0;
sbit wt_p = P1^4;

code unsigned char  per_po[9] =
 {
  0x00,0x06,
  0x40,0x46,
  0x10,0x16,
  0x50,0x56,0x5c
 };

void delay(unsigned int x);

void write_code(unsigned char c)
 {
  while(lcd_rc>=0x80);
  lcd_wc=c;
 }

void put_c(unsigned char c)
 {
  while(lcd_rc>=0x80);
  lcd_wd = c;
 }

void to_string(unsigned int temp)
 {
  *(tring+2) = (temp%10)+0x30;
  temp/=10;
  *(tring+1) = (temp%10)+0x30;
  temp/=10;
  *tring = temp+0x30;
  *(tring+3) ='\0';
 }

void pr_string(unsigned char *c)
 {
  unsigned char count=0;
  while(*(c+count)!='\0')
   {
    put_c(*(c+count));
    count++;
   }
 }

void timer0(void) interrupt 1 using 2
 {
  TH0 = 0x3c;
  TL0 = 0xb0;
  if(++wait_time>=250)wait_time=0;
  if (++ms >= 250)
   {
    ms = 0;
    r_dis = 1;
   }
 }

void transcode(unsigned char *datas)
 {
  unsigned char count=0;
  t_chknum=0;
  while(datas[count]!='\0')
   {
    if(datas[count]=='!')
     {
      ACC=t_chknum;
      TB8=P;
      SBUF=t_chknum;
      delay(10);
      ACC=datas[count];
      TB8=P;
      SBUF=datas[count++];
      delay(10);
     }
    else if(datas[count]=='#')
     {
      ACC=datas[count];
      TB8=P;
      SBUF=datas[count++];
      delay(10);
     }
    else
     {
      ACC=datas[count];
      TB8=P;
      SBUF=datas[count];
      delay(10);
      t_chknum ^= datas[count++];
     }
   }
 }

void serial(void) interrupt 4 using 1
 {
  unsigned char count;
  unsigned char c;
  unsigned char ebz;
  if(RI)
   {
    RI = 0;
    c=SBUF;
    ACC=c;
    if(P==RB8)switch (c)
     {
      case '\21':
      t_cor = 1;
      break;

      case '#':
      r_chknum = 0x0;
      incount = 0;
      for(count =0;count<10;count++)inbuf[count]='\0';
      break;

      case '!':
      if(*inbuf=='m')
       {
	ebz=0x0;
	for(count=1;count<=4;count++)if(inbuf[count]>0x39||inbuf[count]<0x30)ebz=0x1;
        if(ebz==0x0&&r_chknum == 0x0&&incount==6){r_cor=1;ACC='\21';TB8=P;SBUF='\21';delay(10);}
       }
      else if(*inbuf=='p')
       {
	ebz=0x0;
	for(count=1;count<=3;count++)if(inbuf[count]>0x39||inbuf[count]<0x30)ebz=0x1;
        if(ebz==0x0&&r_chknum == 0x0&&incount==5){r_cor=1;ACC='\21';TB8=P;SBUF='\21';delay(10);}
       }
      break;

      default:
      if(incount<10)inbuf[incount++] = c;
      r_chknum ^= c;
      break;
     }
   }
  if(TI)TI=0;
 }

void transdata(void)
 {
  unsigned char count;
  for(count=0;count<=8;count++)
   {
    wait_time=0;
    do
     {
      t_cor = 0x0;
      if(wait_time<=200)wt_p =!wt_p;
      trans[0]='#';
      trans[1]='d';
      to_string(*(shows+count));
      trans[2]=tring[0];
      trans[3]=tring[1];
      trans[4]=tring[2];
      trans[5]=count+0x30;
      trans[6]='!';
      trans[7]='\0';
      transcode(trans);
      delay(200);
     }while(t_cor==0);
   }
  write_code(0x01);
 }

void comedata(unsigned char abc)
 {
  unsigned char count;
  if(abc=='p')
   {
    for(count=0;count<=8;count++)
     {
      wait_time=0x0;
      do
       {
	r_cor = 0x0;
	t_cor = 0x0;
	if(wait_time<=200)wt_p =!wt_p;
	trans[0]='#';
	trans[1]='p';
	trans[2]=0x30+count;
	trans[3]='!';
	trans[4]='\0';
	transcode(trans);
	delay(200);
       }while(t_cor==0||r_cor==0);
      shows[count]=(*(inbuf+1)-0x30)*100+(*(inbuf+2)-0x30)*10+(*(inbuf+3)-0x30);
      write_code(0x80+*(per_po+count));
      put_c(*(inbuf+1));
      put_c(*(inbuf+2));
      put_c(*(inbuf+3));
     }
   }
  else if(abc=='m')
   {
    for(count=0;count<=7;count++)
     {
      wait_time=0;
      do
       {
	t_cor=0x0;
	r_cor=0x0;
	if(wait_time<=200)wt_p =!wt_p;
	trans[0] ='#';
	trans[1] ='m';
	trans[2] =0x30+count;
	trans[3]='!';
	trans[4] ='\0';
	transcode(trans);
	delay(200);
       }while(t_cor==0||r_cor==0);
      write_code(0x80+*(per_po+count));
      if(*(inbuf+4)==0x31) put_c(0x3e);
      else if(*(inbuf+4)==0x32) put_c(0x3c);
      else put_c(0x20);
      put_c(*(inbuf+1));
      put_c(*(inbuf+2));
      put_c(*(inbuf+3));
     }
   }
 }

void user_response()
 {
  unsigned char count=0;
  write_code(0x80+*(per_po+count));
  do
   {
    wt_p =!wt_p;
    delay(400);
    if(!up_k)
     {
      (*(shows+count))++;
      if(*(shows+count)>400) *(shows+count)=1;
      to_string(*(shows+count));
      pr_string(tring);
      write_code(0x80+*(per_po+count));
     }
    else if(!do_k)
     {
      (*(shows+count))--;
      if(*(shows+count)<1) *(shows+count)=400;
      to_string(*(shows+count));
      pr_string(tring);
      write_code(0x80+*(per_po+count));
     }
    else if(!mo_k)
     {
      count = (count+1)%9;
      write_code(0x80+*(per_po+count));
     }
    else if(!en_k)break;
   }while(1);
 }

main()
 {
  P1    = 0X0f;
  TMOD  = 0x21;
  TH1   = 0xf4;
  TL1   = 0xf4;
  TH0   = 0x3c;
  TL0   = 0xb0;
  TR1   = 1;
  PCON  = 0x00;
  SCON  = 0xd0;
  PS  = 1;
  ES  = 1;
  ET0 = 1;
  write_code(0x38);
  write_code(0x0e);
  write_code(0x02);
  write_code(0x01);
  r_dis=0x0;
  ms   =0x0;
  TR0  = 1;
  EA   = 1;
  do
   {
    wt_p =!wt_p;
    if(!mo_k)
     {
      write_code(0x01);
      comedata('p');
      user_response();
      transdata();
      r_dis=1;
     }
    if(r_dis==1)
     {
      comedata('m');
      r_dis=0;
     }
   }while(1);
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -