- #include <reg51.h> //* 单双八拍工作方式:A-AB-B-BC-C-CD-D-DA*
- unsigned char code shun[8] = {0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09};
- unsigned char code ni[8] = {0x09,0x01,0x03,0x02,0x06,0x04,0x0c,0x08};
- void delay(char x) //延时程序
- {
- char i,j;
- for(i=x;i>0;i--)
- for(j=110;j>0;j--);
- }
- void motor_1(float a)//a即想要转的圈数
- {
- unsigned char b;
- unsigned int c;
- for (c=0; c<8*64*a; c++)
- {
- for (b=0; b<8; b++)
- {
- P1 = shun[b];
- delay(10);
- }
- }
- }
- void motor_2(float a)//a即想要转的圈数
- {
- unsigned char b;
- unsigned int c;
- for (c=0; c<8*64*a; c++)
- {
- for (b=0; b<8; b++)
- {
- P1 = ni[b];
- delay(10);
- }
- }
- }
- void main()//主程序
- {
- while(1)
- {
- motor_1(0.5); //转动1圈(可以改动)
- delay(10);
- P1 = 0; //防止电机发热
- //motor_2(0.5); //转动1圈(可以改动)
- //delay(10);
- // P1 = 0; //防止电机发热
- }
- }
复制代码 |