2016-04-22 249 views
0

我試圖從Arduino生成脈衝到驅動電機步進5階段。 驅動器只需要脈衝使電機步進器工作。當我用這樣的代碼如何在Arduino中產生少量脈衝後停止pwm?

for(int i=0; i <= 125; i++) 
{ 
    //analogWrite(13,125); 
    digitalWrite(13, HIGH); 
    delayMicroseconds(300); 
    digitalWrite(13, LOW); 
    delayMicroseconds(300); 
} 
digitalWrite(13,LOW); 
delay(3000); 

步進電機可以完美地工作,但經過十餘旋轉, 的電機的角度沒有回到原來的地方我的問題是。我們可以像這樣在Arduino中使用pwm嗎?所以在使用pwm產生5000個脈衝之後,我們停止pwm?

+0

你能讓它更清楚嗎? – Ccr

+0

好吧,我買的驅動器電機步進電機, 驅動器的輸出是cw + dan cw-, 如果我們給驅動器發出脈衝,步進電機會移動, 1脈衝(1高電平和1低電平)會使步進電機移動0.72 degre 所以如果我想要移動該步進器,我們將需要125脈衝 當我使用上面的代碼產生脈衝, 步進電機沒有移動90度,(有錯誤1或2度) –

回答

0

試試這個代碼:

#include <TimerOne.h> 
const byte CLOCKOUT = 11; 
volatile byte counter=0; 

void setup() { 
    Timer1.initialize(15);   //Every 15 microseconds change the state of the pin in the wave function giving a period of 30 microseconds 
    Timer1.attachInterrupt(Onda); 
    pinMode (CLOCKOUT, OUTPUT); 
    digitalWrite(CLOCKOUT,HIGH); 
} 
void loop() { 
    if (counter>=6000){    //With 6000 changes you should achieve the amount of pulses you need 
     Timer1.stop();    //Here I create the dead time, which must be in HIGH 
     PORTB = B00001000; 
     counter=0; 
     delayMicroseconds(50); 
     Timer1.resume(); 
    } 
} 
void Onda(){ 
    PORTB ^= B00001000; //Change pin status 
    counter+=1; 
} 

我只是不能沒有消除抖動。如果你找到一個解決方案讓我知道