|
自己做的小項(xiàng)目代碼
單片機(jī)源程序如下:
- #include<reg52.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar code CCW[8]={0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09}; //逆時(shí)鐘旋轉(zhuǎn)相序表
- uchar code CW[8]= {0x09,0x01,0x03,0x02,0x06,0x04,0x0c,0x08}; //正時(shí)鐘旋轉(zhuǎn)相序表
- sbit K1=P3^5; //反轉(zhuǎn)按鍵
- sbit K2=P3^3; //正轉(zhuǎn)按鍵
- sbit K3=P3^1; //反轉(zhuǎn)按鍵
- sbit K4=P3^0; //正轉(zhuǎn)按鍵
- sbit K5=P3^2; //正轉(zhuǎn)按鍵
- void delaynms(uint aa)
- {
- uchar bb;
- while(aa--)
- {
- for(bb=0;bb<115;bb++); //1ms基準(zhǔn)延時(shí)程序
- }
- }
-
- void motor_ccw(void)
- { uchar i,j;
- for(j=0;j<8;j++) //電機(jī)旋轉(zhuǎn)一周,不是外面所看到的一周,是里面的傳動(dòng)輪轉(zhuǎn)了一周
- {
- for(i=0;i<8;i++) //旋轉(zhuǎn)45度
- { P1=CCW[i];
- delaynms(10); //調(diào)節(jié)轉(zhuǎn)速
- }
- }
- }
- void motor_ccw0(void)
- { uchar i,j;
- for(j=0;j<8;j++) //電機(jī)旋轉(zhuǎn)一周,不是外面所看到的一周,是里面的傳動(dòng)輪轉(zhuǎn)了一周
- {
- for(i=0;i<8;i++) //旋轉(zhuǎn)45度
- { P0=CCW[i];
- delaynms(10); //調(diào)節(jié)轉(zhuǎn)速
- }
- }
- }
- void motor_cw(void)
- { uchar i,j;
- for(j=0;j<8;j++)
- {
- for(i=0;i<8;i++) //旋轉(zhuǎn)45度
- { P1=CW[i];
- delaynms(10); //調(diào)節(jié)轉(zhuǎn)速
- }
- }
- }
- void motor_cw0(void)
- { uchar i,j;
- for(j=0;j<8;j++)
- {
- for(i=0;i<8;i++) //旋轉(zhuǎn)45度
- { P0=CW[i];
- delaynms(10); //調(diào)節(jié)轉(zhuǎn)速
- }
- }
- }
- void main(void)
- { uchar r;
- uchar N=1; //因?yàn)椴竭M(jìn)電機(jī)是減速步進(jìn)電機(jī),減速比的1/64 ,
- //所以N=64時(shí),步進(jìn)電機(jī)主軸轉(zhuǎn)一圈
- while(1)
- {
- if(K1==0)
- {for(r=0;r<N;r++)
- motor_ccw(); //電機(jī)逆轉(zhuǎn)
- }
- else if(K3==0)
- {for(r=0;r<N;r++)
- motor_ccw0(); //電機(jī)逆轉(zhuǎn)
- }
- else if(K2==0)
- {for(r=0;r<N;r++)
- motor_cw(); //電機(jī)反轉(zhuǎn)
- }
- else if(K4==0)
- {for(r=0;r<N;r++)
- motor_cw0(); //電機(jī)反轉(zhuǎn)
- }
- else if(K5==0)
- {for(r=0;r<N;r++)
- motor_cw0(); //電機(jī)反轉(zhuǎn)
- }
- else P1=0xf0; //電機(jī)停止
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
步進(jìn)電機(jī).zip
(30.87 KB, 下載次數(shù): 103)
2018-5-22 20:57 上傳
點(diǎn)擊文件名下載附件
代碼
|
評(píng)分
-
查看全部評(píng)分
|