正转正常,而反转按90度反转正常,按了反转再按圈数对应的按键无法实现,出现无限正转,请问问题所在?
- #include<reg52.h>
- sbit key_in1=P2^4;
- sbit key_in2=P2^5;
- sbit key_in3=P2^6;
- sbit key_in4=P2^7;
- sbit key_out1=P2^0;
- sbit key_out2=P2^1;
- sbit key_out3=P2^2;
- sbit key_out4=P2^3;
- bit dirmotor=0;
- unsigned char code keymap[4][4]={
- {0x28,0x27,0x26,0x25},
- {0x0D,0x39,0x36,0x33},
- {0x1B,0x38,0x35,0x32},
- {0x30,0x37,0x34,0x31}};
- unsigned char keysta[4][4]={
- {1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}};
- signed long beats;
- void keyDriver();
-
- void main()
- {
- EA=1;
- TMOD=0x01;
- TH0=0xFC;
- TL0=0x67;
- ET0=1;
- TR0=1;
-
- while(1)
- {
- keyDriver();
- }
- }
- void starmotor(signed long angle)
- {
- EA=0;
- beats= ( angle * 4076 ) / 360;
- EA=1;
- }
- void keyaction(unsigned int keycode)
- {
- static bit dirmotor=0;
- if((keycode>=0x30)&&(keycode<=0x39))
- {
- if(dirmotor==0)
-
- starmotor((keycode-0x30)*360);
-
- else
- starmotor((keycode-0x30)*360*);
-
-
- }
- else if(keycode==0x0D)
- {
- dirmotor=1;
- }
- else if(keycode==0x26)
- {
- dirmotor=0;
- }
- else if(keycode==0x28)
- {
-
- starmotor(90);
-
- }
- else if(keycode==0x27)
- {
- starmotor(-90);
- }
- else if(keycode==0x1B)
- {
- EA=0;
- beats=0;
- EA=1;
- }
- }
-
- void keyDriver()
- {
- static unsigned char backup[4][4]={
- {1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}};
-
- unsigned char j=0;
- unsigned char k=0;
- for(j=0;j<4;j++)
- {
- for(k=0;k<4;k++)
- {
- if(keysta[j][k]!=backup[j][k])
- {
- if(backup[j][k]==0)
- {
- keyaction(keymap[j][k]);
- }
- backup[j][k]=keysta[j][k];
-
- }
- }
- }
- }
-
- void keyscan()
- {
- static unsigned char keybuf[4][4]={
- {0xFF,0xFF,0xFF,0xFF},
- {0xFF,0xFF,0xFF,0xFF},
- {0xFF,0xFF,0xFF,0xFF},
- {0xFF,0xFF,0xFF,0xFF}};
- unsigned char i=0;
- static unsigned char cnt=0;
-
- keybuf[cnt][0]=(keybuf[cnt][0]<<1)|key_in1;
- keybuf[cnt][1]=(keybuf[cnt][1]<<1)|key_in2;
- keybuf[cnt][2]=(keybuf[cnt][2]<<1)|key_in3;
- keybuf[cnt][3]=(keybuf[cnt][3]<<1)|key_in4;
- for(i=0;i<4;i++)
- {
- if((keybuf[cnt][i]&0x0F)==0x0F)
- {
- keysta[cnt][i]=1;
- }
- else if ((keybuf[cnt][i]&0x00)==0x00)
- {
- keysta[cnt][i]=0;
- }
- }
- cnt++;
- cnt=cnt&0x03;
- switch(cnt)
- {
- case 0 : key_out4=1;key_out1=0;break;
- case 1 : key_out1=1;key_out2=0;break;
- case 2 : key_out2=1;key_out3=0;break;
- case 3 : key_out3=1;key_out4=0;break;
- default: break;
- }
- }
-
-
-
- void turnmotor()
- {
- unsigned char tmp;
- unsigned char code beatscode[8]={
- 0xE,0xC,0xD,0x9,0xB,0x3,0x7,0x6};
- static signed char index=0;
-
- if(beats>0)
- {
- tmp=P1;
- tmp=index&0xF0;
- tmp=tmp|beatscode[index];
- P1=tmp;
- index++;
- index=index&0x07;
- beats--;
- }
- else if (beats<0)
- {
- tmp=P1;
- tmp=index&0xF0;
- tmp=tmp|beatscode[index];
- P1=tmp;
- index--;
- if(index<0)
- {
- index=7;
- }
- beats++;
- }
- else
- {
- P1=P1|0x0F;
- }
- }
-
- void interrupttimer0() interrupt 1
- {
- static bit div=0;
- TH0=0xFC;
- TL0=0x67;
- keyscan();
- div=~div;
- if(div==1)
- {
- turnmotor();
- }
- }
复制代码
|