單片機控制步進電機實現直線插補仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
0.png (21.02 KB, 下載次數: 31)
下載附件
2018-7-10 19:13 上傳
單片機源程序如下:
- #include<reg51.h>
- #define uint unsigned int
- #define uchar unsigned char
- uchar code bux[]={0xf0,0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9};
- uchar code buy[]={0x00,0x10,0x30,0x20,0x60,0x40,0xc0,0x80,0x90};
- uchar code comx[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- uchar code comy[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- sbit SX=P3^4;
- sbit SY=P3^5;
- sbit ST=P3^6;
- uint countx=0;
- uint county=0;
- uint count,flag=0;
- uint x=0;
- uint y=0;
- uint a=0,b=0,k=0,m=0;
- uint st=0;
- char Fm[20];
- void Key_Scan(void);
- void Key_Action(void);
- void motor();
- void stepx();
- void stepy();
- void star();
- void time0(void);
- void delayms(uint t)
- {
- uint i,j;
- for(i=0;i<t;i++)
- for(j=0;j<112;j++);
- }
- void main()
- {
- while(1)
- {
- Key_Scan();
- time0();
- while(!flag);
- flag=0;
- motor();
- if(1==st)
- {
- star();
- st=0;
- }
-
- }
- }
- void Key_Scan(void)
- {
- if(0==SX)
- {
- delayms(10);
- if(0==SX)
- {
- countx++;
- while(!SX);
- }
- }
- if(0==SY)
- {
- delayms(10);
- if(0==SY)
- {
- county++;
- while(!SY);
- }
- }
-
- }
- void time0(void)
- {
- TMOD=0x01;
- TH0=(65536-9216)/256;
- TL0=(65536-9216)%256;
- EA=1;
- ET0=1;
- TR0=1;
- }
- void TimeRS(void) interrupt 1
- {
- TH0=(65536-9216)/256;
- TL0=(65536-9216)%256;
- count++;
- if(count>=10)
- {
- Key_Action();
- flag=1;
- count=0;
- }
-
- }
- void Key_Action(void)
- {
- uint i,j;
- if(countx>9)
- {
- countx=0;
- }
- if(county>9)
- {
- county=0;
- }
- P0=comx[countx];
- //delayms(100);
- P2=comy[county];
- //delayms(100);
-
-
- }
- void motor()
- {
- uint i,j;
- if(0==ST)
- {
- delayms(10);
- if(0==ST)
- {
- st=1;
- while(!ST);
- }
- }
- }
- void star()
- {
- a=countx;
- b=county;
- m=a+b;
- Fm[k]=0;
- while(m>0)
- {
- if(Fm[k]>=0)
- {
- x++;
- m--;
- stepx();
- k++;
- Fm[k]=Fm[k-1]-b;
- }
- if(Fm[k]<0)
- {
- y++;
- m--;
- stepy();
- k++;
- Fm[k]=Fm[k-1]+a;
-
- }
- }
- }
- void stepx()
- {
- P3=bux[x];
- delayms(1000);
- }
- void stepy()
- {
- P1=bux[y];
- delayms(1000);
- }
- #include<reg51.h>
- #define uint unsigned int
- #define uchar unsigned char
- uchar code bux[]={0xf0,0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9};
- uchar code buy[]={0x00,0x10,0x30,0x20,0x60,0x40,0xc0,0x80,0x90};
- uchar code comx[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- uchar code comy[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- sbit SX=P3^4;
- sbit SY=P3^5;
- sbit ST=P3^6;
- uint countx=0;
- uint county=0;
- uint count,flag=0;
- uint x=0;
- uint y=0;
- uint a=0,b=0,k=0,m=0;
- uint st=0;
- char Fm[20];
- void Key_Scan(void);
- void Key_Action(void);
- void motor();
- void stepx();
- void stepy();
- void star();
- void time0(void);
- void delayms(uint t)
- {
- uint i,j;
- for(i=0;i<t;i++)
- for(j=0;j<112;j++);
- }
- void main()
- {
- while(1)
- {
- Key_Scan();
- time0();
- while(!flag);
- flag=0;
- motor();
- if(1==st)
- {
- star();
- st=0;
- }
-
- }
- }
- void Key_Scan(void)
- {
- if(0==SX)
- {
- delayms(10);
- if(0==SX)
- {
- countx++;
- while(!SX);
- }
- }
- if(0==SY)
- {
- delayms(10);
- if(0==SY)
- {
- county++;
- while(!SY);
- }
- }
-
- }
- void time0(void)
- {
- TMOD=0x01;
- TH0=(65536-9216)/256;
- TL0=(65536-9216)%256;
- EA=1;
- ET0=1;
- TR0=1;
- }
- void TimeRS(void) interrupt 1
- {
- TH0=(65536-9216)/256;
- TL0=(65536-9216)%256;
- count++;
- if(count>=10)
- {
- Key_Action();
- flag=1;
- count=0;
- }
-
- }
- void Key_Action(void)
- {
- uint i,j;
- if(countx>9)
- {
- countx=0;
- }
- if(county>9)
- {
- county=0;
- }
- P0=comx[countx];
- //delayms(100);
- P2=comy[county];
- //delayms(100);
-
-
- }
- void motor()
- {
- uint i,j;
- if(0==ST)
- {
- delayms(10);
- if(0==ST)
- {
- st=1;
- while(!ST);
- }
- }
- }
- void star()
- {
- a=countx;
- b=county;
- m=a+b;
- Fm[k]=0;
- while(m>0)
- {
- if(Fm[k]>=0)
- {
- x++;
- m--;
- stepx();
- k++;
- Fm[k]=Fm[k-1]-b;
- }
- if(Fm[k]<0)
- {
- y++;
- m--;
- stepy();
- k++;
- Fm[k]=Fm[k-1]+a;
-
- }
- }
- }
- void stepx()
- {
- P3=bux[x];
- delayms(1000);
- }
- void stepy()
- {
- P1=bux[y];
- delayms(1000);
- }
復制代碼
0.png (53.37 KB, 下載次數: 37)
下載附件
2018-7-10 19:15 上傳
所有資料51hei提供下載:
步進電機.rar
(32.22 KB, 下載次數: 81)
2018-7-10 14:35 上傳
點擊文件名下載附件
步進電機控制 下載積分: 黑幣 -5
|