|
- #include <reg52.h> //頭文件
- #define uchar unsigned char
- #define uint unsigned int
- uchar code CCW[8]={0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09}; //逆時鐘旋轉(zhuǎn)相序表
- uchar code CW[8]={0x09,0x01,0x03,0x02,0x06,0x04,0x0c,0x08}; //正時鐘旋轉(zhuǎn)相序表
- sbit S2=P3^0;
- sbit S3=P3^1;
- void delaynms(uint aa)
- {
- uchar bb;
- while(aa--)
- {
- for(bb=0;bb<115;bb++) //1ms基準(zhǔn)延時程序
- {
- ;
- }
- }
- }
- void motor_ccw(void) //正轉(zhuǎn)每次轉(zhuǎn)5.625度
- {
- uchar i,j;
- for(j=0;j<64;j++)//電機(jī)旋轉(zhuǎn)一周,不是外面所看到的一周,是里面的傳動輪轉(zhuǎn)了一周
- {
- for(i=0;i<8;i++) //旋轉(zhuǎn)45度
- {
- P1=CCW[i];
- delaynms(2); //調(diào)節(jié)轉(zhuǎn)速
- }
- }
- }
- void motor_cw(void)//反轉(zhuǎn)每次轉(zhuǎn)5.625度
- {
- uchar i,j;
- for(j=0;j<64;j++)
- {
- for(i=0;i<8;i++) //旋轉(zhuǎn)45度
- {
- P1=CW[i];
- delaynms(2); //調(diào)節(jié)轉(zhuǎn)速
- }
- }
- }
- //停止轉(zhuǎn)動
- void MotorStop(void)
- {
- P1=0x00;
- }
- void main(void)
- {
- uchar r;
- uchar N=4; //因?yàn)椴竭M(jìn)電機(jī)是減速步進(jìn)電機(jī),減速比的1/64 ,所以N=64時,步進(jìn)電機(jī)主軸轉(zhuǎn)一圈 64*5.625=360
- while(1)
- {
- // if(S2==0)
- {
- for(r=0;r<N;r++)
- {
- motor_ccw(); //電機(jī)正轉(zhuǎn)
- delaynms(100);
- MotorStop();
- delaynms(100);
- }
- MotorStop(); //停止轉(zhuǎn)動
- delaynms(500);
- }
- // if(S3==0)
- {
- for(r=0;r<N;r++)
- {
- motor_cw(); //電機(jī)反轉(zhuǎn)
- delaynms(100);
- MotorStop();
- delaynms(100);
- }
- MotorStop(); //停止轉(zhuǎn)動
- delaynms(500);
- }
- }
- }
復(fù)制代碼 |
|