网上不是很好找的资料哦,我们自己做的。
这是C程序:#include#defineucharunsignedcharsbitP1_0=P1^0;sbitP1_1=P1^1;sbitP1_3=P1^3;sbitP1_4=P1^4;sbitP1_5=P1^5;sbitP1_6=P1^6;sbitP1_7=P1^7;ucharRRR,flg,KKK;//RRR用于调速控制;
flg=0正转;
flg=1反转;flg=2不转;
KKK为P1的状态寄存ucharloop[2][4]={{0x0c,0x06,0x03,0x09},{0x09,0x03,0x06,0x0c}};voidloop1(void);voidloop2(void);voidstep(void);main(){uchari,j;TMOD=0x10;TL1=0xe0;TH1=0xb1;IE=0;while(1){if(KKK!=P1)//当P1的值发生变化,触发采集信号loop1();if(flg!=2){for(i=0;i<=3;i++){P0=loop[flg][i];for(j=0;j<=RRR;j++) {step();} }}}}voidstep(void)//产生20MS的单位步时间{TF1=0;TR1=1;while(TF1==0);TR1=0;TL1=0xe0;TH1=0xb1;}voidloop1(void)//采集顺时针或逆时针信号,P1.6=1,顺时针,P1.7=1,逆时针{KKK=P1;//暂存P1的状态if(P1_6==1){flg=0;loop2();}elseif(P1_7==1){flg=1;loop2();}elseflg=2;}voidloop2(void){if(P1_0==1)RRR=5;elseif(P1_1==1)RRR=8;elseif(P1_2==1)RRR=11;elseif(P1_3==1)RRR=14;elseif(P1_4==1)RRR=17;elseif(P1_5==1)RRR=20;}
1