以下程序是轉(zhuǎn)載的,個(gè)人覺得是很好的學(xué)習(xí)單片機(jī)多進(jìn)程編程的參考資料。
//頭文件
#include
#include
//宏定義
#defineuchar unsigned char
#define uint unsigned int
#define TN 65436
//進(jìn)程1,2,3執(zhí)行時(shí)間之比為 T1:T2:T3 (時(shí)間單位us)
#define TN1 62536 //1個(gè)進(jìn)程循環(huán)周期內(nèi)進(jìn)程1執(zhí)行的時(shí)間T1us TN1=(65536-T1)
#define TN2 55536 //1個(gè)進(jìn)程循環(huán)周期內(nèi)進(jìn)程2執(zhí)行的時(shí)間T2us TN2=(65536-T1)
#define TN3 60536 //1個(gè)進(jìn)程循環(huán)周期內(nèi)進(jìn)程3執(zhí)行的時(shí)間T3us TN3=(65536-T1)
//
#define N1 10 // 進(jìn)程1的延時(shí)參數(shù)
#define N2 10 // 進(jìn)程2的延時(shí)參數(shù)
#define N3 10 // 進(jìn)程3的延時(shí)參數(shù)
本文引用地址:http://m.butianyuan.cn/article/201611/320063.htm//定義全局變量
uint address1,address2,address3;
uchar test1_1=0,test2_1=0,test3_1=0,PID=1;
//各進(jìn)程的標(biāo)志位,是否為第一次執(zhí)行;PID進(jìn)程號(hào);
uint ac1,ac2,ac3; //, PC_Next; 各進(jìn)程的初始地址寄存器.
//test1 的參數(shù) 由于進(jìn)程切換時(shí) 沒有保存普通變量,
//所以各進(jìn)程的普通參數(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); //進(jìn)程一
void test2(void);
void test3(void);
//main函數(shù)
void main(void)
{
// PC_Next=Get_Next_PC();
chushihua();
ac1=(unsigned int)(test1); //獲取進(jìn)程1的入口地址
ac2=(unsigned int)(test2);//獲取進(jìn)程2的入口地址
ac3=(unsigned int)(test3); //獲取進(jìn)程3的入口地址
//常規(guī)任務(wù)
while(1);
}
//初始化時(shí)鐘
void chushihua(void)
{
TMOD=0x01;//
EA=1;//
ET0=1;//
TH0=TN/256;
TL0=TN%6;
TR0=1;
}
//中斷處理,進(jìn)程調(diào)度
void time0() interrupt 1 using 0
{
TR0=0;
//進(jìn)程順序分配
PID++;
if(PID==4)
{PID=1;}
//進(jìn)程調(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)場保護(hù)
table3[1]=*((unsigned char *)(SP-1)); //現(xiàn)場保護(hù)
table3[2]=*((unsigned char *)(SP-2)); //現(xiàn)場保護(hù)
table3[3]=*((unsigned char *)(SP-3)); //現(xiàn)場保護(hù)
}
if(test1_1==0)
{ //執(zhí)行新進(jìn)程
test1_1=1;
*((unsigned char *)(SP-4))=ac1>>8; //PC的高字節(jié)
*((unsigned char *)(SP-5))=ac1 & 0x00ff; //PC的低字節(jié)
}
else
{ //執(zhí)行新進(jìn)程,恢復(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)場保護(hù)
table1[1]=*((unsigned char *)(SP-1));//現(xiàn)場保護(hù)
table1[2]=*((unsigned char *)(SP-2));//現(xiàn)場保護(hù)
table1[3]=*((unsigned char *)(SP-3)); //現(xiàn)場保護(hù)
}
if(test2_1==0)
{ //執(zhí)行進(jìn)程2
test2_1=1;
*((unsigned char *)(SP-4))=ac2>>8; //PC的高字節(jié)
*((unsigned char *)(SP-5))=ac2 & 0x00ff; //PC的低字節(jié)
}
else
{ //執(zhí)行進(jìn)程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)場保護(hù)
table2[1]=*((unsigned char *)(SP-1)); //現(xiàn)場保護(hù)
table2[2]=*((unsigned char *)(SP-2)); //現(xiàn)場保護(hù)
table2[3]=*((unsigned char *)(SP-3)); //現(xiàn)場保護(hù)
}
if(test3_1==0)
{ //執(zhí)行進(jìn)程3
test3_1=1;
*((unsigned char *)(SP-4))=ac3>>8;//PC的高字節(jié)
*((unsigned char *)(SP-5))=ac3 & 0x00ff; //PC的低字節(jié)
}
else
{ //執(zhí)行進(jìn)程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;
}
}
//進(jìn)程一 P1演示二進(jìn)制加法死循環(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;
}
}
}
//進(jìn)程二P2演示二進(jìn)制加法死循環(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;
}
}
}
//進(jìn)程三 P0口演示二進(jìn)制加法死循環(huán) 進(jìn)程3的延時(shí)參數(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;
}
}
}
評(píng)論