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

📄 m.c

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

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

#define ad_hot XBYTE[0x2001]
#define ad_col XBYTE[0X2000]
#define hot_po XBYTE[0x4000]
#define col_po XBYTE[0x6000]

unsigned int  idata shows[9];
unsigned int  idata realv[8];   
unsigned char idata trans[10];
unsigned char idata tring[4];
unsigned char idata inbuf[10];
unsigned char idata control;
unsigned char idata ms;       
unsigned char idata syn;
unsigned char idata incount;
unsigned char idata r_cor;
unsigned char idata t_cor;
unsigned char idata r_chknum;
unsigned char idata re_get;
unsigned char idata re_ctl;
unsigned char idata t_chknum; 

sbit  SCL = P1^3;
sbit  SDA = P1^4;

code unsigned int v_t[256] =
 {
  2,  3,  6,  7,  9,  11, 13, 14, 16, 18,
  20, 21, 23, 25, 27, 28, 30, 32, 34, 35,
  38, 39, 41, 42, 45, 46, 48, 50, 51, 53,
  54, 57, 58, 60, 61, 64, 65, 67, 69, 70,
  72, 73, 76, 77, 79, 81, 82, 84, 85, 88,
  89, 91, 92, 94, 96, 97, 99, 101,102,104,
  106,108,109,111,113,114,116,118,119,121,
  122,124,126,127,129,131,132,134,136,137,
  139,141,143,144,145,148,149,150,152,154,
  155,157,159,160,162,163,165,167,168,170,
  172,173,175,177,178,179,182,183,184,186,
  188,189,191,192,194,196,197,199,200,202,
  203,205,207,208,210,211,213,214,216,218,
  219,220,222,224,225,227,228,230,231,233,
  235,236,238,239,241,242,244,245,247,248,
  250,251,253,255,256,257,259,261,262,263,
  265,267,268,269,271,273,274,276,277,278,
  280,282,283,284,286,288,289,290,292,293,
  295,296,298,299,300,302,304,305,306,308,
  309,311,312,314,315,316,318,319,321,322,
  324,325,326,328,330,331,332,334,335,336,
  338,340,341,342,344,345,346,348,349,351,
  352,354,355,356,357,359,360,362,363,365,
  366,367,369,370,371,373,374,376,377,379,
  380,381,382,384,385,386,387,389,390,392,
  393,395,396,397,399,400 
 };

code unsigned char ad590[40]=
 {
  0, 0, 1, 2, 2, 3, 4, 4, 5, 6,
  6, 7, 8, 9, 9, 10,11,11,12,13,
  14,14,15,16,16,17,18,19,19,20,
  21,22,22,23,24,25,25,26,27,27
 };

void delay(unsigned int x);

unsigned char write_char(unsigned char write,unsigned char address);

unsigned char read_char(unsigned char address);

void output(void)
 {
  unsigned char count1;
  unsigned char count2;
  unsigned char hot_out=0x0;
  unsigned char col_out=0x0;
  unsigned char t_m = 0x1;
  unsigned int hh_tem;
  unsigned int hi_tem;
  unsigned int lo_tem;
  unsigned int re_tem;
  for(count2=0;count2<=7;count2++)
   {
    hh_tem =*(shows+count2)+*(shows+count2)/20;
    hi_tem =*(shows+count2);
    lo_tem =(*(shows+count2)*70)/100;
    re_tem =*(realv+count2);
    if(re_tem<lo_tem) count1 = 99;
    else if(re_tem >= lo_tem && re_tem<hi_tem) count1 = 50;
    else  count1 = 0;
    if(count1 == 0)
     {
      if(re_tem>=hh_tem)
       {
	col_out = col_out|t_m;
	hot_out=hot_out&(~t_m);
       }
      else
       {
	col_out = col_out&(~t_m);
	hot_out=hot_out&(~t_m);
       }
     }
    else if(count1 >= control){hot_out = hot_out|t_m;col_out=col_out&(~t_m);}
    else {hot_out=hot_out&(~t_m);col_out=col_out&(~t_m);}
    t_m = t_m*2;
   }
  hot_po =hot_out;
  col_po =col_out;
 }

void get_data(void)
 {
  unsigned char count1;
  unsigned char count2;
  unsigned char val[16];
  unsigned int  r_cool;                  
  unsigned int temp;

  for(count2 = 0;count2<11;count2++)
   {
    ad_col = 0x0;                         
    delay(5);
    *(val+count2) = ad_col;
   }
  temp=0;
  for(count2=1;count2<11;count2++) temp = temp+*(val+count2);
  temp = temp/10;
  if(temp<216) temp = 216;
  r_cool = *(ad590+temp-216);
  
  for(count1=0;count1<=7;count1++)
   {
    P1 = 0xf0;
    P1 = P1|count1;
    delay(100);
    for(count2 = 0;count2<11;count2++)
     {
      ad_hot = 0x0;                   
      delay(5);
      *(val+count2) = ad_hot;
     }
    temp =0x0;
    for(count2=1;count2<11;count2++)temp = temp+*(val+count2);
    realv[count1] = *(v_t+temp/10+r_cool);
   }
 }

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

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);
      t_chknum ^=*(datas+count++);
      delay(10);
     } 
   }
 }

void serial (void) interrupt 4 using 1
 {     
  unsigned char c;
  unsigned char count;
  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)=0x0;
      break;

      case '!':
      if(*inbuf=='m')
       {
	ebz=0x0;
	if(inbuf[1]>0x39||inbuf[1]<0x30)ebz=0x1;
        if(ebz==0x0&&r_chknum == 0x0&&incount==3){r_cor=1;ACC='\21';TB8=P;SBUF='\21';delay(10);}
       }
      else if(*inbuf=='p')
       {
	ebz=0x0;
	if(inbuf[1]>0x39||inbuf[1]<0x30)ebz=0x1;
        if(ebz==0x0&&r_chknum == 0x0&&incount==3){r_cor=1;ACC='\21';TB8=P;SBUF='\21';delay(10);}
       }
      else if(*inbuf=='d')
       {
	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);}
       }
      break;

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

main()
 {
  unsigned int  cov;
  unsigned char warn;
  unsigned char wr_data;
  unsigned char count;
  union ut
   {
    unsigned int word;
    struct
     {
      unsigned char hi;
      unsigned char lo;
     }bytes;
   };
  union ut tem;

  TMOD  = 0x21;         
  TH1   = 0xf4;
  TL1   = 0xf4;
  TH0   = 0x3c;         
  TL0   = 0xb0;
  TR1 = 1;     
  PCON  = 0x00;      
  SCON  = 0xd0;
  PS  = 1;
  ES  = 1;
  ET0 = 1;
  for(count=0;count<=8;count++)
   {
    delay(50);
    wr_data = read_char(30+count*2);
    tem.bytes.hi = wr_data;
    delay(50);
    wr_data  = read_char(30+count*2+1);
    tem.bytes.lo = wr_data;
    shows[count] = tem.word;
   }
  hot_po = 0x0;
  col_po = 0x0;
  r_cor  = 0x0;
  t_cor  = 0x0;
  re_get = 0x0;
  re_ctl = 0x0;
  ms  = 0;
  syn = 0;
  TR0 = 1;            
  EA  = 1;            
  do
   {
    if(r_cor==0x1)
     {
      r_cor=0; 
      if(*inbuf=='m')
       {
	count =*(inbuf+1)-0x30;
	cov = *(realv+count);
	if(cov>*(shows+count)+*(shows+count)/20) warn=0x31;
	else if(cov<*(shows+count)-*(shows+count)/20) warn=0x32;
	else warn = 0x30;
	do
	 {
	  t_cor=0;
	  trans[0]='#';
	  trans[1]='m';
	  to_string(cov);
	  trans[2]=tring[0];
	  trans[3]=tring[1];
	  trans[4]=tring[2];
	  trans[5]=warn;
	  trans[6]='!';
	  trans[7]='\0';
	  transcode(trans);
	  delay(100);
	 }while(t_cor==0);
       }
      else if(*inbuf == 'p')
       {
	count = *(inbuf+1)-0x30;
	do
	 {
	  t_cor=0;
	  trans[0]='#';
	  trans[1]='p';
	  to_string(*(shows+count));
	  trans[2]=tring[0];
	  trans[3]=tring[1];
	  trans[4]=tring[2];
	  trans[5]='!';
	  trans[6]='\0';
	  transcode(trans);
	  delay(100);
	 }while(t_cor==0);
       }
      else if(*inbuf =='d')
       {
	count = *(inbuf+4)-0x30;
	tem.word = (*(inbuf+1)-0x30)*100+(*(inbuf+2)-0x30)*10+*(inbuf+3)-0x30;
	shows[count] = tem.word;
	wr_data = tem.bytes.hi;
	write_char(wr_data,30+count*2);
	delay(50);
	wr_data = tem.bytes.lo;
	write_char(wr_data,30+count*2+1);
	delay(50);
       }
     }
    if(re_get==0x1) {get_data();re_get=0x0;}
    if(re_ctl==0x1) {output();re_ctl=0x0;}
   }while(1);
 }

void timer0(void) interrupt 1 using 2
 {
  TH0 = 0x3c;    
  TL0 = 0xb0;
  if (++ms >= 200)
   {
    ms = 0;
    re_get=0x1;
   }
  if(++syn == 120)    
   {
    syn=0;
    if(++control ==100)control = 0;
    re_ctl=0x1;
   }
 }

⌨️ 快捷键说明

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