|
- //定义引脚
- int Pin0 = 8;
- int Pin1 = 9;
- int Pin2 = 10;
- int Pin3 = 11;
- int _step = 0;
- boolean dir = false;//正反转
- int stepperSpeed = 1;//电机转速,1ms一步
- //脉冲总数,或者说步的总数
- int stepsum=0;
- void setup()
- {
- pinMode(Pin0, OUTPUT);
- pinMode(Pin1, OUTPUT);
- pinMode(Pin2, OUTPUT);
- pinMode(Pin3, OUTPUT); }
- void loop()
- {
- //进步机的驱动方式,有八种
- switch(_step){
- case 0:
- digitalWrite(Pin0, LOW);
- digitalWrite(Pin1, LOW);
- digitalWrite(Pin2, LOW);
- digitalWrite(Pin3, HIGH);
- break;
- case 1:
- digitalWrite(Pin0, LOW);
- digitalWrite(Pin1, LOW);
- digitalWrite(Pin2, HIGH);
- digitalWrite(Pin3, HIGH);
- break;
- case 2:
- digitalWrite(Pin0, LOW);
- digitalWrite(Pin1, LOW);
- digitalWrite(Pin2, HIGH);
- digitalWrite(Pin3, LOW);
- break;
- case 3:
- digitalWrite(Pin0, LOW);
- digitalWrite(Pin1, HIGH);
- digitalWrite(Pin2, HIGH);
- digitalWrite(Pin3, LOW);
- break;
- case 4:
- digitalWrite(Pin0, LOW);
- digitalWrite(Pin1, HIGH);
- digitalWrite(Pin2, LOW);
- digitalWrite(Pin3, LOW);
- break;
- case 5:
- digitalWrite(Pin0, HIGH);
- digitalWrite(Pin1, HIGH);
- digitalWrite(Pin2, LOW);
- digitalWrite(Pin3, LOW);
- break;
- case 6:
- digitalWrite(Pin0, HIGH);
- digitalWrite(Pin1, LOW);
- digitalWrite(Pin2, LOW);
- digitalWrite(Pin3, LOW);
- break;
- case 7:
- digitalWrite(Pin0, HIGH);
- digitalWrite(Pin1, LOW);
- digitalWrite(Pin2, LOW);
- digitalWrite(Pin3, HIGH);
- break;
- default:
- digitalWrite(Pin0, LOW);
- digitalWrite(Pin1, LOW);
- digitalWrite(Pin2, LOW);
- digitalWrite(Pin3, LOW);
- break;
- }
- //判断是正转还是反转
- if(dir)
- {
- _step++;
- }else{
- _step--;
- }
- //每八步一个驱动方式的循环
- if(_step>7){
-
- _step=0;
- }
- if(_step<0){
-
- _step=7;
- }
-
- delay(stepperSpeed);
- //计算脉冲数,每4096个脉冲停1秒
- stepsum++;
- if(stepsum==4096)
- {
- Serial.println(stepsum);
- stepsum=0;
- delay(1000);
- }
- }
复制代码
用ardiuno实现的电机正反转
|
-
-
正反转.doc
18 KB, 下载次数: 18, 下载积分: 黑币 -5
如题
评分
-
查看全部评分
|