<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 基于51單片機的多任務(wù)多進程

          基于51單片機的多任務(wù)多進程

          作者: 時間:2016-11-22 來源:網(wǎng)絡(luò) 收藏
          以下程序是轉(zhuǎn)載的,個人覺得是很好的學(xué)習(xí)單片機多進程編程的參考資料。



          //頭文件
          #include
          #include
          //宏定義
          #defineuchar unsigned char
          #define uint unsigned int
          #define TN 65436
          //進程1,2,3執(zhí)行時間之比為 T1:T2:T3 (時間單位us)
          #define TN1 62536 //1個進程循環(huán)周期內(nèi)進程1執(zhí)行的時間T1us TN1=(65536-T1)
          #define TN2 55536 //1個進程循環(huán)周期內(nèi)進程2執(zhí)行的時間T2us TN2=(65536-T1)
          #define TN3 60536 //1個進程循環(huán)周期內(nèi)進程3執(zhí)行的時間T3us TN3=(65536-T1)
          //
          #define N1 10 // 進程1的延時參數(shù)
          #define N2 10 // 進程2的延時參數(shù)
          #define N3 10 // 進程3的延時參數(shù)

          本文引用地址:http://www.ex-cimer.com/article/201611/320063.htm

          //定義全局變量
          uint address1,address2,address3;
          uchar test1_1=0,test2_1=0,test3_1=0,PID=1;
          //各進程的標志位,是否為第一次執(zhí)行;PID進程號;
          uint ac1,ac2,ac3; //, PC_Next; 各進程的初始地址寄存器.
          //test1 的參數(shù) 由于進程切換時 沒有保存普通變量,
          //所以各進程的普通參數(shù)需要定義成全局變量.
          uint m1,i1,j1,k1;
          uchar table1[4];
          //test2 的參數(shù)
          int m2,i2,j2,k2;
          uchar table2[4];
          //test3 的參數(shù)
          int m3,i3,j3,k3; //
          uchar table3[4];

          //聲明
          //unsigned int Get_Next_PC(void);//調(diào)用子程序,獲取PC
          void chushihua(void); //初始化函數(shù)
          void test1(void); //進程一
          void test2(void);
          void test3(void);

          //main函數(shù)
          void main(void)
          {
          // PC_Next=Get_Next_PC();
          chushihua();
          ac1=(unsigned int)(test1); //獲取進程1的入口地址
          ac2=(unsigned int)(test2);//獲取進程2的入口地址
          ac3=(unsigned int)(test3); //獲取進程3的入口地址
          //常規(guī)任務(wù)
          while(1);
          }

          //初始化時鐘
          void chushihua(void)
          {
          TMOD=0x01;//
          EA=1;//
          ET0=1;//
          TH0=TN/256;
          TL0=TN%6;
          TR0=1;
          }

          //中斷處理,進程調(diào)度
          void time0() interrupt 1 using 0
          {
          TR0=0;
          //進程順序分配
          PID++;
          if(PID==4)
          {PID=1;}
          //進程調(diào)度
          switch(PID)
          {
          case 1:
          if(test3_1!=0)
          { //保存現(xiàn)場,還回地址
          address3=*((unsigned char *)(SP-4));//PC的高字節(jié)
          address3 <<= 8;
          address3+=*((unsigned char *)(SP-5)); //PC的低字節(jié)
          table3[0]=*((unsigned char *)(SP)); //現(xiàn)場保護
          table3[1]=*((unsigned char *)(SP-1)); //現(xiàn)場保護
          table3[2]=*((unsigned char *)(SP-2)); //現(xiàn)場保護
          table3[3]=*((unsigned char *)(SP-3)); //現(xiàn)場保護
          }
          if(test1_1==0)
          { //執(zhí)行新進程
          test1_1=1;
          *((unsigned char *)(SP-4))=ac1>>8; //PC的高字節(jié)
          *((unsigned char *)(SP-5))=ac1 & 0x00ff; //PC的低字節(jié)
          }
          else
          { //執(zhí)行新進程,恢復(fù)現(xiàn)場
          *((unsigned char *)(SP-4))=address1>>8; //PC的高字節(jié)
          *((unsigned char *)(SP-5))=address1 & 0x00ff; //PC的低字節(jié)
          *((unsigned char *)(SP))=table1[0]; //現(xiàn)場恢復(fù)
          *((unsigned char *)(SP-1))=table1[1]; //現(xiàn)場恢復(fù)
          *((unsigned char *)(SP-2))=table1[2]; //現(xiàn)場恢復(fù)
          *((unsigned char *)(SP-3))=table1[3]; //現(xiàn)場恢復(fù)
          }
          TH0=TN1/256;
          TL0=TN1%6;
          TR0=1;
          break;

          case 2:
          if(test1_1!=0)
          { //保存現(xiàn)場,還回地址
          address1=*((unsigned char *)(SP-4)); //PC的高字節(jié)
          address1 <<= 8;
          address1+=*((unsigned char *)(SP-5)); //PC的低字節(jié)
          table1[0]=*((unsigned char *)(SP)); //現(xiàn)場保護
          table1[1]=*((unsigned char *)(SP-1));//現(xiàn)場保護
          table1[2]=*((unsigned char *)(SP-2));//現(xiàn)場保護
          table1[3]=*((unsigned char *)(SP-3)); //現(xiàn)場保護
          }
          if(test2_1==0)
          { //執(zhí)行進程2
          test2_1=1;
          *((unsigned char *)(SP-4))=ac2>>8; //PC的高字節(jié)
          *((unsigned char *)(SP-5))=ac2 & 0x00ff; //PC的低字節(jié)
          }
          else
          { //執(zhí)行進程2,恢復(fù)現(xiàn)場
          *((unsigned char *)(SP-4))=address2>>8;//PC的高字節(jié)
          *((unsigned char *)(SP-5))=address2 & 0x00ff; //PC的低字節(jié)
          *((unsigned char *)(SP))=table2[0]; //現(xiàn)場恢復(fù)
          *((unsigned char *)(SP-1))=table2[1]; //現(xiàn)場恢復(fù)
          *((unsigned char *)(SP-2))=table2[2]; //現(xiàn)場恢復(fù)
          *((unsigned char *)(SP-3))=table2[3]; //現(xiàn)場恢復(fù)
          }
          TH0=TN2/256;
          TL0=TN2%6;
          TR0=1;
          break;

          case 3:
          if(test2_1!=0)
          { //保存現(xiàn)場,還回地址
          address2=*((unsigned char *)(SP-4)); //PC的高字節(jié)
          address2 <<= 8;
          address2+=*((unsigned char *)(SP-5)); //PC的低字節(jié)
          table2[0]=*((unsigned char *)(SP)); //現(xiàn)場保護
          table2[1]=*((unsigned char *)(SP-1)); //現(xiàn)場保護
          table2[2]=*((unsigned char *)(SP-2)); //現(xiàn)場保護
          table2[3]=*((unsigned char *)(SP-3)); //現(xiàn)場保護
          }
          if(test3_1==0)
          { //執(zhí)行進程3
          test3_1=1;
          *((unsigned char *)(SP-4))=ac3>>8;//PC的高字節(jié)
          *((unsigned char *)(SP-5))=ac3 & 0x00ff; //PC的低字節(jié)
          }
          else
          { //執(zhí)行進程3,恢復(fù)現(xiàn)場
          *((unsigned char *)(SP-4))=address3>>8; //PC的高字節(jié)
          *((unsigned char *)(SP-5))=address3 & 0x00ff; //PC的低字節(jié)
          *((unsigned char *)(SP))=table3[0]; //現(xiàn)場恢復(fù)
          *((unsigned char *)(SP-1))=table3[1]; //現(xiàn)場恢復(fù)
          *((unsigned char *)(SP-2))=table3[2]; //現(xiàn)場恢復(fù)
          *((unsigned char *)(SP-3))=table3[3]; //現(xiàn)場恢復(fù)
          }
          TH0=TN3/256;
          TL0=TN3%6;
          TR0=1;
          break;
          default:
          TH0=TN/256;
          TL0=TN%6;
          TR0=1;
          break;
          }
          }

          //進程一 P1演示二進制加法死循環(huán)
          void test1(void)
          {
          while(1)
          {

          for(i1=0;i1<256;i1++)
          {
          for(k1=0;k1<=N1;k1++)
          {for(j1=0;j1<=20;j1++)
          for(m1=0;m1<=113;m1++);} // 約1ms*T1/T1+T2+T3
          P1=i1;
          //P2=0x0;
          }
          }
          }

          //進程二P2演示二進制加法死循環(huán)
          void test2(void)
          {
          while(1)
          {

          for(i2=0;i2<256;i2++)
          {
          for(k2=0;k2<=N2;k2++)
          {for(j2=0;j2<=20;j2++)
          for(m2=0;m2<=113;m2++);}
          P2=i2;
          }
          }
          }

          //進程三 P0口演示二進制加法死循環(huán) 進程3的延時參數(shù)
          void test3(void)
          {
          while(1)
          {

          for(i3=0;i3<256;i3++)
          {
          for(k3=0;k3<=N3;k3++)
          {for(j3=0;j3<=20;j3++)
          for(m3=0;m3<=113;m3++);}
          P0=i3;
          }
          }
          }



          關(guān)鍵詞: 51單片機多任務(wù)多進

          評論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();