標(biāo)題: 單片機(jī)雙機(jī)通訊源代碼 [打印本頁]

作者: txyxyxy    時(shí)間: 2016-7-19 22:42
標(biāo)題: 單片機(jī)雙機(jī)通訊源代碼

#include <reg52.h>
#define uchar unsigned char   //用uchar代替unsigned char
#define uint unsigned int     //用unitdaiti unsigned int
sbit P20=P2^0;        //LED輸出管腳
uchar Receive_Data[6],cNumber=0,cFlag=0;  //串口接收數(shù)據(jù)存放
uchar zhentouxulie;
uchar send0;
uchar send1;
uchar position;
uchar yiwei;
uchar biaozhi;
bit zhentou;
bit shuchu;
uchar PPM[41];         //存放一幀的高低電平信息 6個(gè)時(shí)隙的幀頭+32個(gè)時(shí)隙的信息
uchar wz;
uint bit1;
uint bit0;                                //位置控制位


void DelayMs(unsigned int Delay_MS)
{
uint iNumber,iValue;
for(iNumber=0;iNumber<Delay_MS;iNumber++)
{
     iValue=107;
     while(iValue--);
}
}

void Init_UART()                //串口初始化
{
    SCON=0x50;      //SCON:串行口工作方式1, 8-bit UART,允許串行接收位(REN=1)
    TMOD=0x21;      //TMOD:定時(shí)器1,方式2,8位自動(dòng)重裝方式
    PCON=0x80;      //SMOD=1;(將串口傳送的波特率加倍)
    TH1=0xfA;       //Baud:9600  fosc=11.0592MHz
        TL1=0xfA;  
        TH0=(65536-500)/256;//賦初值定時(shí)
    TL0=(65536-500)%256;//定時(shí)500us     
        ET0=1;//開定時(shí)器0中斷  
        TR0=1;//啟動(dòng)定時(shí)器0
        TR1=1;          //啟動(dòng)定時(shí)器/計(jì)數(shù)器1運(yùn)行
        ES=1;      //開串口中斷
        EA=1;   //開總中斷
        P20=0;
        send0=0;
        send1=0;
}

void tiaozhi()
{
                if((send0==1)&&(send1==1))
                {
                        if(wz<40)
                        {
                                if((wz==0)||(wz==7))       
                                {
                                        PPM[wz]=0;
                                        wz++;
                                }
                                else if((wz>0)&&(wz<7))
                                {
                                        PPM[wz]=1;
                                        wz++;
                                }
                                else if((wz>7)&&(wz<24))  //開始跳入數(shù)據(jù)發(fā)送
                                {         
                                                if(Receive_Data[0]&0x01) //1發(fā)送
                                                {
                                                        PPM[wz]=1;
                                                        PPM[wz+1]=0;
                                                }
                                                else                        //0發(fā)送
                                                {
                                                        PPM[wz]=0;
                                                        PPM[wz+1]=1;       
                                                }
                                        wz=wz+2;
                                        Receive_Data[0]=Receive_Data[1]>>1;               
                                }

                                else if((wz>23)&&(wz<40))  //開始跳入數(shù)據(jù)發(fā)送
                                {         
                                                if(Receive_Data[1]&0x01) //1發(fā)送
                                                {
                                                        PPM[wz]=1;
                                                        PPM[wz+1]=0;
                                                }
                                                else                        //0發(fā)送
                                                {
                                                        PPM[wz]=0;
                                                        PPM[wz+1]=1;       
                                                }
                                        wz=wz+2;
                                        Receive_Data[1]=Receive_Data[1]>>1;               
                                }
                        }
                        else if(wz==40)
                        {
                                PPM[40]=0;//輸出一個(gè)保護(hù)電平時(shí)隙
                                wz=0;
                                send0=0;
                                send1=1;
                                position=0;
                        }
           }
               
}




void main()
{
DelayMs(50);    //穩(wěn)定系統(tǒng)
Init_UART(); //初始化串口
while(1)
{
        tiaozhi();
        if((send0==0)&&(send1==0))
          {
                send0=1;
                send1=1;          
                Receive_Data[1]=0xff;
                Receive_Data[0]=0x00;
                wz=0;//編碼位置
                DelayMs(1000);
          }
}
}


void timer0() interrupt 1
{  
        TR0=0;//賦初值時(shí),關(guān)閉定時(shí)器
           TH0=(65536-500)/256;//賦初值定時(shí) 高八位
        TL0=(65536-500)%256;//定時(shí)500us  低八位
        TR0=1;//打開定時(shí)器
                    
          if((send1==1)&&(send0==0))  //編碼完成
          {
                  if(position<41)
                {
                  if(PPM[position]==1)
                  {
                          P20=0;
                  }
                  else
                  {
                          P20=1;
                  }
                 }
                 else
                 {
                         send1=0;//發(fā)送完成
                        send0=0;
                 }
                 position++;
         }
         else
                 P20=0;//其余點(diǎn)亮燈


}
void UART_Routine() interrupt 4
{
   if(RI==1)//RI為接受中斷標(biāo)志,可能是發(fā)送產(chǎn)生的中斷
{
          ES=0;      //關(guān)串口中斷
          Receive_Data[cNumber++]=SBUF;//將接受數(shù)據(jù)放入預(yù)置數(shù)組
          if(cNumber==2)
          {
                send0=1;
                send1=1;          
                cNumber=0;
                wz=0;//編碼位置
          }
          RI=0;//接受中斷標(biāo)志軟件清零
          ES=1;      //開串口中斷
}
}




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1