找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2799|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

開關(guān)量輸入,pc端和單片機相連

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:215989 發(fā)表于 2017-6-30 06:35 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit sw_0=P3^3;//開關(guān)端口定義  switch
sbit sw_1=P3^4;
sbit sw_2=P3^5;
sbit sw_3=P3^6;
//sbit PS0=P2^4;
//sbit PS1=P2^5;
//sbit PS2=P2^6;
//sbit PS3=P2^7;
sfr P_data=0x80;
sbit P_K_L=P2^2;
uchar code tab[]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6,0xee,0x3e,0x9c,0x7a,0x9e,0x8e};         //字段轉(zhuǎn)換表
uchar rec[50];         //不能超過50
uchar code error[]={0x40,0x30,0x46,0x2a,0x2a,0x37,0x36,0x0d};        //數(shù)據(jù)不正確
uchar send[]={0x40,0x30,0x46,0x30,0x32,0x00,0x00,0x00,0x00,0x00,0x00,0x0d};          //正確的數(shù)據(jù)
uchar i;
uchar t;
uint sw_in(void);        //開關(guān)量輸入采集
void display(uint);
void delay(uint);
uint dth(uint);                   //16~10
uchar ath(uchar,uchar);         //ASCII~16
uint hta(uchar);         //16~ASCII
void uart(void);
void main(void)
{
   uint a,b,c,t;
   TMOD=0x20;
   TL1=0xfd;
   TH1=0xfd;
   SCON=0x50;
   TR1=1;                   //為1是定時器T1開始計數(shù);0時不計數(shù)。
   IE=0x90;
   while(1)
   {
      c=sw_in();
          t=dth(c);
          a=hta(t>>8);
          send[5]=a>>8;
          send[6]=(uchar)a;
          a=hta(t);
          send[7]=a>>8;
          send[8]=(uchar)a;
          b=0;
          for(a=1;a<9;a++)
          b^=send[a];
          b=hta(b);
          send[9]=b>>8;
          send[10]=(uchar)b;
          for(a=0;a<100;a++)
             display(c);
   }
}
//數(shù)碼管顯示函數(shù)沒寫
uint dth(uint a)
{
   uint b,c;
   b=a%16;
   if(b>9)
     c=b+6;
   else
     c=b;
   a=a/16;
   b=a%16;
   if(b>9)
     c+=(b+6)*10;
   else
     c=c+b*100;
   a=a/16;
   b=a%16;
   if(b>9)
     c+=(b+6)*1000;
   else
     c=c+b*1000;
   return c;
}
void delay(uint delay_time)
{
   for(delay_time=20;delay_time>0;delay_time--)
   {}
}
uchar ath(uchar a,uchar b)
{
   if(a<0x40)
      a-=0x30;
   else if(a<0x47)
      a-=0x37;
          else if(a<67)
             a-=0x57;
   if(b<0x40)
      b=-0x30;
   else if(b<0x47)
      b-=0x37;
          else if(b<67)
             b-=0x57;
        return((a<<4)+b);
}
uint hta(uchar a)
{
   uint b;
   b=a>>4;
   a&=0x0f;
   if(a<0x0a)
      a+=0x30;
   else
      a+=0x37;
   if(b<0x0a)
      b+=0x30;
   else
      b+=0x37;
   b=((b<<8)+a);
   return b;

}
void uart(void) interrupt 4           //檢查過了
{
   uchar a,b;
   if(RI)
   {
      a=SBUF;
          RI=0;
          if(a==0x40)//
             i=0;
          rec[i]=a;
          i++;
          if(a==0x0d)//
          {
             if(ath(rec[1],rec[2])==15)
                 {
                    b=0;
                        for(a=1;a<i-3;a++)
                        b^=rec[a];
                        if(b==ath(rec[i-3],rec[i-2]))
                        {
                           if((ath(rec[3],rec[4])&0x01)==0)          //
                           {
                              for(a=0;a<12;a++)           //
                                  {
                                     SBUF=send[a];
                                         while(TI!=1);
                                         TI=0;
                                  }
                           }
                        }
                        else   //
                        {
                           for(a=0;a<8;a++)         //
                           {
                              SBUF=error[a];
                                  while(TI!=1);
                                  TI=0;
                           }
                        }
                 }
          }
   }
   else
   {
      TI=0;
   }
}
開關(guān)量輸入,pc端和單片機相連

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表